TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Anatomy of a Pipeline

Anatomy of a Pipeline

Pipelines on every platform are built from the same parts. Learn them once, and GitHub Actions, GitLab CI, Jenkins and Azure Pipelines become different spellings of one idea.

Part What it is GitHub Actions GitLab CI
Trigger the event that starts a run on: push, pull_request, workflow_dispatch push, merge request, schedule, manual, API
Job a unit of work on one machine (runner/agent) jobs.<id> a top-level key with script:
Step one command or reusable action inside a job steps[*].run / uses lines of script:
Stage a group of jobs that run together (none: only needs) stages: + stage:
Dependency "B starts after A succeeded" needs: [a] stage order, or needs: [a]
Artifact files a job hands to later jobs or keeps as evidence actions/upload-artifact artifacts: paths:
Cache files reused between runs to save time (dependencies) actions/cache cache:
Gate a job whose failure stops the pipeline (tests, scans, approvals) a failing step, an environment approval a failing job, when: manual
Environment a deployment target with its own secrets and rules environment: environment:

The typical stages

 commit ──► lint ──► unit tests ──► build ──► scan ──► push ──► deploy staging ──► (approve) ──► deploy production
            fast      fast          once      gate     by digest                     gate

Three design rules hide in that line:

  1. Fail fast and cheap first. Linting takes seconds; an end-to-end test suite takes minutes. Put cheap checks first so most failures are reported before the expensive work starts.
  2. Build once, promote the same artifact. The image or binary built in the build job is the one that is scanned, pushed and deployed everywhere. Rebuilding per environment means production runs something that was never tested.
  3. Every gate is a job with an exit code. A scanner that prints warnings but exits 0 is a report, not a gate.

A model: how long does a run take?

Jobs that don't depend on each other run in parallel, so a pipeline's duration is its critical path: the longest chain of needs. This model computes it for a small pipeline and shows what moving one job changes:

python · try it yourself
from functools import lru_cache

def critical_path(jobs):
    """jobs: name -> (minutes, [needs]). Returns (total, path)."""
    @lru_cache(maxsize=None)
    def finish(name):
        minutes, needs = jobs[name]
        if not needs:
            return minutes, (name,)
        best = max((finish(n) for n in needs), key=lambda r: r[0])
        return best[0] + minutes, best[1] + (name,)
    return max((finish(j) for j in jobs), key=lambda r: r[0])

pipeline = {
    "lint":      (1, []),
    "unit":      (4, []),
    "build":     (3, ["lint", "unit"]),
    "e2e":       (9, ["build"]),
    "scan":      (2, ["build"]),
    "push":      (1, ["scan", "e2e"]),
    "deploy":    (2, ["push"]),
}
total, path = critical_path(pipeline)
print(f"stage-free graph: {total} min via {' -> '.join(path)}")
print(f"sum of all jobs:  {sum(m for m, _ in pipeline.values())} min (what one runner in sequence would take)")

# The e2e suite only needs the image to push after it; scan can start right away.
# What if e2e ran against a preview environment *after* push instead?
faster = dict(pipeline, push=(1, ["scan"]), e2e=(9, ["push"]), deploy=(2, ["e2e"]))
total2, path2 = critical_path(faster)
print(f"e2e after push:   {total2} min via {' -> '.join(path2)}")
stage-free graph: 19 min via unit -> build -> e2e -> push -> deploy
sum of all jobs:  22 min (what one runner in sequence would take)
e2e after push:   21 min via unit -> build -> scan -> push -> e2e -> deploy

The run takes 19 minutes, not 22, because lint, unit, scan and e2e overlap. Moving e2e behind push made things worse (21 minutes): the slow job now also waits for scan and push, so they joined the critical path. To speed a pipeline up, find the critical path first, then shorten a job on it (split the e2e suite into parallel shards, cache dependencies, skip unchanged components). Speeding up a job off the critical path changes nothing. Release Engineering's job-graph model expands matrices into jobs with the same idea.

Pipelines vs stages vs DAGs

Older systems ran stages strictly in order: every job in test waits for every job in lint. Modern pipelines are DAGs (directed acyclic graphs): each job lists exactly what it needs, and starts as soon as those are done. GitHub Actions has only needs; GitLab CI has both (stages by default, needs to opt into the DAG). Stages are easier to read; DAGs are faster. You'll use both in this course.

Test yourself

A pipeline's jobs total 22 minutes of work, but the run takes 19 minutes. What determines the 19?

  1. The slowest single job
  2. The critical path: the longest chain of dependent jobs
  3. The number of runners
  4. The number of stages times 5

Why should the image deployed to production be the one built in the pipeline's build job, not a rebuild?

  1. Rebuilding is slower
  2. A rebuild may differ from what was tested and scanned (new base image, new dependency)
  3. Registries reject rebuilds
  4. Rebuilds lose the tag

A scanner prints 12 critical findings but the job is green. What is wrong?

  1. Nothing
  2. The scanner is run as a report, not a gate: it must exit non-zero on findings above the threshold
  3. The scanner is too old
  4. Critical findings are always ignored