我正在使用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
但是构建时间仍然相同。
答案 0 :(得分:18)
- 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-
上面的缓存代码仅缓存和恢复纱线缓存目录,而不缓存node_modules
目录。因此,如果您使用此代码(@Edric的答案),
- name: Install project dependencies
if: steps.yarn-cache.outputs.cache-hit != 'true' # Over here!
run: yarn
node_modules
未创建,您将收到未找到依赖项错误。
相反,您可以使用此:
- name: Install project dependencies
run: yarn --prefer-offline
这告诉yarn
始终运行,但尽可能使用缓存的下载内容(在上述缓存目录中),而不是从服务器下载。
您还可以直接缓存node_modules
目录,并在可用缓存时跳过安装步骤。实际上不建议这样做(请参阅评论)。示例:
- 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'
答案 1 :(得分:7)
如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
P.S。您可能应该使用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
file。
编辑:事实证明,纱(Yarn)包含在software installed on the GitHub-hosted Ubuntu 18.04.4 LTS (ubuntu-latest
/ubuntu-18.04
) runner的列表中,因此无需包括全局安装纱的步骤。
答案 2 :(得分:1)
我不知道为什么其他答案没有提到使用内置的 actions/setup-node@v2 方式缓存 npm 和 yarn 依赖项的简单方法,所以我将只添加文档,这要简单得多。
正如 readme 的 github package 所说:
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '14'
cache: 'npm' # or yarn
- run: npm install
- run: npm test
答案 3 :(得分:-1)
这是专门用于纱线的1线缓存: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