MSBA-300 · Lesson 12 of 12 · 21 min

Case study: a continuous integration pipeline

▶ Video coming soon A video version of this lesson is on the way.

The last clinic was a project rather than a lesson. Priya gave Lena a brief that pulled together everything from the series and asked her to design, write and defend a new pipeline for TrailCheck, replacing the separate check and publish workflows that had grown up over the autumn. Dmitri would review the pull request as if it came from a stranger, Grace would approve the first production deployment, and Rosa would read the usage report a week later. This lesson follows Lena's work from the brief to the finished file and the review.

The brief

Lena's brief for the TrailCheck pipeline.
#RequirementFrom
1Every pull request to main gets lint, spelling and trail-report validation, and a preview of the built siteGrace, Dmitri
2The validator is tested on Node 22 and 24, with Node 26 as an allowed failureMalik
3Links are checked on the built site, not the sourceGrace
4Pushes to main deploy to GitHub Pages after approval by Grace or PriyaGrace
5Fork pull requests must work without secrets, and nothing contributed may run with write accessPriya
6Superseded pull request runs are canceled; production deployments never overlapDmitri
7Results are readable on the run page without opening logsOwen
8The pipeline should cost less than the two workflows it replacesRosa
9Photograph-only pull requests must still satisfy the required checkGrace

Designing before writing

Lena started on paper, as Priya had taught her. She listed the jobs, what each needed and produced, and which requirement each satisfied. Lint, spelling and the validator matrix needed only the source, so they could start together. The build needed the source and produced the site as an artifact. The link check needed the built site, so it needed the build. The preview deployment needed the build and ran only for pull requests; the production deployment needed the build and the link check and ran only for pushes to main. One small job at the end would gather the results and serve as the single required check.

A pipeline diagram. A pull request or push to main leads to two parallel jobs: lint and spelling, and the report validator with a matrix of Node 22 and 24. Both lead to build site and upload artifact. Build leads to link check on built site, and to deploy to Pages, main only, with an environment. A box below lists guard rails: contents read permission by default, pages write and id-token write only on deploy; concurrency that cancels superseded pull request runs but never cancels a deploy; caching keyed on the lock file, artifacts kept 14 days and timeouts on every job.
The design Lena drew before writing any YAML.
The job plan, mapped to the brief.
JobNeedsRuns whenProducesRequirements
changesNothingAlwaysOutput: whether site files changed9, 8
lintchangesSite files changedAnnotations1
validate (matrix)changesSite files changedReports as artifacts, summary1, 2, 7
buildlint, validateSite files changedsite artifact1, 3
linkcheckbuildSite files changedSummary3, 7
previewbuildPull requests from the same repositoryPreview link1, 5
deploybuild, linkcheckPushes to mainLive site4, 6
resultEverything aboveAlwaysOne pass or fail check9

The changes job was Lena's answer to requirement 9. Instead of path filters on the workflow, which would leave the required check missing for photograph-only pull requests, a first job decides whether any site files changed and exposes the answer as an output. The expensive jobs skip themselves when nothing relevant changed, and the result job always runs, so the required check always reports. Requirement 5 shaped the preview job: previews need a hosting token, fork pull requests do not get secrets, so the preview job runs only when the pull request comes from the TrailCheck repository itself, and fork contributors get the build artifact to download instead.

The workflow file

Lena wrote the file in one sitting and then went through it with Priya line by line. It is long, but every part of it comes from an earlier lesson.

.github/workflows/pipeline.yml, the finished TrailCheck pipeline.
name: TrailCheck pipeline
run-name: ${{ github.event_name }} on ${{ github.ref_name }} by @${{ github.actor }}

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

permissions:
  contents: read

concurrency:
  group: pipeline-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

env:
  NODE_LTS: "24"

