如何在 GitHub Actions 中缓存纱线包

问题描述 投票:0回答:4

我正在使用 GitHub Actions 构建我的 TypeScript 项目。 每次运行操作时,我都会等待 3 分钟以安装所有依赖项。

有没有办法缓存纱线依赖项,这样构建时间会更快?

我试过这个:

     - 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

但构建时间仍然相同。

typescript yarnpkg github-actions
4个回答
102
投票
  1. 使用

    actions/setup-node@v2
    或更新版本:

    - name: Set up Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '16'
        cache: 'yarn'
    
    - name: Install project dependencies
      run: yarn
    

    actions/setup-node@v2
    或更新版本内置了缓存,因此您不再需要设置
    actions/cache

  2. 使用

    actions/setup-node@v1
    并使用
    actions/cache
    缓存 Yarn 全局缓存:

    - name: Set up Node.js
      uses: actions/setup-node@v1
      with:
        node-version: '16'
    
    - name: Get yarn cache directory path
      id: yarn-cache-dir-path
      run: echo "::set-output name=dir::$(yarn cache dir)"
    
    - uses: actions/cache@v3
      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 project dependencies
      run: yarn --prefer-offline
    

上面的缓存代码仅缓存和恢复 Yarn 全局缓存目录,它不缓存

node_modules
目录本身。

为了提高安装速度,你需要告诉 Yarn 在安装过程中尽可能使用缓存下载(在上面提到的缓存目录中),而不是从服务器下载:

- name: Install project dependencies
  run: yarn --prefer-offline
  1. 使用

    node_modules
    缓存
    actions/cache
    推荐)

    您也可以直接缓存

    node_modules
    目录,当缓存可用时跳过运行
    yarn

    但不推荐,因为:

    • yarn
      擅长利用全局缓存。如果依赖项已在全局缓存中可用,
      yarn
      可以在不到 1 秒的时间内完成运行。 (参见@mvlabat评论)。
    • node_modules
      可能已损坏。每次重新运行
      yarn
      并让
      yarn
      决定是否从缓存中获取文件会更安全(因为
      yarn
      会在使用缓存之前尝试验证缓存)。
    - name: Set up Node.js
      uses: actions/setup-node@v1
      with:
        node-version: '16'
    
    - 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@v3
        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@v3
        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'
    

42
投票

正如 github 包readme 所说:

steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
  with:
    node-version: '14'
    cache: 'npm' # or yarn
- run: npm install
- run: npm test

编辑:

事实证明,文档的编写方式非常具有误导性,他们进行了更新以明确它不缓存

node_modules
文件夹,而仅缓存全局缓存目录,如此问题中所述。

也正如 Mrchief 在评论中所说:

...您仍然会遇到

npm i time
,只需节省从互联网下载的时间(如果模块位于 npm 缓存中)

因此,您仍然应该使用从互联网下载软件包来节省时间,但如果您想缓存

node_modules
文件夹,请检查它使用 actions/cache 的其他答案。

您还应该检查Quang Lam答案以及它关于为什么您不应该缓存

node_modules
文件夹的评论。


14
投票

如缓存步骤

id
字段旁边的评论中所述:

用它来检查是否有

cache-hit
(
steps.yarn-cache.outputs.cache-hit != 'true'
)

您缺少一个条件

if
属性来确定是否应运行该步骤:

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

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

附注您可能应该使用 Setup NodeJS GitHub Action 来另外设置 Yarn:

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

请参阅

action.yml
文件,了解有效输入的完整列表。


编辑:事实证明,Yarn 包含在 GitHub 托管的 Ubuntu 18.04.4 LTS (ubuntu-latest

/
ubuntu-18.04
) 运行器
上安装的
软件列表中,因此无需包含全局步骤安装纱线。


11
投票

actions/setup-node
支持缓存自v2以来,具有多个自定义选项

- uses: actions/checkout@v3

- name: Setup Node.js
  uses: actions/setup-node@v3
  with:
    node-version: '16'
    cache: 'yarn'

- name: Install JS dependencies
  run: yarn install

按照建议完成缓存,仅缓存yarn cache dir

而不缓存
node_modules
。不建议缓存 
node_modules
,因为它可能会导致问题,例如当节点版本更改时。


旧答案:

这是专门用于 Yarn 的 1-liner 缓存:https://github.com/c-hive/gha-yarn-cache

它按照 GitHub 的建议进行缓存。支持 Yarn v1 和 v2。

NPM 相同:https://github.com/c-hive/gha-npm-cache

© www.soinside.com 2019 - 2024. All rights reserved.