Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 36 ยท GitHub Actions

Caching, Artifacts, Reusable Workflows, Environments

Speed up and organize GitHub Actions: dependency caching with keys, artifacts between jobs, reusable workflows, composite actions and deployment environments with approvals.

Once your workflows work, two problems appear: they are slow, and they are repetitive. This lesson covers the tools that solve both. Caches and artifacts handle files between runs and jobs; reusable workflows and composite actions remove copy-and-paste; environments add approval gates around deployments.

Caching dependencies

Every job starts on a clean machine, so npm ci downloads the same packages again and again. A cache stores a folder after one run and restores it in later runs. The key idea is the cache key: if a cache with that exact key exists, it is restored; if not, the job runs normally and saves a new cache at the end.

YAML
- uses: actions/cache@v4
  with:
    path: ~/.npm
    key: npm-${{ runner.os }}-${{ hashFiles('**/package-lock.json') }}
    restore-keys: |
      npm-${{ runner.os }}-

The key includes the operating system and a hash of the lock file. When the lock file changes, the hash changes, so you get a fresh cache. restore-keys are fallback prefixes: if the exact key is missing, GitHub restores the most recent cache whose key starts with a prefix, which gives you a partial head start (npm then downloads only what changed).

Most setup actions can do this for you: actions/setup-node@v4 with cache: npm, actions/setup-python@v5 with cache: pip, and similar options exist for Java, Go and Ruby.

Facts worth knowing:

  • Caches are scoped by branch: a run can read caches from its own branch, its base branch and the default branch, but not from sibling branches.
  • A key can never be overwritten. To refresh a cache, change the key.
  • Each repository has a total cache limit (10 GB by default); old entries unused for a week are removed, and the oldest are evicted when the limit is hit.
  • A cache is a performance tool. Your build must still work when the cache is empty.

The stand-in simulates the lookup rule (exact key first, then the newest prefix match). It is our model for learning, not GitHub's storage logic.

JS Stand-in: cache key lookup (not GitHub's service)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Artifacts: files between jobs and for humans

Caches speed things up; artifacts keep results. Use them to hand build output from one job to another, or to attach test reports, screenshots and binaries to a run so people can download them.

YAML
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build
      - uses: actions/upload-artifact@v4
        with:
          name: site
          path: dist/
          retention-days: 7

  deploy:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          name: site
          path: dist
      - run: ls dist

In upload-artifact v4 an artifact name must be unique within a run, and artifacts are downloadable from the run summary page. They count against storage and expire after the retention period (90 days by default, configurable).

Cache Artifact
Purpose Speed up repeated downloads Preserve and share results
Shared across runs Yes, by key Not meant to be (download manually)
Shared across jobs of one run Possible but not its purpose Yes, designed for it
Must your build work without it Yes n/a

Reusable workflows

If ten repositories all run the same test pipeline, copy-paste means ten places to fix. A reusable workflow is a normal workflow with the trigger workflow_call; other workflows call it as a job.

YAML
# .github/workflows/node-ci.yml  (the reusable one)
on:
  workflow_call:
    inputs:
      node-version:
        type: string
        default: "22"
    secrets:
      npm-token:
        required: false

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ inputs.node-version }}
      - run: npm ci && npm test
YAML
# .github/workflows/main.yml  (the caller)
jobs:
  call-ci:
    uses: ./.github/workflows/node-ci.yml
    with:
      node-version: "24"
    secrets: inherit

For a workflow in another repository, write uses: my-org/shared-workflows/.github/workflows/node-ci.yml@v1, and pin the @ref to a tag or SHA. The called workflow can expose outputs, and the caller can pass secrets: inherit or name each secret. Limits: reusable workflows can be nested a few levels deep, and a job that calls one cannot also define steps.

Composite actions

A composite action bundles several steps into one reusable step, defined in an action.yml file. Use it for repeated setup sequences inside jobs, while reusable workflows replace whole jobs.

YAML
# .github/actions/setup-project/action.yml
name: Setup project
description: Install Node and dependencies
inputs:
  node-version:
    default: "22"
runs:
  using: composite
  steps:
    - uses: actions/setup-node@v4
      with:
        node-version: ${{ inputs.node-version }}
        cache: npm
    - run: npm ci
      shell: bash

Call it with - uses: ./.github/actions/setup-project. Note that every run step in a composite action needs an explicit shell.

Environments and approvals

An environment (Settings -> Environments) represents a deployment target such as staging or production. A job that sets environment: production gains:

  • Required reviewers: the job pauses until a named person or team approves.
  • Wait timers and branch restrictions: only certain branches may deploy there.
  • Environment secrets and variables: production can have a different API_URL than staging.
  • A deployment history shown on the repository page.
YAML
jobs:
  deploy-prod:
    needs: deploy-staging
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://example.com
    steps:
      - run: ./deploy.sh
        env:
          API_KEY: ${{ secrets.API_KEY }}
flowchart LR
  B["build"] --> S["deploy to staging"]
  S --> A{"Approval needed?"}
  A -->|"approved"| P["deploy to production"]
  A -->|"rejected"| X["stop"]

An environment with required reviewers pauses the job until someone approves.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does restore-keys do in actions/cache?

    If the exact key misses, the newest cache whose key starts with a prefix is restored.
  2. 2. You want a built site folder to reach a deploy job. What should you use?

    Artifacts are designed to carry results between jobs of one run.
  3. 3. How is a reusable workflow made callable?

    workflow_call lets another workflow run it with uses: at the job level.
  4. 4. Which feature makes a deployment job wait for a human to approve?

    Required reviewers on an environment pause jobs that target it.

Summary

  • Cache dependencies with a key built from the OS and a lock-file hash; add restore-keys for partial hits.
  • Use artifacts to move or keep build results; use caches only as an accelerator.
  • Reusable workflows share whole pipelines (workflow_call); composite actions share step sequences.
  • Environments add secrets, branch rules and human approval to deployments.

Next: Dependabot and Dependency Security