在 GitHub Actions 中保存作业失败时的缓存

Dun*_*Luk 7 continuous-integration github continuous-deployment github-actions

我正在使用 GitHub缓存操作,但我注意到如果作业失败,将创建无缓存。从文档

如果作业成功完成,该操作将使用路径目录的内容创建一个新缓存。

我的工作流程 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
Run Code Online (Sandbox Code Playgroud)

我注意到,如果我的Build步骤失败,cache后步骤将被不必要地跳过,这会导致已安装的依赖项不会被缓存。这需要后续运行以再次下载依赖项,从而减慢了作业速度。

有没有办法始终缓存依赖项,即使构建步骤失败?

use*_*610 22

现在,您可以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') }}
Run Code Online (Sandbox Code Playgroud)

https://github.com/actions/cache/tree/main/save#always-save-cache


Pat*_*irk 6

在官方版本的操作中,如果构建失败,则无法缓存依赖项。请参阅缓存操作清单中的这一行

runs:
  using: 'node12'
  main: 'dist/restore/index.js'
  post: 'dist/save/index.js'
  post-if: 'success()'
Run Code Online (Sandbox Code Playgroud)

如果作业成功,它只会运行后步骤。我不知道这样做的最初原因,但是围绕这个想法存在一些问题。在这个问题上,用户分叉的动作来改变post-ifalways(),这可能是你以后,假设你愿意运行一个非官方的行动。

  • 官方操作中是否有无法配置的原因? (11认同)