CircleCI での Yarn (npm の代替) の使用

Yarn は、JavaScript 用のオープンソース パッケージ マネージャーです。 Yarn によってインストールされるパッケージはキャッシュできるため、 ビルドを高速化できるだけでなく、さらに重要なメリットとして、ネットワーク接続に関するエラーも低減できます。

CircleCI での Yarn の使用方法

Yarn might already be installed in your build environment if you are using the docker executor. With Pre-built CircleCI Docker Images, the NodeJS image (circleci/node) already has Yarn preinstalled. If you are using one of the other language images such as circleci/python or circleci/ruby, there are two image variants that will include Yarn as well as NodeJS. These would be the -node and -node-browsers image variants. For example, using the Docker image circleci/python:3-node will give you a Python build environment with Yarn and NodeJS installed.

If you’re using your own Docker image base or the macos, windows or machine executors, you can install Yarn by following the official instructions from Yarn Docs. The Yarn Docs provide several installation methods depending on what machine executor you might be using. For example, you can install on any unix-like environment using the following curl command.

curl -o- -L https://yarnpkg.com/install.sh | bash

キャッシュ

Yarn packages can be cached to improve CI build times.

An example for Yarn 2:

#...
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          name: Install Dependencies
          command: yarn install --immutable
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn
#...

An example for Yarn 1.x:

#...
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          name: Install Dependencies
          command: yarn install --frozen-lockfile --cache-folder ~/.cache/yarn
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn
#...

See also

Caching Dependencies



Help make this document better

This guide, as well as the rest of our docs, are open-source and available on GitHub. We welcome your contributions.