GitHub Actions の actions/cache とは?
actions/cache はディレクトリをキーで実行間に保存・復元し、毎回依存関係をダウンロードし直さずに済むようにします。
実行のたびに依存関係をインストールするのは遅く、無駄です。actions/cache は選んだディレクトリを lockfile のハッシュなどをキーにして永続化し、次の実行で復元することで大幅に高速化します。
概要
公式のキャッシュ action です。cache する path と key を指定します。ヒット時にはパスを復元し、ミス時には job の終わりにそのキーの下にパスを保存します。
Caching npm
steps:
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: npm-仕組み
job の開始時に、action はキーの完全一致を探し、なければ restore-keys のプレフィックスにフォールバックします。何も一致しなければ job は通常どおり実行され、その後パスが新しい cache エントリとしてアップロードされます。
良いキーの選び方
キーは通常 lockfile をハッシュし、依存関係が変わったときだけ cache が無効になるようにします。restore-keys は部分的なフォールバックを提供し、小さな変更の後でも古い cache の大部分を活用できます。
なぜ重要か
cache は実行ごとに数分を削減でき、それが直接コスト削減につながります。多くの setup-* action には cache が組み込まれていますが、actions/cache はそれ以外のあらゆるものをカバーします。Latchkey のマネージド runner は、さらに高速な build のために独自のレイヤー cache を上乗せします。
関連する概念
cache は artifact とは異なります。cache は将来の実行を高速化し、artifact は現在の実行の output を共有します。
重要なポイント
actions/cacheはキーによってパスを復元・保存します。- キーには lockfile をハッシュし、フォールバックに
restore-keysを使います。 - cache は実行を高速化し、コストを下げます。
関連ガイド
What Is an Artifact in GitHub Actions?An artifact is a file or set of files a job uploads so they can be downloaded later or shared with other jobs…
What Is actions/checkout in GitHub Actions?actions/checkout is the official action that clones your repository onto the runner so subsequent steps can b…
What Is an Action in GitHub Actions?An action is a reusable, packaged unit of work you invoke from a step with uses. Actions can be JavaScript, D…
What Are GitHub Actions Minutes?GitHub Actions minutes are the billing unit for GitHub-hosted runners: wall-clock job time, multiplied by an…