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.
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.
GitHub Action for Yarn is not certified by GitHub.
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.
Starting from Yarn v2, Yarn will by default configure the cache to be local to your project.
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.
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.
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).
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.
- 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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With