Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cache yarn packages in GitHub Actions

I am using GitHub Actions to build my TypeScript project. Everytime I run action I am waiting 3 minutes for all dependencies to get installed.

Is there way to cache yarn dependencies, so build time will be faster?

I tried this:

     - name: Get yarn cache directory path        id: yarn-cache-dir-path        run: echo "::set-output name=dir::$(yarn cache dir)"       - uses: actions/cache@v1        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)        with:          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}          restore-keys: |            ${{ runner.os }}-yarn-      - name: Install yarn       run: npm install -g yarn      - name: Install project dependencies       run: yarn 

but build time is still same.

like image 882
Baterka Avatar asked Apr 03 '20 10:04

Baterka


People also ask

How do you cache yarn?

when you run yarn cache dir from the command line, it will print out the path where yarn's global cache is currently stored. When you run the yarn cache clean [<module_name...>] command, it will clear the global cache. The global cache will then be repopulated when next you run yarn or yarn install.

Can I use yarn in GitHub Actions?

GitHub Action for Yarn is not certified by GitHub.

How does caching work in GitHub Actions?

The cache key uses contexts and expressions to generate a key that includes the runner's operating system and a SHA-256 hash of the package-lock. json file. When key matches an existing cache, it's called a cache hit, and the action restores the cached files to the path directory.

Does yarn cache locally?

Starting from Yarn v2, Yarn will by default configure the cache to be local to your project.

What should I do with the yarn cache?

Instead of caching the yarn cache, you should cache your node_modules. This caches all of your node_modules folders throughout your repository, and busts the cache every time a yarn.lock file changes.

How do I use yarn from the GitHub action for npm?

Using yarn from the GitHub Action for npm is delightfully simple. Here's what running npm install looks like: Pivoting to use yarn install instead of npm install is as simple as: You can see we've introduced the runs = "yarn" and after that the args are whatever you need them to be.

What can I do with the yarn command?

This Action for yarn enables arbitrary actions with the yarn command-line client, including testing packages and publishing to a registry. Please keep in mind that this Action was originally written for GitHub Actions beta (when Docker was the only way of doing things).

How do I cache dependencies in GitHub workflows?

To help speed up the time it takes to recreate these files, GitHub can cache dependencies you frequently use in workflows. To cache dependencies for a job, you'll need to use GitHub's cache action. The action retrieves a cache identified by a unique key. For more information, see actions/cache.


Video Answer


1 Answers

     - name: Get yarn cache directory path        id: yarn-cache-dir-path        run: echo "::set-output name=dir::$(yarn cache dir)"       - uses: actions/cache@v1        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)        with:          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}          restore-keys: |            ${{ runner.os }}-yarn- 

The caching code above only caches and restores the yarn cache directory, it doesn't cache the node_modules directory. So if you use this code (@Edric's answer),

- name: Install project dependencies   if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here!   run: yarn 

node_modules is not created and you will receive dependencies not found errors.

Instead, you can use this:

- name: Install project dependencies   run: yarn --prefer-offline 

This tells yarn to always run but use cached downloads (in the cache directory mentioned above) whenever possible instead of downloading from the server.


You can also cache the node_modules directory directly and skip the installation step when the cache is available. This is actually NOT recommended (see comments). Example:

    - name: Get yarn cache directory path       id: yarn-cache-dir-path       run: echo "::set-output name=dir::$(yarn cache dir)"     - name: Cache yarn cache       uses: actions/cache@v2       id: cache-yarn-cache       with:         path: ${{ steps.yarn-cache-dir-path.outputs.dir }}         key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}         restore-keys: |           ${{ runner.os }}-yarn-     - name: Cache node_modules       id: cache-node-modules       uses: actions/cache@v2       with:         path: node_modules         key: ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-${{ hashFiles('**/yarn.lock') }}         restore-keys: |           ${{ runner.os }}-${{ matrix.node-version }}-nodemodules-     - run: yarn       if: |         steps.cache-yarn-cache.outputs.cache-hit != 'true' ||         steps.cache-node-modules.outputs.cache-hit != 'true' 
like image 59
Quang Lam Avatar answered Sep 28 '22 01:09

Quang Lam