CI/CD Fundamentals
Design a pipeline that turns every change into a tested, versioned artifact: stages, gates, caching, promotion of a single artifact, pipeline as code, and safe handling of credentials.
Introduction
Continuous integration (CI) means every change is merged into the shared codebase frequently and verified automatically. Continuous delivery (CD) means every verified change is always in a state that could be released. Continuous deployment goes one step further and releases every passing change automatically.
In the quality course you defined pipeline stages for tests. This lesson widens the view to the whole path: build, verify, package, publish, and promote.
Learning Objectives
- Distinguish continuous integration, continuous delivery, and continuous deployment.
- Design pipeline stages with triggers, gates, and time budgets.
- Build once and promote a single immutable artifact.
- Write the pipeline as code, and keep it fast, deterministic, and maintainable.
- Handle credentials and permissions in the pipeline safely.
- Use AI to draft and review pipeline definitions.
1. The Three Terms
| Practice | Meaning | Human step |
|---|---|---|
| Continuous integration | Merge small changes often; build and test each automatically | Review before merge |
| Continuous delivery | Every change that passes is releasable at any time | A person chooses when to release |
| Continuous deployment | Every change that passes goes to production automatically | None, after merge |
Which one you choose depends on risk, regulation, and maturity. For SignalDesk’s pilot, a sensible target is continuous delivery: automatic deployment to a staging environment, and a deliberate, one-click promotion to production. Continuous deployment can follow once rollback and monitoring are proven.
2. Pipeline Stages
Stages mirror the quality strategy: fast checks first, slow checks later, and expensive checks only when earlier ones pass.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
On every push / pull request
1. Validate lint, format, type check, docs checks ~1-2 min
2. Build compile, resolve dependencies ~2 min
3. Unit tests fast, isolated ~3 min
4. Integration real database, contract tests ~8 min
On merge to main
5. Package build container image, tag with commit and version
6. Scan dependency and image scans, secret scan
7. Publish push image and attach metadata to the registry
8. Deploy to staging
9. Acceptance end-to-end smoke tests, migration check ~10 min
On release
10. Approve human decision, with the readiness checklist
11. Deploy to production, in the chosen strategy (lesson 6)
12. Verify health checks and smoke tests against production
Time budgets are illustrative; set your own and enforce them. Feedback to the author on a pull request should arrive within about ten minutes; beyond that, developers switch tasks and fixes get slower.
Design rules:
- Fail fast. Order stages so that the cheapest, most likely failures come first.
- Parallelize independent jobs (lint, unit tests, documentation checks).
- Gates map to exit criteria. A stage that blocks promotion must correspond to a criterion in the quality strategy, so that nobody wonders why it exists.
- Every stage produces evidence: reports, logs, and test results are stored and linked to the commit.
3. Build Once, Promote the Artifact
The most important rule in delivery: the artifact that passed testing is the artifact that is deployed.
1
2
3
4
commit abc1234 -> build -> image signaldesk-api:abc1234 (digest sha256:...)
|
staging <--- same image, staging configuration
production <- same image, production configuration
- Build one immutable artifact per commit (a container image, a package). Never rebuild for production.
- Identify it by a content digest or an exact version, not by a mutable tag like
latest. - Differences between environments come from configuration supplied at deploy time (lesson 5), not from different builds.
- Record provenance: which commit, which pipeline run, which dependencies. This supports rollback, audit, and the security course.
4. Pipeline as Code
Define the pipeline in version-controlled files next to the code, so that changes are reviewed and history is kept. Syntax differs by tool; the structure is similar. A tool-neutral illustration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
pipeline:
triggers:
pull_request: [validate, build, unit, integration]
main: [validate, build, unit, integration, package, scan,
publish, deploy_staging, acceptance]
release_tag: [approve, deploy_production, verify]
stages:
validate:
steps:
- run: make lint typecheck docs-check
unit:
needs: [build]
cache: [dependencies]
steps:
- run: make test-unit
integration:
needs: [build]
services: [postgres]
steps:
- run: make test-integration
package:
needs: [unit, integration]
steps:
- run: make image VERSION=$COMMIT_SHA
deploy_production:
needs: [approve]
environment: production
approval: required
steps:
- run: make deploy ENV=production IMAGE=$IMAGE_DIGEST
Practices:
- Pipeline steps call the same commands developers run (
make test-unit), so that a failure can be reproduced locally. Logic belongs in scripts under version control, not in pipeline-tool configuration. - Reuse shared steps and templates, but resist over-abstraction that nobody can read.
- Pin versions of tools, base images, and actions or plugins. An unpinned dependency in the pipeline can break a build or inject malicious code.
- Keep the pipeline definition reviewable: small files, clear names, comments for non-obvious choices.
5. Speed and Reliability
A slow, flaky pipeline is ignored or bypassed. Treat it as a product.
- Cache dependencies and build layers, keyed by lock file, so that unchanged inputs are not rebuilt.
- Run only what is affected where safely possible (for example, skip heavy suites for documentation-only changes), but always run the full set before release.
- Parallelize and shard long test suites.
- Use ephemeral, clean runners so that state does not leak between runs.
- Apply the flaky-test policy from the quality course: quarantine with a deadline, never rerun until green without recording the flake.
- Keep builds reproducible: pinned dependencies, lock files, and no network downloads of unpinned content during the build.
- Measure the pipeline: duration per stage, failure rate, queue time, and the proportion of failures caused by infrastructure rather than code.
6. Branching and Merging
CI works best with short-lived branches and frequent integration.
- Trunk-based development: small changes merged to the main branch daily, with incomplete features hidden behind flags.
- Short-lived feature branches: merged within a day or two through pull requests with required checks.
- Avoid long-lived release branches except where you must support several versions in parallel.
- Protect the main branch: required status checks, required review, no force pushes, linear history if your team prefers.
- Keep main green. A broken main blocks everyone; fixing or reverting it is the first priority.
7. Credentials and Permissions in the Pipeline
The pipeline can deploy to production, which makes it a high-value target. Treat it accordingly.
- Least privilege. Each job gets only the permissions it needs; a test job cannot deploy.
- Short-lived credentials issued per run (federated identity) are better than long-lived keys stored in the pipeline.
- Secrets come from a secrets manager or the pipeline’s protected store, are masked in logs, and are never printed.
- Separate environments: production credentials are available only to production jobs, ideally behind an approval.
- Do not run untrusted code with secrets, for example pull requests from forks.
- Protect the pipeline definition: changes to it require review, because modifying the pipeline can bypass every gate.
- Audit: keep logs of who approved and deployed what.
The security course expands on supply chain protection; here, establish the habits.
8. Using AI for Pipeline Work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Act as a DevOps engineer drafting a CI/CD pipeline.
Inputs:
- Repository tree, build tool, and test commands (Makefile): [paste]
- Quality strategy stages, triggers, and time budgets: [paste]
- Target CI system and its syntax (or "tool-neutral"): [paste]
- Deployment targets and approval rules: [paste]
Tasks:
1. Draft the pipeline definition with stages, triggers, dependencies,
caching, and artifacts.
2. Use only commands that exist in the Makefile. List any command
you needed but did not find.
3. Show which stages are gates and which exit criterion each maps to.
4. List the permissions and secrets each job needs, minimized.
5. List risks: steps that could deploy the wrong thing, skip a gate,
or expose a secret.
Pin all versions. Do not rebuild the artifact per environment.
Mark assumptions.
Review an existing pipeline.
1
2
3
4
5
6
7
8
Review this pipeline definition: [paste].
Identify: stages that can be skipped or bypassed; artifacts rebuilt
instead of promoted; unpinned versions; excessive permissions;
secrets that could be printed or exposed to untrusted code; missing
gates relative to this quality strategy [paste]; and opportunities
to speed it up without removing checks. Separate certain problems
from suspicions.
Run the drafted pipeline on a branch against a non-production target first. Read what each step does before trusting it, and check what it can reach.
9. Practical Exercise
For SignalDesk (or your project):
- Make sure a single
makecommand exists for each stage: lint, build, unit, integration, package, deploy. - Write the pipeline definition with the stages in section 2, matching your quality strategy’s triggers and time budgets.
- Configure the pipeline to build one artifact per commit, identify it by digest, and store it in a registry.
- Add caching and parallel jobs, and record the time before and after.
- Protect the main branch with required checks and review.
- Give each job least-privilege permissions, and move any long-lived credentials out of the repository and into the protected store.
- Run the AI drafting and review prompts, verify each suggestion, and apply the valid ones.
- Break the pipeline deliberately (failing test, lint error, missing secret) and confirm that it fails at the right stage with a clear message.
- Update the baseline metrics from lesson 1 with real pipeline data.
Verification
You are ready to continue when:
- Every change triggers the pipeline and results are visible within the time budget.
- Exactly one artifact is built per commit and promoted unchanged through environments.
- Every gate corresponds to an exit criterion in the quality strategy.
- Pipeline steps can be reproduced locally with the same commands.
- Tools and base images are pinned, and no long-lived production credentials are in the repository.
- Branch protection prevents merging a change that fails required checks.
- You have seen each stage fail for the right reason.
Key Takeaways
- CI verifies every change quickly; CD keeps every verified change releasable.
- Build once and promote the same artifact; vary only configuration.
- Order stages to fail fast, and map gates to exit criteria.
- Keep the pipeline as code, fast, reproducible, and reviewed like the software it ships.
- The pipeline can reach production, so give it least privilege and protect its definition.
What’s Next?
The pipeline produces an artifact. Next, we define what that artifact is: how to package SignalDesk into container images that are small, secure, reproducible, and identical in every environment.