GitHub Actions Advanced — DevOps

strategy.matrix runs one job for every combination of values (the cartesian product: every pair, with the first key as the outer loop). They all run in…

The workflow in the GitHub Actions Basics lesson had only one job. Once notes-api is deployed through Actions, you need five more features: matrix, cache, needs, environment and secrets. Picture our catering kitchen getting busier. One recipe is tasted on several stoves at once (matrix), and the base sauce is made once and kept in the fridge (cache). A dish may only be sent after every tasting passed (needs). Delivering to a big customer needs the head chef's signature (environment), and the delivery van's key is kept in a locked drawer (secrets).

The full notes-api workflow

This is the same .github/workflows/ci.yml as in the Lab: tests on several Node versions and OSes (operating systems), then a deploy to web-1.

name: CI/CD

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    strategy:
      matrix:
        node: [22, 24]
        os: [ubuntu-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: ${{ matrix.node }}
      - uses: actions/cache@v6
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('package-lock.json') }}
      - run: npm ci
      - run: npm test

  deploy:
    needs: test
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v7
      - run: ./scripts/deploy.sh ${{ github.sha }}
        env:
          DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}

${{ ... }} is an expression: GitHub replaces it with a value before the step runs. For example, ${{ matrix.node }} becomes 22, and ${{ github.sha }} becomes the code of the commit that triggered the workflow.

Matrix: one job, many combinations

Guess first: with node: [22, 24] and os: [ubuntu-latest, macos-latest], how many test jobs run, and one after another or at the same time?

strategy.matrix runs the same job for every combination of values. Two lists of 2 and 2 values give 2 × 2 = 4 jobs, and all four run at the same time (in parallel), each on its own runner.

# node os job name in the Actions tab
1 22 ubuntu-latest test (22, ubuntu-latest)
2 22 macos-latest test (22, macos-latest)
3 24 ubuntu-latest test (24, ubuntu-latest)
4 24 macos-latest test (24, macos-latest)

The order of the combinations: the first key (node) on the outside, the next key inside, just like two nested loops (a loop inside a loop). So node 22 is paired with both OSes first, then node 24. The set of all these pairs is called the cartesian product. In the exercise, the function you write builds this set. One key with one value, for example php: ["8.3"], gives one job. Adding a key c: [true] adds no jobs, but every combination gets c: true. Inside the job, the values are read with ${{ matrix.node }} and ${{ matrix.os }}.

Cache: don't download the same thing again

Every job starts on an empty runner, so npm ci downloads hundreds of packages (dependencies) every time. actions/cache keeps a folder from one run for the next runs:

Three runs in a row of the test (22, ubuntu-latest) job:

Run What happens actions/cache log npm ci
1 the first time Cache not found for input keys: Linux-node22-... then Cache saved with key: ... 38 seconds
2 another push, same lockfile Cache restored from key: Linux-node22-... 6 seconds
3 package-lock.json changed Cache not found for input keys: ... (a new hash) 38 seconds

Why ~/.npm and not node_modules? npm ci always deletes node_modules before installing the dependencies again, so a cached node_modules is thrown away. The shortcut: actions/setup-node with cache: npm does this caching for you, with no actions/cache step.

One more rule: a run in a pull request can use caches from main, but a cache made in a pull request only counts for that PR.

needs: deploy waits for test

Without needs, all jobs run at the same time. needs: test makes deploy wait for every matrix test job to pass. If even one fails, deploy is skipped. if: github.event_name == 'push' adds a condition: the job only runs when the event is a push. Because on only accepts pushes to main, a deploy only happens for a push to main, never for a pull request. Ordering lint, tests and E2E with needs is covered in more depth in the Testing in a CI/CD Pipeline lesson (Testing track).

Environment: a deploy needs approval

environment: production links the job to an environment called production. On GitHub, an environment is a deploy target that can have its own rules. Open Settings, then Environments, then production. Tick Required reviewers and choose who may approve. From then on, the deploy job is held with the status Waiting and the message:

Waiting for review: production needs approval to start deploying changes.

A reviewer (one of the people you chose) presses "Review deployments", then "Approve and deploy". Only then does the job run. This is Continuous Delivery from the What is CI/CD lesson, in GitHub Actions form.

Secrets only for trusted triggers

The SSH key for getting into web-1 is stored in Settings, then Secrets and variables, then Actions, under the name DEPLOY_SSH_KEY. The workflow reads it through the secrets context (a set of values GitHub provides): ${{ secrets.DEPLOY_SSH_KEY }}. In the workflow above, env: gives that value to the deploy step as an environment variable. Its value is masked as *** in the log. Runs triggered by a push to main, or by a PR from a branch in the same repo, get the secret. Runs from a PR from a fork (someone else's copy of the repo) get an empty string, because you haven't reviewed that code yet. Keeping secrets safe in more depth is in the CI/CD Pipeline Security lesson (Security track).

⚠️ Workflows that waste time, or put production at risk

TL;DR: strategy.matrix runs one job for every combination of values (the cartesian product: every pair, with the first key as the outer loop). They all run in parallel: node: [22, 24] × os: [ubuntu-latest, macos-latest] = 4 jobs. actions/cache with path: ~/.npm and a key containing hashFiles('package-lock.json') keeps npm's downloads between runs. A key exactly the same as a stored one (a hit) restores those downloads. If there is none (a miss), the folder is saved at the end of the job, if the job passed. A changed lockfile (package-lock.json) gives a new key. needs: test makes the deploy wait for every test job to pass. environment: production with Required reviewers makes the deploy wait for a human's approval. A secret is read with ${{ secrets.NAME }}, masked as *** in the log, and empty for a PR from a fork (someone else's copy of the repo).

⚡ Matrix: one job, many combinations

Write:

strategy:
  matrix:
    node: [22, 24]
    os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
Guess first: with this matrix, how many test jobs run, and one after another or at the same time?

Four (2 × 2), and all four run at the same time, each on its own runner.

Meaning: A matrix runs the same job for every combination of values, in parallel. The order is like two nested loops: the first key (node) in the outer loop, the next key in the inner loop. ${{ matrix.os }} is replaced with that combination's OS, so runs-on picks an Ubuntu or a macOS runner.

⚡ Cache: don't download again

Write:

- uses: actions/cache@v6
  with:
    path: ~/.npm
    key: ${{ runner.os }}-node${{ matrix.node }}-${{ hashFiles('package-lock.json') }}

Meaning: actions/cache keeps the folder ~/.npm (npm's downloads) between runs, under the name key. hashFiles('package-lock.json') is a fingerprint of the lockfile's contents (the exact version of every dependency). A key exactly the same as a stored one is a cache hit: the downloads are restored, and npm ci drops from 38 seconds to 6. Once the lockfile changes, its fingerprint changes, so the key is new.

⚡ needs: deploy waits for test

Write:

deploy:
  needs: test
  if: github.event_name == 'push'
  environment: production

Meaning: needs: test makes the deploy job wait for every test job of the matrix to pass. if: makes it run only for a push event (in this workflow, a push to main), not for a pull request. environment: production with Required reviewers (set in the repo's Settings) makes it wait for a human's approval first.

⚡ Secrets through secrets

Write:

env:
  DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}

Meaning: ${{ secrets.DEPLOY_SSH_KEY }} reads a secret (a private value, here the SSH key for web-1) stored in the repo's Settings, and env: gives it to the step. The value is masked as *** in the log. A run from a PR from a fork (someone else's copy of the repo) gets an empty string, because you haven't reviewed that code yet.

What you will learn