CI/CD Automation Tutorial 0/46 lessons ~6 min read Lesson 4

    Pipeline Anatomy

    Pipeline anatomy is the decomposed delivery machine: what triggers work, which stages run in what order, what artifacts hand off between stages, which environments mutate, where…

    Course progress0%
    Focus
    20 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Pipeline anatomy is the decomposed delivery machine: what triggers work, which stages run in what order, what artifacts hand off between stages, which environments mutate, where gates fail or warn, and which observability hooks prove a release healthy. Tools implement anatomy; they don't replace it.

    The story

    An e-commerce team migrated from Jenkins to GitHub Actions and copied job names one-for-one — but lost the artifact promotion model. Staging rebuilt from source with npm run build --production while prod deployed the CI image; a minification difference caused a checkout bug only in prod. Tracing the pipeline anatomy exposed two build stages, no immutable handoff, and no post-deploy metric hook. Fixing anatomy (one image digest promoted, smoke + error-rate gate) mattered more than the platform migration.

    Understanding the topic

    Six structural parts every production pipeline must name explicitly:

    • Triggers: push, pull_request, tag, schedule, workflow_dispatch — each implies different secret scope and artifact retention.
    • Stages: ordered jobs with needs: or stages — lint, test, build, scan, deploy, notify; parallel where safe, serial at promotion boundaries.
    • Artifacts: immutable binaries (container digest, jar, terraform plan) stored in registry with retention policy — the contract between CI and CD.
    • Environments: staging, prod, preview/pr-123 — each maps to credentials, URLs, and approval rules.
    • Gates: hard fail (exit code 1), soft warn, manual approval, metric threshold — define per stage.
    • Observability hooks: post-deploy smoke, synthetic checks, canary analysis, deployment markers in APM — close the feedback loop.

    Internal architecture

    Reference anatomy for a containerized service:

    text
    Trigger: PR + push main
    Stage: verify (parallel)
    ├─ lint · unit · contract tests
    └─ SAST / secret scan
    Stage: build
    └─ artifact → registry@digest
    Stage: scan artifact (Trivy/Grype)
    Stage: deploy staging (env: staging)
    ├─ gate: smoke HTTP 200
    └─ hook: Datadog deployment event
    Stage: deploy prod (env: production)
    ├─ gate: approver OR canary SLI
    └─ hook: pager if error rate ↑ 5m

    Visual explanation

    Two diagrams show where Pipeline Anatomy lives in the delivery path and how teams implement it in production.

    Pipeline Anatomy — system view
    Git trigger
    PR · main · tag
    Verify stages
    Test · scan
    Artifact registry
    Immutable
    Deploy + observe
    Env · hooks
    Where this topic sits in the delivery path.
    Pipeline Anatomy — execution flow
    Fail fast on PR
    Cheap stages
    Build once
    Single digest
    Promote digest
    Not rebuild
    Metric feedback
    Rollback input
    Follow this loop when designing or reviewing pipelines.

    Step-by-step explanation

    1. Step 1 — List triggers: Document every event that starts a pipeline and whether untrusted forks may run it (PR from fork = no prod secrets).
    2. Step 2 — Draw stages left-to-right: Mark parallel fan-out (test shards) and serial bottlenecks (deploy prod). Assign owner team per stage.
    3. Step 3 — Define artifact contract: Format, tagging scheme (app:gitsha, digest pin), retention, and who may pull (CI role vs CD role).
    4. Step 4 — Map environments: URL, cluster, credential, data policy; ensure staging receives the same artifact prod will — never a fresh build.
    5. Step 5 — Wire gates and hooks: Each deploy stage gets pre-gate (tests passed upstream), post-gate (smoke), and observability hook (deployment annotation + SLI watch window).

    Production implementation

    GitLab CI — explicit stages, artifact report, environment tracking:

    • needs: encodes stage DAG — clearer than implicit stage order for complex pipelines.
    • environment: blocks give audit trail and URL per deploy — anatomy visible in GitLab UI and API.
    yaml
    stages: [verify, build, scan, deploy]
    variables:
    IMAGE: registry.acme.com/checkout:$CI_COMMIT_SHA
    verify:
    stage: verify
    script:
    - npm ci && npm test
    rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    - if: $CI_COMMIT_BRANCH == "main"
    build:
    stage: build
    script:
    - docker build -t $IMAGE .
    - docker push $IMAGE
    rules:
    - if: $CI_COMMIT_BRANCH == "main"
    scan:
    stage: scan
    script:
    - trivy image --exit-code 1 $IMAGE
    needs: [build]
    deploy_staging:
    stage: deploy
    environment:
    name: staging
    url: https://staging.checkout.acme.com
    script:
    - kubectl set image deploy/checkout app=$IMAGE
    - ./scripts/smoke.sh $CI_ENVIRONMENT_URL
    needs: [scan]
    deploy_prod:
    stage: deploy
    environment:
    name: production
    url: https://checkout.acme.com
    when: manual
    script:
    - kubectl set image deploy/checkout app=$IMAGE
    - ./scripts/watch-error-rate.sh --window 10m
    needs: [deploy_staging]

    Execution workflow

    1Pipeline anatomy review workflow
    1 / 5

    Sketch on whiteboard

    Triggers → stages → artifact → envs.

    No tool icons — just boxes and arrows.

    Real-world use

    Spotify's "golden path" templates encode anatomy so product squads don't reinvent stages. Google's CI (Bazel + TAP) separates fast presubmit from slower postsubmit — same anatomy, different trigger budgets. Netflix ties deployment markers to Atlas dashboards — observability hooks are how canaries decide promote vs rollback.

    Enterprise use cases

    Retail platform — 120 microservices, unified anatomy template: Platform team ships a golden .gitlab-ci.yml include: verify → build → scan → deploy. Service repos override only test command and health check URL. Central observability hook posts deployment events to Datadog with service, version, env tags — SLO dashboards auto-segment by deploy.

    • Trigger policy: PR runs verify only; main runs full anatomy; tags run verify+build+scan for hotfix lanes.
    • Artifact: single ECR per service; cross-account pull via OIDC — no long-lived keys on runners.
    • Gate: CVE critical = hard fail; high = warn with 7-day waiver ticket required.

    Production case study

    Logistics API — pipeline archaeology rescue:

    • Symptom: "Random prod bugs" — no correlation between CI green and prod behavior.
    • Discovery: Three build paths (local, CI, deploy script); no artifact registry; staging pointed at latest tag.
    • Fix: Single build stage, ECR digest promotion, staging smoke gate, Datadog deploy marker + 15m error-rate watch.
    • Result: Mean time to identify bad deploy dropped from 4h to 12m; change failure rate 15% → 6%.

    Trade-offs

    • Many small stages: clear failure attribution and cache boundaries — more YAML and longer wall clock if over-serialized.
    • Few fat stages: simpler mental model — harder to know which step failed and expensive reruns.
    • Hard gates everywhere: high confidence — flaky smoke blocks all releases; invest in quarantine and synthetic test stability.
    • Soft warn gates: keeps velocity — risk of warn fatigue where teams ignore yellow pipelines.

    Security implications

    Anatomy defines where secrets appear. Verify stages on fork PRs must use read-only tokens; deploy stages run only on trusted refs with OIDC to cloud roles.

    • Scan stage must run on the artifact destined for prod — scanning source on PR then building unscanned image on main is a common gap.
    • Environment-scoped secrets: staging credentials never available in prod jobs and vice versa — anatomy maps to blast radius.
    • Observability hooks often use API keys — inject via secret store, not env vars logged at job start.

    Scalability analysis

    Anatomy that works for 5 PRs/day breaks at 500 without path filters, merge queues, and remote cache. Artifact registry egress and storage grow with retention × services × commits.

    • Split anatomy: lightweight PR verify vs full main pipeline — same stage names, different job lists.
    • Remote build cache (Bazel, sccache, Docker layer cache) belongs in build stage anatomy — document invalidation rules.
    • Observability hook cardinality: tagging every preview env in prod dashboards noise-floods on-call — separate staging monitors.

    Staff engineer insights

    • Interview candidates who draw anatomy before YAML pass — ask them to mark the artifact box first.
    • When a pipeline "feels slow," profile stage wall clock — teams often parallelize test before optimizing deploy hooks.
    • Observability hooks are gates that run after deploy — treating them as optional is why canaries get skipped under pressure.
    • Platform teams sell templates that fix anatomy, not tools — golden paths reduce cognitive load more than runner upgrades.

    Best practices

    • Name stages after outcomes (verify, publish, deploy) not tools (jenkins, docker).
    • Pin artifact by digest in deploy stages — tags are human-readable aliases only.
    • Run expensive scans after build, before any deploy stage mutates an environment.
    • Log pipeline URL and artifact digest in deployment ticket / Slack — anatomy aids incident triage.

    Common mistakes

    • Deploy job clones repo and builds again — destroys reproducibility.
    • Smoke test hits wrong URL because environment block misconfigured — green pipeline, broken prod.
    • Parallel test without overall timeout — one hung shard blocks anatomy silently.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    5 questions
    1BeginnerQuestionName the six parts of pipeline anatomy and give an example of each.+

    Answer

    Triggers (PR opened), stages (test then build), artifacts (container digest in ECR), environments (staging cluster), gates (Trivy exit code 1 on critical CVE), observability hooks (post-deploy smoke + APM deployment marker).

    Follow-up

    Which part do teams most often omit?
    2IntermediateQuestionHow would you design anatomy for a monorepo with 10 services?+

    Answer

    Path-filtered triggers so only affected services run verify/build. Shared template with service-specific test cmd and image name. Single registry namespace per service. Deploy stages per service with independent gates — avoid one fat pipeline blocking all teams.

    Follow-up

    When would you use a merge queue?
    3AdvancedQuestionWhat's the difference between a gate and an observability hook?+

    Answer

    Gate fails the pipeline before or during promotion — test failure, scan CVE, manual approval. Observability hook runs after deploy, compares SLIs to threshold, may trigger rollback workflow — it's feedback, often async. Both should be documented in anatomy; conflating them hides rollback responsibility.

    Follow-up

    Should a failing canary fail the pipeline job or invoke a separate rollback workflow?
    4AdvancedQuestionDraw anatomy for hotfix via tag when main is broken.+

    Answer

    Trigger on tag matching hotfix pattern; skip unrelated verify if tag points to released commit; still run build+scan+deploy with same artifact rules. Separate environment gate for prod with expedited approver list. Observability hook with tighter error budget.

    Follow-up

    How prevent hotfix lane from bypassing security scan permanently?
    5IntermediateQuestionWhy is 'artifact' the most important box on the diagram?+

    Answer

    It's the contract between CI and CD — proves what was tested is what ships. Without immutable artifact, staging validates build A and prod runs build B. All gates upstream of deploy are meaningless if the handoff isn't the same bits.

    Follow-up

    How do terraform pipelines define artifact?

    Hands-on exercise

    Take an existing workflow YAML (yours or open source). Annotate each job with trigger scope, stage name, artifact produced/consumed, environment mutated, gate type, and observability hook (or mark MISSING). Propose one fix for the highest-risk gap.

    yaml
    # Annotation template (comments in your repo copy)
    # job: test
    # trigger: pull_request
    # stage: verify
    # artifact: none
    # gate: hard (exit 1)
    # observability: none

    Summary

    You can decompose any pipeline into six anatomical parts, spot missing artifact handoffs and observability hooks, and design templates that scale across services. Teach anatomy before syntax — it's how teams survive platform migrations without repeating prod-only bugs.

    Ready to mark this lesson complete?Track your journey across the entire course.