Commit a03d0d0e authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '610-new-job-pnpm-build' into 'latest'

Resolve "[New job] - Pnpm build"

Closes #610

See merge request r2devops/hub!375
parents 728c663b 7182d2a2
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
# Changelog
All notable changes to this job will be documented in this file.

## [0.1.0] - 2022-07-11
* Initial version
 No newline at end of file
+64 −0
Original line number Diff line number Diff line
## Objective

This job allows you to build your project with `pnpm` using a script (`build` by default)
from your `package.json` file. For more information see [the doc]('https://pnpm.io/cli/run')

## How to use it

1. Ensure that your project have
   [`package.json`](https://docs.npmjs.com/cli/v6/configuring-npm/package-json){:target="_blank"}
   file which contains the script required to build (`build` by default)
1. Copy the job URL located in the `Install` part of the right panel and add it inside the `include` list of your `.gitlab-ci.yml` file (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
1. If you need to customize the job (stage, variables, ...) 👉 check the [jobs
   customization](/use-the-hub/#jobs-customization)
1. Well done, your job is ready to work ! 😀

### Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| `PROJECT_ROOT` | Path to the directory containing `package.json` (path relative from the root of the repository) | `.` |
| `PNPM_INSTALL_OPTIONS` | Additional options for `pnpm install` | ` ` |
| `PNPM_BUILD_SCRIPT_NAME` | The build script in the `package.json` that you want to run | `build` |
| `PNPM_BUILD_OPTIONS` | Options passed to your build script | ` ` |
| `PNPM_BUILD_OUTPUT_FOLDER` | Path to the output produced by the `pnpm` build script used (path relative from the `PROJECT_ROOT`) | `dist` |
| `PAGES_DEPLOY` | Prepare your build result to be deployed on pages (require [pages job](jobs/deploy/pages/)) | `false` |
| `PAGES_FOLDER` | Path where to copy the output to be exposed for deployment on [pages](jobs/deploy/pages/) (path relative from the root of the repository) | `./website_build` |
| `IMAGE_TAG` | The default tag for the docker image | `18-buster`  |

### Example to deploy on pages

Following example of `.gitlab-ci.yml` file describes how to enable Gitlab pages
deployment using this job:

```yaml
stages:
  - build
  - deploy

include:
  # It's recommanded to use fixed version of jobs instead of larest. Here is only for example.
  - remote: 'https://api.r2devops.io/job/r/r2devops-bot/pnpm_build/latest.yml' 
  - remote: 'https://api.r2devops.io/job/r/r2devops-bot/pages/latest.yaml'

pnpm_build:
  variables:
    PAGES_DEPLOY: "true"
```

### Artifact

When the job is successful, the build result is available as artifact.

!!! warning
    It's also [exposed
    as](https://docs.gitlab.com/ee/ci/yaml/#artifactsexpose_as){:target="_blank"}
    `pnpm build` in merge requests.  Exposition of artifact currently works only
    if you keep `PNPM_BUILD_OUTPUT_FOLDER` default value because of [this issue
    from
    Gitlab](https://gitlab.com/gitlab-org/gitlab/-/issues/37129){:target="_blank"}.
    As soon as the issue will be fixed, exposed artifacts will be available
    with any output location.

### Author
This resource is an **[official job](https://docs.r2devops.io/faq-labels/)** added in [**R2Devops repository**](https://gitlab.com/r2devops/hub) by [@DjNaGuRo](https://gitlab.com/DjNaGuRo)
 No newline at end of file
+38 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io

pnpm_build:
  stage: build
  image: node:${IMAGE_TAG}
  variables:
    IMAGE_TAG: "18-buster"
    PNPM_INSTALL_OPTIONS: ""
    PNPM_BUILD_SCRIPT_NAME: "build"
    PNPM_BUILD_OPTIONS: ""
    PNPM_BUILD_OUTPUT_FOLDER: "dist"
    PAGES_DEPLOY: "false"
    PAGES_FOLDER: "./website_build"
    PROJECT_ROOT: "."
  
  before_script:
      - curl -f https://get.pnpm.io/v6.16.js | node - add --global pnpm@7
      - pnpm config set store-dir ${CI_PROJECT_DIR}/${PROJECT_ROOT}/.pnpm-store
  
  script:
    - cd ${PROJECT_ROOT}
    - pnpm install ${PNPM_INSTALL_OPTIONS}
    - pnpm run ${PNPM_BUILD_SCRIPT_NAME} ${PNPM_BUILD_OPTIONS}
    # To deploy on pages
    - if [ "$PAGES_DEPLOY" == "true" ]; then
        # Pages folder must to be at the root of the repo
    -   cd $CI_PROJECT_DIR
    -   cp -r ${PROJECT_ROOT}/${PNPM_BUILD_OUTPUT_FOLDER} ${CI_PROJECT_DIR}/${PAGES_FOLDER}
    - fi

  artifacts:
    expose_as: "pnpm build"
    paths:
      - ${PROJECT_ROOT}/${PNPM_BUILD_OUTPUT_FOLDER}
      - "${PAGES_FOLDER}"
      # Below path is a workaround to provide artifact exposition in MR if
      # default output value is used. See https://r2devops.io/jobs/build/npm_build/#artifact
      - "build/"
+0 −0

Empty file added.