jobs:
  changes:
    runs-on: ubuntu-latest
    timeout-minutes: 5
    outputs:
      site: ${{ steps.diff.outputs.site }}
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
      - id: diff
        env:
          BASE: ${{ github.event.pull_request.base.sha || github.event.before }}
        run: |
          if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ] || \
             git diff --name-only "$BASE" HEAD | grep -qvE '^(images/|docs/editorial/)'; then
            echo "site=true" >> "$GITHUB_OUTPUT"
          else
            echo "site=false" >> "$GITHUB_OUTPUT"
          fi

  lint:
    needs: changes
    if: needs.changes.outputs.site == 'true'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: ${{ env.NODE_LTS }}
          cache: npm
      - run: npm ci
      - run: npm run lint -- --format github
      - run: npm run spell

  validate:
    needs: changes
    if: needs.changes.outputs.site == 'true'
    name: Validator (Node ${{ matrix.node }})
    runs-on: ubuntu-latest
    timeout-minutes: 10
    continue-on-error: ${{ matrix.experimental == true }}
    strategy:
      fail-fast: false
      matrix:
        node: ["22", "24"]
        include:
          - node: "26"
            experimental: true
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: ${{ matrix.node }}
          cache: npm
      - run: npm ci
      - run: npm run validate -- --report reports/node${{ matrix.node }}.md
      - if: always()
        run: cat reports/node${{ matrix.node }}.md >> "$GITHUB_STEP_SUMMARY"

  build:
    needs: [lint, validate]
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v5
        with:
          node-version: ${{ env.NODE_LTS }}
          cache: npm
      - run: npm ci && npm run build
      - uses: actions/upload-pages-artifact@v4
        with:
          path: dist/
          retention-days: 14

  linkcheck:
    needs: build
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/download-artifact@v7
        with:
          name: github-pages
      - run: mkdir site && tar -xf artifact.tar -C site
      - run: npx --yes linkinator site --recurse --format markdown >> "$GITHUB_STEP_SUMMARY"

  preview:
    needs: build
    if: >-
      github.event_name == 'pull_request' &&
      github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    timeout-minutes: 10
    environment:
      name: staging
      url: https://preview.trailcheck.example/pr-${{ github.event.number }}
    steps:
      - uses: actions/download-artifact@v7
        with:
          name: github-pages
      - run: ./scripts/deploy-preview.sh artifact.tar
        env:
          HOST_TOKEN: ${{ secrets.HOST_TOKEN }}
          PR_NUMBER: ${{ github.event.number }}

  deploy:
    needs: [build, linkcheck]
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    concurrency:
      group: pages-production
      queue: max
    steps:
      - id: deployment
        uses: actions/deploy-pages@v4

  result:
    needs: [changes, lint, validate, build, linkcheck]
    if: always()
    runs-on: ubuntu-latest
    timeout-minutes: 2
    steps:
      - env:
          RESULTS: ${{ toJSON(needs.*.result) }}
        run: |
          echo "Job results: $RESULTS"
          if echo "$RESULTS" | grep -qE '"(failure|cancelled)"'; then exit 1; fi

Walking through the decisions

Triggers and concurrency. The workflow listens for pull requests to main, pushes to main and manual runs. One concurrency group per pull request, or per branch for pushes, cancels superseded runs, but only for pull requests: the cancel-in-progress expression is true only when the event is a pull request, so a push to main is never canceled by the next push. The deploy job adds its own group with queue set to max, so production deployments wait in line and none is skipped, which meets requirement 6.

Permissions. The workflow grants contents read and nothing else. Only the deploy job adds pages write and id-token write. Pull request runs from forks get a read-only token anyway, and no job that runs contributed code has any secret, because the only secret, the hosting token, is used in the preview job, which is restricted to pull requests from the repository itself and runs in the staging environment.

