Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

510
Visualizações
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.

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

I'm not sure why other answer doesn't mention the simple way of caching npm and yarn dependencies with the built-in way of actions/setup-node@v2, so I'm going to just add the docs, which is much more simple.

As the readme of the github package says:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
  with:
    node-version: '14'
    cache: 'npm' # or yarn
- run: npm install
- run: npm test
over 4 years ago · Santiago Trujillo Relatório

0

This is a 1-liner cache specifically for Yarn: https://github.com/c-hive/gha-yarn-cache

- uses: actions/checkout@v2
- name: Setup Node.js
  uses: actions/setup-node@v1
  with:
    node-version: 12.x

- uses: c-hive/gha-yarn-cache@v1

- name: Install JS dependencies
  run: yarn install

- name: Test
  run: yarn test

It does caching as recommended by GitHub. Supports Yarn v1 and v2.

over 4 years ago · Santiago Trujillo Relatório

0

     - 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'
over 4 years ago · Santiago Trujillo Relatório

0

As mentioned in the comment next to the id field for the caching step:

Use this to check for cache-hit (steps.yarn-cache.outputs.cache-hit != 'true')

You're missing a conditional if property that determines whether the step should be run:

- name: Install yarn
  run: npm install -g yarn

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

P.S. You should probably use the Setup NodeJS GitHub Action that additionally sets up Yarn for you:

- uses: actions/setup-node@v1
  with:
    node-version: '10.x' # The version spec of the version to use.

See the action.yml file for a full list of valid inputs.


EDIT: As it turns out, Yarn is included in the list of software installed on the GitHub-hosted Ubuntu 18.04.4 LTS (ubuntu-latest/ubuntu-18.04) runner, so there's no need to include a step to globally install Yarn.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda