重要通知: 此文档针对的是 Yarn 1 (Classic) 版本。
对于 Yarn 2+ 文档及迁移指南,请访问 yarnpkg.cn。

Continuous Integration

Yarn can easily be used in various continuous integration systems. To speed up builds, the Yarn cache directory can be saved across builds.

Select the continuous integration system you're using from the options above

Yarn is preinstalled on AppVeyor, so you don’t need to do anything extra in order to use it as part of your build.

To speed up your builds, you can cache Yarn’s cache folder by adding this to your appveyor.yml:

cache:
 - "%LOCALAPPDATA%\\Yarn"

CircleCI provides documentation for Yarn. You can get up and running by following their Yarn documentation.

Yarn is pre-installed Codeship Basic.

If you are using Codeship Pro (with Docker), it is recommended to install Yarn via our Debian/Ubuntu package instead.

Travis CI detects the use of Yarn by the presence of yarn.lock in the repository root. If it is available, Travis CI will install yarn if necessary, and execute yarn as the default install command.

If your install phase requires more, it is necessary to install Yarn yourself until it is pre-installed on build images.

before_install: # if "install" is overridden
  # Repo for Yarn
  - sudo apt-key adv --fetch-keys http://dl.yarnpkg.com/debian/pubkey.gpg
  - echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  - sudo apt-get update -qq
  - sudo apt-get install -y -qq yarn
cache:
  yarn: true

It is recommended that you lock in a specific version of Yarn, so that all your builds use the same version of Yarn, and you can test new Yarn releases before switching over. You can do this by adding the version number to the apt-get install call:

sudo apt-get install -y -qq yarn=1.22.17-1

Semaphore has Yarn pre-installed for all supported Node.js versions, and no user interaction is required for the Yarn cache to work.

To assure that your local Yarn version matches the one on Semaphore, add the lines below to your setup commands, in Project Settings.

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb http://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
# install-package is a tool for caching APT installations in Semaphore
# defining a package version is optional
install-package --update-new yarn=<version>

Yarn is pre-installed on SolanoCI. You can quickly get up and running by following their Yarn documentation. For an example configuration file, check out one of their sample configuration files.

Because GitLab CI uses docker in the background, you can specify an image with yarn pre-installed.

# .gitlab-ci.yml
image: node:9.4.0

If you’re using a docker image that doesn’t come with yarn pre-installed you can still install it after the container has loaded.

# .gitlab-ci.yml
image: does-not-have-yarn

before_script:
  # Install yarn as outlined in (https://yarnpkg.com/lang/en/docs/install/#alternatives-stable)
  - curl -o- -L https://yarnpkg.com/install.sh | bash
  # Make available in the current terminal
  - export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"

In either case, it’s good practice to cache your .yarn folder as well to speed up your builds.

# .gitlab-ci.yml
cache:
  paths:
    - .yarn

Here’s an example .gitlab-ci.yml file using yarn to run a testing suite. Just save this file to the root of your project and GitLab’s CI will pick up the jobs.

# .gitlab-ci.yml
image: node:9.11.1

before_script:
  - yarn install --cache-folder .yarn

test:
  stage: test
  cache:
    paths:
    - node_modules/
    - .yarn

Codefresh pipelines use Docker images in all their steps so it is very easy to use any Yarn version in any pipeline.

This pipeline checks out the source code and then runs yarn with two freestyle steps.

codefresh.yml

version: '1.0'
stages:
  - prepare
  - test
  - build
steps:
  main_clone:
    title: Cloning main repository...
    stage: prepare
    type: git-clone
    repo: 'codefresh-contrib/react-sample-app'
    revision: master
    git: github
  MyUnitTests:
    title: Unit test
    stage: test
    image: node:11.0
    commands:
      - yarn install
      - yarn test
    environment:
      - CI=true
  MyReactBuild:
    title: Packaging application
    stage: build
    image: node:8.16
    commands:
      - yarn build

Notice that it is possible to use any version of node/yarn that exists in Dockerhub. In this case we use version 11 for running tests and version 8.6 for packaging the application. You can also use any private docker image that includes yarn and any other tools you need in your pipeline.

There is no need for any special caching directives as Codefresh automatically caches the current workdir with all its folders (such as node_modules).

For more details see a complete pipeline with Yarn.