Skipping without breaking the required check. The changes job compares the pull request with its base, or a push with the previous commit, and reports whether anything outside the images and editorial folders changed. The lint, validate and build jobs read that output in their conditions. When they skip, everything that needs them skips too, and the result job, which always runs, finds no failures and passes. The result job is the only required check in the main branch ruleset, so renaming jobs or changing the matrix never breaks branch protection.

The matrix. The validator runs on two supported Node versions with fail-fast off, so both report, and an experimental Node 26 job that may fail without failing the run. Each job appends its report to the job summary, meeting Owen's requirement 7 alongside the link checker's Markdown output.

Artifacts and caching. setup-node caches npm downloads keyed on the lock file in every job, which brought the cost down. The build uploads the site once with upload-pages-artifact, kept 14 days rather than 90 to save storage; the link check, the preview and the deployment all use that same artifact, so what is checked is exactly what is published. Every job has a short timeout.

Testing the pipeline before trusting it

Lena did not merge the pipeline on the strength of one green run. She opened the pull request as a draft and pushed a series of deliberate test commits to it, each designed to exercise one path through the design. A commit that broke a link proved that the link check failed and that the result job failed with it. A commit that changed only a photograph proved that the expensive jobs skipped and the result job still passed. A commit with a misspelled word proved that spelling errors appeared as annotations on the right line. Malik opened a small pull request from his fork to prove that the pipeline ran without secrets and skipped the preview. Only the deployment path could not be tested in the pull request, because it runs only on main; for that, Lena relied on the production environment's approval as a final safety net, so that a mistake would stop at Grace rather than reach the public.

Each test commit was reverted after its run, and the pull request description listed the scenarios with links to the runs that demonstrated them. Dmitri said afterwards that the list of test runs was the most persuasive part of the pull request: it turned "I think this works" into evidence that a reviewer could click on.

What the pipeline deliberately leaves out

A good design also says what it does not do. The pipeline does not run code scanning, because Tidewater already runs CodeQL through GitHub's default setup for code scanning, which GitHub configures and runs without a workflow file in the repository. It does not publish the validator as a package; that belongs in Malik's repository and its release workflow. It does not notify anyone in chat, because the team agreed that pull request checks and the deployment approval request were enough notification. Writing these decisions down in the pull request prevented three later discussions about adding them "while we are at it".

The review

Dmitri reviewed the pull request carefully, as asked, and left seven comments. Three led to changes, and the discussion is a good model of what to look for in any workflow review.

Dmitri's review comments and Lena's responses.
CommentResponse
The actions are pinned to major tags. Should third-party actions be pinned to commit SHAs?Yes for anything outside the actions organization; Dependabot will propose updates. First-party actions stay on major tags by team policy.
The changes job fetches full history. Is that slow?TrailCheck's history is small; a fetch-depth of 0 takes two seconds. Revisit if it grows.
The result job treats a skipped job as a pass. Is that intended?Yes, that is requirement 9. Failures and cancellations fail it; skips do not.
What happens if someone runs the workflow manually on a feature branch?The deploy condition requires a push event on main, so a manual run builds and checks but cannot deploy. The production environment also limits deployments to main.
The run-name inserts the actor and branch. Is that safe?run-name is display text, not a script, so there is no injection risk; branch names never reach a run block directly.
Should the validator also run on Windows?Malik's matrix in his own repository covers operating systems; here only Node versions matter.
Add a link to the troubleshooting page in the README.Done, next to the status badge.

Priya pointed out that the review itself showed why the workflow was a file in the repository. Every decision was visible, questioned and answered in one place, and anyone who later wondered why the result job existed could find the reasoning in the pull request.

Reading the finished file as a newcomer

Priya's final test was to hand the file to Owen, who had never written a workflow, and ask him three questions: when does the site go live, what happens to a pull request that only changes photographs, and who can publish? He answered all three in under five minutes, using only the job names, the conditions and the comments. A pipeline that only its author can read is a liability; one that an editor can follow is part of the team's shared knowledge. Lena added two comments after watching where Owen paused, one above the changes job explaining the folders it ignores and one above the result job explaining why skipped jobs count as passes.

