How do you cache dependencies in GitHub Actions?
⚡
Quick Answer
Use actions/cache with a key derived from a lockfile hash, or the built-in cache option of setup-* actions.
Detailed Answer
actions/cache restores a directory keyed on hashFiles('lockfile') and saves it after the run, skipping re-downloads when the lockfile is unchanged. Many setup actions (setup-node cache: npm) wrap this. Good cache keys are specific enough to be correct but stable enough to hit often.
Code Example
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ hashFiles('requirements.txt') }}💡
Interview Tip
Explain a good key balances hit-rate vs correctness.
github-actionscacheperformance