1 min read
GitHub Actions Caching Strategies
Effective caching in GitHub Actions dramatically reduces build times and costs. This post covers caching strategies for various package managers and build systems.
Caching Fundamentals
name: Build with Caching
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# NPM caching
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
# Manual cache for custom paths
- uses: actions/cache@v3
with:
path: |
~/.cache/custom
.build-cache
key: ${{ runner.os }}-custom-${{ hashFiles('**/config.json') }}
restore-keys: |
${{ runner.os }}-custom-
# Gradle caching
- uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
# Docker layer caching
- uses: docker/setup-buildx-action@v2
- uses: docker/build-push-action@v4
with:
cache-from: type=gha
cache-to: type=gha,mode=max
- run: npm ci
- run: npm run build
Language-Specific Caching
# Python with pip
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
# Ruby with Bundler
- uses: actions/cache@v3
with:
path: vendor/bundle
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
# Go modules
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
Proper caching can reduce build times by 50-80% for dependency-heavy projects.