The first week

The first production deployment waited forty minutes for Grace, who was in a meeting, and went live when she approved it. The first fork pull request from Malik ran all the checks without secrets, skipped the preview and offered the built site as a downloadable artifact, exactly as designed. A photograph-only pull request from Owen showed the lint, validate and build jobs as skipped, the result job green, and was merged without anyone noticing anything unusual, which was the point.

Rosa's usage report a week later showed the pipeline using about 40 percent fewer runner minutes than the two old workflows together. Most of the saving came from caching and from skipping jobs on photograph-only changes, and a little from canceling superseded pull request runs. The one surprise was the experimental Node 26 job, which failed every run on a date-handling function, as Malik already knew; he fixed it the following week, and the job turned green.

Worked problem

Extending the pipeline

Six months later Tidewater wants the same pipeline in the guidebook repository, which has no validator but needs a PDF proof job, and in a new map-tiles repository. What should Lena do with pipeline.yml?

Show solution
  1. Most of the pipeline, changes detection, lint, build, link check and the result gate, is shared logic.
  2. Move the shared jobs into a reusable workflow in the organization's .github repository, with inputs for the folders to ignore and the build command.
  3. Each repository calls it and adds only its own jobs, such as the PDF proof or the tile validator, and its own deployment.
  4. Version the reusable workflow with tags so each repository chooses when to adopt changes.

Answer: Extract the common jobs into a tagged reusable workflow in the .github repository and keep repository-specific jobs in each caller.

Common mistakes

  • MistakeUsing workflow-level path filters on a workflow that provides a required check.

    InsteadDecide inside the workflow and keep an always-running result job as the required check.

  • MistakeGiving deployment permissions to the whole workflow.

    InsteadGrant pages and id-token write only to the deploy job.

  • MistakeChecking links on the source instead of the built site.

    InsteadBuild once, upload the artifact, and check and deploy that same artifact.

  • MistakeCanceling in-progress runs for every event.

    InsteadCancel superseded pull request runs, but queue deployments and leave main's runs alone.

Lena booked her GH-200 exam the day the pipeline merged. The companion courses in the program take the same skills further: building the custom actions that pipelines use, and managing and securing Actions across an organization.

Summary and key terms

  • Design the job graph on paper first: what each job needs, produces and which requirement it meets.
  • Use a changes job and an always-running result job to skip work without breaking required checks.
  • Keep permissions at contents read and add deployment scopes only to the deploy job.
  • Restrict secret-using jobs to trusted events and environments so fork pull requests work safely without secrets.
  • Build once, upload one artifact, and check, preview and deploy that same artifact.
  • Cancel superseded pull request runs, queue production deployments, cache dependencies and set timeouts.
Pipeline
A set of jobs that takes a change from checks through build to deployment.
Required check
A status check that must pass before a pull request can merge under a ruleset or branch protection.
Result job
A final job that always runs, gathers the results of other jobs and serves as the single required check.
Change detection
Deciding during a run whether relevant files changed, so that jobs can skip themselves.
Preview deployment
A temporary deployment of a pull request so reviewers can see the result.
Least privilege
Granting only the permissions each job needs.

Practice problems

Work each problem on paper first, then reveal the answer to check.

  1. Why does the pipeline use a result job instead of requiring each check?

    Show answer

    So skipped jobs and matrix changes never leave a required check missing; one job always reports.

  2. Why does the preview job check that the pull request comes from the same repository?

    Show answer

    Fork pull requests have no secrets, so the preview would fail; this also keeps the hosting token away from contributed code.

  3. How does the pipeline guarantee that what was checked is what was deployed?

    Show answer

    It builds once and uses the same uploaded artifact for the link check, the preview and the deployment.

Lesson quiz · 5 questions

Score 60% or more to complete this lesson.

Take the quiz