Aller au contenu principal

GitHub Actions Caching

Optimize CI/CD build times with caching strategies.

Dependency Caching

- uses: actions/setup-node@v4
with:
node-version: 20
cache: "yarn"

# Or manual caching
- uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-modules-

NX Build Cache

- uses: actions/cache@v4
with:
path: .nx/cache
key: ${{ runner.os }}-nx-${{ hashFiles('yarn.lock') }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-nx-${{ hashFiles('yarn.lock') }}-
${{ runner.os }}-nx-

Docker Layer Caching

- uses: docker/build-push-action@v5
with:
cache-from: type=gha
cache-to: type=gha,mode=max

Cache Strategy Table

AssetKey Based OnTTL
node_modulesyarn.lock hash7 days
NX cacheyarn.lock + SHA7 days
Docker layersDockerfile + depsAuto
Build artifactsSource hash1 day

Impact

Without CacheWith CacheSavings
~8 min install~30s restore~93%
~10 min build~2 min~80%
~5 min Docker~1 min~80%