我正在使用 GitHub cache 操作,但我注意到如果作业失败,将不会创建缓存。来自docs:
如果作业完成成功,该操作将创建一个包含路径目录内容的新缓存。
我的工作流程 YAML 文件的精简版:
name: Build
on: [push]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Setup Node.js
uses: actions/setup-node@master
with:
node-version: '10.x'
- name: Get yarn cache path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore yarn cache
uses: actions/cache@v1
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install yarn dependencies
run: yarn install
- name: Build
run: yarn build
我注意到如果我的
Build
步骤失败, cache
后步骤将被不必要地跳过,这会导致安装的依赖项不被缓存。这需要后续运行再次下载依赖项,从而减慢作业速度。
有没有办法始终缓存依赖项,即使构建步骤失败?
在action正式版中,不可以在构建失败时缓存依赖。请参阅缓存操作清单中的这一行:
runs:
using: 'node12'
main: 'dist/restore/index.js'
post: 'dist/save/index.js'
post-if: 'success()'
如果作业成功,它只会运行后步骤。我不知道这样做的最初原因,但围绕这个想法有一些问题。在 this issue 中,用户 forked the action 将
post-if
更改为 always()
,假设您愿意执行非官方操作,这可能就是您想要的。
您现在可以使用
actions/cache/save
操作在填充缓存后立即保存缓存,并且在任何测试(或任何可能使作业失败的操作)运行之前。这就是问题 actions/cache#92 最终得到解决的方式。
steps:
- uses: actions/checkout@v3
.
. // restore if need be
.
- name: Build
run: /build.sh
- uses: actions/cache/save@v3
if: always() // or any other condition to invoke the save action
with:
path: path/to/dependencies
key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
https://github.com/actions/cache/tree/main/save#always-save-cache