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

Merge branch '281-new-job-yarn' into 'latest'

Resolve "[New job] - Yarn"

Closes #281

See merge request r2devops/hub!142
parents 25bd747e dd80c695
Loading
Loading
Loading
Loading
+72 −0
Original line number Diff line number Diff line
## Objective

This job allows you to build your project using a script (`build` by default)
from your `package.json` file.

## How to use it

1. Ensure that your project have
   [`package.json`](https://classic.yarnpkg.com/en/docs/package-json/){:target="_blank"}
   file which contains the script required to build (`build` by default)
1. Add the corresponding URL to your `.gitlab-ci.yml` file (see [Getting
   started](/getting-started)). Example:
    ```yaml
    include:
      - remote: 'https://jobs.r2devops.io/yarn_build.yml'
    ```
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 ! 😀


## Job details

* Job name: `yarn_build`
* Default stage: `build`
* Docker image: [`node:15.7-buster`](https://hub.docker.com/_/node){:target="_blank"}
* When: `always`


### Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| `PROJECT_ROOT` | Path to the directory containing `package.json` (path relative from the root of the repository) | `.` |
| `YARN_INSTALL_OPTIONS` | Additional options for `yarn install` | ` ` |
| `YARN_BUILD_SCRIPT_NAME` | The build script in the `package.json` that you want to run | `build` |
| `YARN_BUILD_OPTIONS` | Options passed to your build script | ` ` |
| `YARN_BUILD_OUTPUT_FOLDER` | Path to the output produced by the `yarn` build script used (path relative from the `PROJECT_ROOT`) | `build` |
| `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` |

### 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:
  - remote: 'https://jobs.r2devops.io/yarn_build.yml'
  - remote: 'https://jobs.r2devops.io/pages.yml'

yarn_build:
  variables:
    PAGES_DEPLOY: "true"
```

### Artifact

Result of  build is [exposed
as](https://docs.gitlab.com/ee/ci/yaml/#artifactsexpose_as){:target="_blank"}
`yarn build` in merge requests.


!!! warning
    Exposition of artifact doesn't work currently 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 in merge
    requests.
+6 −0
Original line number Diff line number Diff line
name: yarn_build
description: A ready to use `yarn` job to build your application
default_stage: build
icon: 🧱
maintainer: coconux
license: MIT
+0 −0

Empty file added.

+1 −0
Original line number Diff line number Diff line
* Initial version
+35 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io

stages:
  - build

yarn_build:
  stage: build
  image:
    name: node:15.7-buster
    entrypoint: [""]
  variables:
    PROJECT_ROOT: "."
    YARN_INSTALL_OPTIONS: ""
    YARN_BUILD_SCRIPT_NAME: "build"
    YARN_BUILD_OPTIONS: ""
    YARN_BUILD_OUTPUT_FOLDER: "build"
    PAGES_DEPLOY: "false"
    PAGES_FOLDER: "./website_build"
  script:
    # Working directory
    - cd $PROJECT_ROOT
    - yarn install $YARN_INSTALL_OPTIONS
    - yarn run $YARN_BUILD_SCRIPT_NAME $YARN_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}/${YARN_BUILD_OUTPUT_FOLDER} ${CI_PROJECT_DIR}/${PAGES_FOLDER}
    - fi
  artifacts:
    expose_as: "yarn build"
    paths:
      - "${PROJECT_ROOT}/${YARN_BUILD_OUTPUT_FOLDER}"
      # To deploy on pages
      - "${PAGES_FOLDER}"