Skip to content

Lesson 2 of 12 · 20m

Anatomy of a Security-Aware Pipeline

Five stages, five latency budgets, and the rule that decides where each control lives: fast and deterministic early, slow and probabilistic late.

A CI pipeline has five distinct stages, and a control dropped into the wrong one is either useless or hated. The wrong mental model is "add a security job at the end of the build" — as if security were a step. It's not a step; it's a set of controls distributed across the lifecycle, each spending a different latency budget. Map the stages before you install anything.

Pipeline control map

The five stages

  1. Pre-commit — seconds. Formatting, linting, secrets detection (gitleaks protect --staged). Only controls that are fast and near-zero false positives earn a spot here; a hook that takes forty seconds or cries wolf trains developers to type --no-verify, and once that muscle memory exists every hook is dead.
  2. Pull request — minutes. Incremental SAST on the diff (semgrep ci, driven by SEMGREP_APP_TOKEN), dependency review on changed lockfiles (actions/dependency-review-action), unit-level security tests. This is where blocking is cheapest: the change is open and the author is right there.
  3. Post-merge / scheduled — unbounded. Full-repo SAST, deep dependency scans, DAST against staging. The slow, thorough, occasionally noisy work lives here and reports to a triage queue — not to a red X on somebody's merge.
  4. Build / publish. Container image scanning, SBOM generation, artifact signing. These operate on the artifact you're about to ship, not the source — a distinction that matters when the question is "does this image contain CVE-2024-3094."
  5. Runtime. Rescanning deployed images as new CVEs drop. Code doesn't change after deploy, but the vulnerability database does: last Tuesday's clean image has three new CVEs today.

A realistic PR stage

on:
  pull_request:
  schedule:
    - cron: '17 4 * * *'   # nightly full pass, reports to the queue

jobs:
  secrets:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }        # scanners need history, not a merge snapshot
      - run: gitleaks detect --source . --redact --exit-code 1

  sast:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }   # diff-aware needs the merge base, not a snapshot
      - run: semgrep ci --sarif --output=semgrep.sarif
        env: { SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} }

  deps:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/dependency-review-action@v4   # fails on new vulns in changed lockfiles

Two details carry the load. fetch-depth: 0 gives the secrets scanner full git history — the default depth of 1 shows it a snapshot, not the commits. And the cron trigger is what covers "new CVE in old code": the diff you merged in March doesn't change, but a CVE published in November still applies to it.

The ordering rule

Fast and deterministic first, slow and probabilistic later. A PR check that takes twenty minutes or flakes half the time will be bypassed within a sprint — I've watched it happen at three different companies, and the developers were right every time. A control that can't keep its stage's latency budget belongs in the next stage, tuned down to advisory, or deleted.

Two controls don't fit the map cleanly and end up homeless. License scanning is a legal control, not a security one — run it on a schedule, never in the dev loop. Fuzzing is real security work but measured in CPU-hours; it lives in a dedicated job, feeding crashes into the same triage queue as everything else.

Field notes

  • Run the same scanners in CI even where developers have pre-commit hooks. Hooks are opt-out; CI is not.
  • Every stage reports to the same findings queue — SARIF into code scanning is the free version of that.
  • If you can only afford one stage, make it the PR stage. It's the last point where fixing is still cheap.
  • Budget each stage's wall-clock time and publish it — "PR checks complete in under 6 minutes" is a promise the pipeline has to keep, or the bypass metrics will tell you it didn't.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.