How does caching work with `actions/cache`?
IntermediateConceptGitHub Actions
Answer
actions/cache saves a directory under a key and restores it in later runs. If no exact key matches, it tries restore-keys prefixes (partial hit). Caches are scoped by branch: a PR can read the base branch's cache but not siblings'. The repository has a total cache limit (10 GB by default) and entries unused for 7 days are evicted.
- uses: actions/cache@v4
with:
path: ~/.m2/repository
key: maven-${{ runner.os }}-${{ hashFiles('**/pom.xml') }}
restore-keys: |
maven-${{ runner.os }}-
Many setup-* actions (setup-node with cache: npm, setup-java with cache: maven) do this for you.
More on GitHub Actions
What are workflows, jobs, steps and actions in GitHub Actions?BeginnerWhat events can trigger a workflow?BeginnerWhat is the difference between `pull_request` and `pull_request_target`?AdvancedWhat is a runner, and how do GitHub-hosted and self-hosted runners differ?BeginnerHow do you pass data between steps and between jobs?IntermediateHow does a build matrix work?Intermediate