Skip to content

13 - Using Caching

13 - Using Caching #9

Workflow file for this run

# See 13-caching/README.md for more information
# about how to use caching in workflows, best practices,
# and limitations of caching in GitHub Actions.
name: 13 - Using Caching
on:
workflow_dispatch:
inputs:
use-cache:
description: Whether to execute cache step
type: boolean
default: true
node-version:
description: Node version
type: choice
options:
- 18.x
- 20.x
- 21.x
default: 20.x
jobs:
install-deps:
runs-on: ubuntu-latest
defaults:
run:
working-directory: 13-caching/react-app
outputs:
deps-cache-key: ${{ steps.cache-key.outputs.CACHE_KEY }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: Calculate cache key
id: cache-key
run: |
echo "CACHE_KEY=deps-node-modules-${{ hashFiles('13-caching/react-app/package-lock.json') }}" >> "$GITHUB_OUTPUT"
- name: Download cached dependencies
uses: actions/cache@v3
if: ${{ inputs.use-cache }}
id: cache
with:
path: 13-caching/react-app/node_modules
key: ${{ steps.cache-key.outputs.CACHE_KEY }}
- name: Install dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci
lint-test:
runs-on: ubuntu-latest
needs: install-deps
defaults:
run:
working-directory: 13-caching/react-app
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: Download cached dependencies
uses: actions/cache@v3
id: cache
with:
path: 13-caching/react-app/node_modules
key: ${{ needs.install-deps.outputs.deps-cache-key }}
- name: Testing
run: npm run test
- name: Linting
run: echo "Linting..."
build:
runs-on: ubuntu-latest
needs: install-deps
defaults:
run:
working-directory: 13-caching/react-app
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}
- name: Download cached dependencies
uses: actions/cache@v3
id: cache
with:
path: 13-caching/react-app/node_modules
key: ${{ needs.install-deps.outputs.deps-cache-key }}
- name: Building
run: npm run build