🙆

github actionsのactions/cacheのはじめの一歩 | ハンズオンあり

に公開

actions/cacheとは

actions/cache は GitHub Actions のキャッシュストレージ(=GitHub が提供する専用のキャッシュ領域)使って、キャッシュデータを保存・復元します。

actions/cacheの基本構文

path: キャッシュしたいディレクトリやファイル
key: キャッシュの一意なキー(ハッシュを使うことが多い)
restore-keys: ヒットしなかったときに試すフォールバックキー(プレフィックス一致)

- name: Cache node modules
  uses: actions/cache@v4
  with:
    path: ~/.npm
    key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      ${{ runner.os }}-npm-

actions/cacheを簡単に使って動きを確認

  1. GitHub Actionsのキャッシュストレージにファイルを保存
  2. GitHub Actionsのキャッシュストレージからファイルを取得し、確認
    という2ステップで、actions/cacheの動きのイメージをつけてみます。

1. GitHub Actionsのキャッシュストレージにファイルを保存

  1. name: Create sample file
    sample fileを、runnerの/tmp/cache-test配下にhello.txtを作成

  2. name: Restore cache
    actions/cacheを利用して、key:test-cache-keyにて、/tmp/cache-testをcache(tmp/cache-test/hello.txtをcache)

  3. name: Show contents after restore
    tmp/cache-test/hello.txtを確認(1. - name: Create sample fileで作成したので当然ある。)

jobs:
  frontend_deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      - name: Create sample file
        run: |
          mkdir -p /tmp/cache-test
          echo "Hello from cache!" > /tmp/cache-test/hello.txt
      - name: Restore cache
        uses: actions/cache@v4
        with:
          path: /tmp/cache-test
          key: test-cache-key
          restore-keys: |
            test-cache-
      - name: Show contents after restore
        run: |
          echo "Listing cache directory:"
          ls -la /tmp/cache-test || echo "Cache directory not found"
          echo "File content:"
          cat /tmp/cache-test/hello.txt || echo "File not found"

actionsのキャッシュストレージに、test-cache-keyという名前で追加されます。

2. GitHub Actionsのキャッシュストレージからファイルを取得し、確認

続いて、Create sample fileのstepを削除します。

  1. name: Show contents before restore
    /tmp/cache-test/hello.txtが存在するかを確認
    -->> 存在しません。 runnerはworkflowが実行されるごとに新しくなるので。

  2. name: Restore cache
    key:test-cache-keyでcacheを復元します。

Cache restored successfully
Cache restored from key: test-cache-key
  1. name: Show contents after restore
    /tmp/cache-test/hello.txtが存在するか確認
    -->> 存在します。2. name: Restore cacheの部分でcacheが復元されたためです。
drwxr-xr-x  2 runner docker  4096 May 22 09:33 .
drwxrwxrwt 14 root   root   12288 May 22 09:54 ..
-rw-r--r--  1 runner docker    18 May 22 09:33 hello.txt
jobs:
  frontend_deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
      # - name: Create sample file
      #   run: |
      #     mkdir -p /tmp/cache-test
      #     echo "Hello from cache!" > /tmp/cache-test/hello.txt

      - name: Show contents before restore
        run: |
          echo "Listing cache directory:"
          ls -la /tmp/cache-test || echo "Cache directory not found"
          echo "File content:"
          cat /tmp/cache-test/hello.txt || echo "File not found"

      - name: Restore cache
        uses: actions/cache@v4
        with:
          path: /tmp/cache-test
          key: test-cache-key
          restore-keys: |
            test-cache-

      - name: Show contents after restore
        run: |
          echo "Listing cache directory:"
          ls -la /tmp/cache-test || echo "Cache directory not found"
          echo "File content:"
          cat /tmp/cache-test/hello.txt || echo "File not found"

Discussion