[GitHub Actions] actions/cache アクションの v4 がリリースされました

2024.01.23

こんにちは、CX 事業本部製造ビジネステクノロジー部の若槻です。

GitHub Actions で依存関係やビルド出力のキャッシュを保存を可能にするアクションである actions/cache の v4 がリリースされました。

現時点での actions/cache 最新バージョンは v4.0.0 となります。

V3 からの変更点

現時点での V3 からの変更点は以下の通りです。

アクションのデフォルトランタイムとして Node.js 20 が使われるようにする変更と、前段のステップが失敗した際にもキャッシュの保存を常に行うようにする save-always フラグ(デフォルトは無効)が変更が加えられています。

試してみた

アップデート前の動作

アップデート前のワークフローです。actions/cache のバージョンは v3 です。

.github/workflows/cicd.yml

on:
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
      - closed
    paths-ignore:
      - "**/*.md"
      - ".vscode/**"
      - "packages/e2e/**"
      - .github/dependabot.yml
      - .github/workflows/add-issue-to-project.yml

env:
  TARGET_ENV: ${{ github.base_ref == 'main' && 'prd' || github.base_ref == 'staging' && 'stg' || 'dev' }}

jobs:
  Integration:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node.js 20
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm

      - name: Cache Dependency
        uses: actions/cache@v3
        id: cache_dependency
        env:
          cache-name: cache-dependency
        with:
          path: "**/node_modules"
          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }}

      - name: Install Dependency
        if: ${{ steps.cache_dependency.outputs.cache-hit != 'true' }}
        run: npm ci --no-audit --progress=false --silent

      - name: Check Lint
        run: |
          npm run lint

      - name: Check Format
        run: |
          npm run format

      - name: Check Type
        run: npm run check

      - name: Cdk Snapshot Test
        run: npm run test-snapshot -- run

      - name: Unit Test
        run: npm run test-unit -- run

# 省略

ワークフローの実行履歴を確認すると、actions/cache@v3 で使われている Node.js 16 は昨年 11 月に EOL を迎えているため非推奨(Deprecated)であるという警告が表示され、Node.js 20 を使うように促されるようになっています。

Node.js 16 actions are deprecated. Please update the following actions to use Node.js 20: actions/cache@v3. For more information see: https://github.blog/changelog/2023-09-22-github-actions-transitioning-from-node-16-to-node-20/.

アップデート

次のようにワークフローを修正し、actions/cache を v4 にアップデートします。

$ git diff
diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml
index 7fccca1..7ea4d28 100644
--- a/.github/workflows/cicd.yml
+++ b/.github/workflows/cicd.yml
@@ -30,7 +30,7 @@ jobs:
           cache: npm
 
       - name: Cache Dependency
-        uses: actions/cache@v3
+        uses: actions/cache@v4
         id: cache_dependency
         env:
           cache-name: cache-dependency

アップデート後の動作

アップデート後のワークフローを実行すると、v4 の場合は警告が表示されなくなりました。

おわりに

GitHub Actions で actions/cache アクションの v4 がリリースされたのでご紹介しました。

GitHub は 2024年春までにすべてのアクションの Node.js 20 へのアップデートを計画しています。昨年 11 月には actions/checkout アクションの v4 リリースに伴う Node.js 20 のデフォルト化が行われていましたが、今回の actions/cache アップデートもそれに続く公式アクションの一連のアップデートの一つとなります。

ランタイムが強制アップデートされないように、早めにアップデート対応をするようにしましょう。

以上