CI/CD Pipeline Best Practices (Part 1): Build a Pipeline You Can Trust
A practical guide to CI/CD foundations: fast feedback, repeatable builds, immutable artifacts, safe deployments, least privilege, rollback and a production-ready GitHub Actions example.

A CI/CD pipeline can be twenty lines of YAML or twenty thousand. Neither tells you whether it is any good.
The useful question is simpler: when a developer changes the code, how quickly can the team find out whether that change is safe to release — and how confidently can they put it into production?
That is what a good pipeline is for.
A surprising number of pipelines grow in the opposite direction. They begin with a build and a deploy step, accumulate scanners, approval gates, environment variables and copy-pasted actions, and eventually become a temperamental piece of infrastructure nobody wants to touch. The build takes 25 minutes, a flaky browser test fails every third run, production uses a subtly different build from staging, and the deployment credential has enough permission to rearrange the entire cloud account.
It technically qualifies as CI/CD. It is also exactly the sort of pipeline we want to avoid.
This first part focuses on the foundations: what a dependable CI/CD pipeline should actually guarantee, how to structure one, and the mistakes worth fixing before adding more tooling.
CI, continuous delivery and continuous deployment are not the same thing
The terms are often bundled together, but the distinction matters.
Continuous integration (CI) is the practice of integrating changes frequently and automatically checking that the combined code still works. In practical terms, a commit or pull request triggers repeatable checks such as compilation, linting, unit tests and security analysis.
Continuous delivery takes that further. A successful change produces a releasable artifact and the system can promote it toward production reliably. Production release may still require a deliberate human decision.
Continuous deployment removes that final manual release step as well: a change that satisfies the pipeline’s controls can be deployed automatically.
None of those definitions require Kubernetes, GitHub Actions, Jenkins or a particular branching strategy. The tool is implementation detail. The objective is a repeatable path from source code to running software with useful evidence at every stage.
DORA’s continuous-delivery research emphasises capabilities such as reliable automated tests, deployment automation and trunk-based development because they reduce the amount of uncertainty bundled into each release. Small, frequent changes are simply easier to understand, test and recover from than enormous releases containing three weeks of unrelated work.
Start with the guarantees, not the YAML
Before designing jobs and stages, write down what must be true before software reaches production.
For a typical web application, a sensible first set of guarantees might be:
- The exact commit being released has passed the required automated tests.
- Dependencies are restored from a lockfile rather than silently floating to whatever version happens to be newest.
- The application is built once in a controlled environment.
- The resulting artifact is identifiable and immutable.
- The artifact deployed to production is the same artifact that passed the preceding checks.
- Production credentials are not available to ordinary build or pull-request jobs.
- A failed deployment is detectable quickly.
- The team knows how to roll back or roll forward safely.
That list is far more useful than starting with “we need five pipeline stages”. Stages should exist because they enforce a guarantee, not because a diagram on the internet had five boxes.
A practical pipeline shape
For many applications, this is enough:
Developer change
│
▼
Pull request
│
├── lint / formatting
├── compile / type-check
├── unit tests
├── dependency + security checks
└── focused integration tests
│
▼
Merge to main
│
▼
Build release artifact ONCE
│
├── attach version / commit identity
├── generate integrity/provenance information where appropriate
└── store immutable artifact
│
▼
Deploy same artifact to staging
│
├── smoke tests
└── higher-cost integration / E2E checks
│
▼
Production approval or automatic promotion
│
▼
Deploy same artifact to production
│
├── health checks
├── telemetry / error monitoring
└── rollback or roll-forward path
You can make this more sophisticated later. Resist doing so until the simple version is trustworthy.
1. Optimise CI for feedback, not theatre
The first few minutes after a developer pushes a change are unusually valuable. They still remember what they changed and why. A compiler error returned in 40 seconds is useful; the same error returned after a 22-minute queue of unrelated checks is an interruption.
Put cheap, high-signal checks early.
Formatting, linting, type checking, compilation and focused unit tests should generally run before expensive end-to-end suites. Independent jobs can run in parallel. Cache dependency downloads where it is safe to do so. Do not make every pull request rebuild the universe because one CSS file changed if your architecture lets you identify the affected area reliably.
There is a trap here: “fast” must not become “skip the checks that catch real failures”. The goal is to move useful failures earlier and reserve expensive validation for the places where it adds value.
A healthy pipeline often has two feedback loops:
- PR loop: fast enough that developers will actually respond to it while working.
- release loop: more comprehensive and allowed to spend additional time proving the release candidate.
If your PR pipeline takes half an hour, measure where that time goes before buying faster runners. Often the real culprit is duplicated package restoration, serial jobs that could run concurrently, enormous unfocused test suites or tests doing network work they never needed to do.
2. Build once, then promote the artifact
One of the most important CI/CD rules is also one of the easiest to violate:
Do not independently rebuild the application for staging and production.
Imagine this flow:
main → build for staging → test staging
main → build again for production → deploy production
Those are two builds. Even if they point at the same Git commit, dependency resolution, generated files, timestamps, external package registries, build images or tooling can make the outputs differ.
A stronger model is:
main → build artifact A → deploy A to staging → test A → deploy A to production
Now the thing you tested is the thing you release.
For a .NET service, that artifact might be a published application package or container image. For a frontend it may be the generated static bundle. For a containerised service, use an immutable image digest for promotion rather than relying only on a mutable tag such as latest.
The same principle applies to configuration: keep environment-specific configuration outside the compiled artifact wherever practical. The binary should not need to be rebuilt because production has a different API hostname from staging.
3. Make every release traceable
When something breaks at 02:00, “production is running version 1.7-ish” is not enough.
At minimum, you should be able to answer:
- Which source commit produced this release?
- Which workflow/build produced the artifact?
- Which artifact was deployed?
- When was it deployed and by whom or what?
- Which tests/checks passed before promotion?
- What was running immediately before it?
A simple version string plus Git commit SHA gets you surprisingly far. Container digests and artifact checksums improve the chain further.
For software where supply-chain integrity matters, build provenance provides stronger evidence about where, when and how an artifact was produced. SLSA (Supply-chain Levels for Software Artifacts) defines progressively stronger build guarantees; its current build track begins with provenance existing and progresses toward signed provenance and hardened build platforms.
You do not need to turn a three-person SaaS team into a compliance department overnight. But knowing exactly where a production artifact came from is not enterprise bureaucracy. It is basic operational hygiene.
4. Treat the pipeline as production infrastructure
CI/CD systems are attractive targets because they sit in a privileged position: they consume source code, execute scripts, download third-party code, access secrets, create artifacts and often have permission to deploy them.
OWASP’s CI/CD security guidance therefore treats the pipeline itself as part of the attack surface. That changes how you should think about a workflow file. It is not harmless configuration. A pull request that modifies .github/workflows/deploy.yml may be modifying production infrastructure.
At a minimum:
- protect important branches;
- require review for sensitive changes;
- use MFA on source-control accounts;
- give workflow identities the minimum permissions they need;
- do not hard-code credentials in source or workflow files;
- avoid exposing production secrets to workflows triggered by untrusted code;
- pin and review third-party actions/dependencies appropriately;
- keep runners and build images patched;
- retain enough logs to investigate a suspicious deployment;
- separate build permissions from production deployment permissions.
The last point is especially important. A job that runs tests on arbitrary pull-request code usually has no legitimate reason to possess a credential capable of deploying production.
5. Prefer short-lived identity over permanent cloud keys
Historically, CI systems were often configured by creating a cloud service-account key, storing it as a repository secret and hoping nobody ever printed or exfiltrated it.
Modern CI platforms and major cloud providers support a better pattern: OpenID Connect (OIDC) federation.
Instead of storing a long-lived cloud credential in GitHub, the workflow proves its identity to the cloud provider and receives a short-lived credential under a trust policy you control. GitHub explicitly recommends OIDC for authenticating workflows to cloud providers.
Conceptually:
GitHub workflow
│
├── requests signed OIDC identity token
│
▼
Cloud identity provider
│
├── verifies repository / branch / environment claims
│
▼
Short-lived deployment credential
If your platform supports it, this removes an entire class of “forgotten production key sitting in CI secrets for four years” problems.
6. Default workflow permissions to read-only
GitHub Actions makes this particularly concrete. Define the permissions a job actually needs instead of accepting broad defaults.
A simplified CI workflow might begin like this:
name: ci
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- name: Install locked dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm test
- name: Build
run: npm run build
The important part is not that your application must use Node 22. It is the shape: the workflow starts without write permission, installs from the committed lockfile, and fails before deployment ever enters the conversation.
If another job genuinely needs permission such as packages: write, grant it to that job rather than broadening the entire workflow.
Before you automate the deployment, inspect what you are deploying
SiteTidy can audit a public site for technical, performance, SEO and security issues — useful as a post-deployment sanity check alongside your application-specific tests.
7. Separate CI from production deployment
It is tempting to end a giant workflow with:
- run: deploy-production.sh
That is fine for a toy project. For a real application, make production deployment a distinct security boundary.
Use your CI/CD platform’s environment protection features where available. Production can require a particular branch, a reviewer, a dedicated identity and its own environment-scoped secrets. The deployment job should consume the already-built release artifact rather than rebuilding the application.
A simplified GitHub Actions deployment job using OIDC might look conceptually like this:
permissions:
contents: read
id-token: write
jobs:
deploy-production:
environment: production
runs-on: ubuntu-latest
steps:
- name: Download release artifact
uses: actions/download-artifact@v4
with:
name: release
# Authenticate to your cloud provider here using OIDC.
# The exact action and trust configuration depend on AWS,
# Azure, Google Cloud or another deployment platform.
- name: Deploy
run: ./scripts/deploy-production.sh
Notice what is missing: no production password embedded in the YAML, and no second application build.
8. A deployment is not successful because the CLI returned zero
This is an operational mistake that survives in otherwise mature pipelines.
The deployment command completes successfully, so the pipeline paints the job green. Thirty seconds later the application starts returning HTTP 500 because a database migration failed, a required environment variable is missing or the new container cannot reach a dependency.
Your deployment process needs a definition of healthy.
That may include:
- the new instances passing readiness/health checks;
- a smoke request reaching a critical endpoint;
- an authentication flow completing;
- a synthetic transaction succeeding;
- error rate remaining within an acceptable range;
- latency not jumping unexpectedly;
- background workers actually consuming jobs.
The right checks depend on the application. GET / returning 200 is useful, but it is not proof that checkout, login or payment processing works.
9. Design rollback before you need rollback
“SSH into the server and fix it” is not a rollback strategy.
Before enabling automatic production deployments, answer this question:
If the next release is bad, how do we restore service in five minutes?
Depending on your architecture, that could mean redeploying the previous immutable artifact, switching traffic back to the previous environment, reverting a feature flag, or issuing a small forward fix.
Database changes deserve special care. An application rollback can become impossible if the new deployment has already applied a destructive migration that the previous version cannot understand.
A useful pattern is to make schema changes backward-compatible across the deployment window: add before removing, tolerate both representations temporarily, migrate data safely, then remove the old path in a later release.
This is less exciting than a clever deployment animation. It is also the sort of engineering that saves an evening.
10. Do not let flaky tests become normal
A flaky test is not merely annoying. It teaches developers that a red pipeline may not mean anything.
Once that happens, people rerun failed jobs until they turn green. The pipeline has stopped being evidence and become a slot machine.
Track flaky tests as defects. Quarantine one temporarily if you must keep the team moving, but assign an owner and fix or remove it. A smaller reliable suite is more valuable than a gigantic suite everybody ignores.
The same applies to scanners. If a security tool produces hundreds of unactionable warnings on every run, developers will learn to scroll past the one warning that matters. Tune the signal.
A sensible first production pipeline
If you are starting from scratch, do not try to implement every possible DevSecOps control in week one. Build confidence in layers.
Pull requests
- restore dependencies deterministically;
- lint/type-check/compile;
- run fast unit tests;
- run focused integration tests;
- run useful dependency/security checks;
- prevent merge when required checks fail;
- require review on protected branches.
Main branch
- repeat the critical validation;
- build the release once;
- version it with an unambiguous source identifier;
- store the artifact immutably;
- deploy that artifact to staging;
- run smoke/integration tests against the deployed application.
Production
- promote the same artifact;
- use a dedicated least-privilege deployment identity;
- prefer short-lived/OIDC credentials;
- protect the production environment appropriately;
- perform post-deployment health checks;
- record deployment metadata;
- make rollback/roll-forward an explicit, tested procedure.
The boring pipeline is often the good pipeline
A pipeline should be predictable enough that releasing software stops feeling like an event. Prefer a small number of well-understood stages, deterministic inputs and clear failure messages over an impressive maze of automation nobody can explain.
Explore SiteTidy's free web toolsCommon CI/CD smells worth fixing
You can learn a lot about a pipeline without knowing which platform it uses. These are warning signs:
“Just rerun it.” A flaky test or unreliable external dependency has been normalised.
“Only Sarah knows how production deploys.” The pipeline has not actually captured the release process.
“Staging passed, but production rebuilds separately.” You did not test the production artifact.
“That secret has been there forever.” Long-lived credentials have no ownership or rotation story.
“Every workflow has admin permissions because it was easier.” Least privilege has been postponed indefinitely.
“We can’t roll back because of the migration.” Deployment and data evolution were designed independently.
“The pipeline is green, but the site is down.” Deployment completion is being confused with application health.
“Nobody touches the workflow because it might break.” Your delivery mechanism has become legacy code.
CI/CD configuration deserves the same treatment as application code: version control, review, testing, refactoring and ownership.
Where to go next
A strong pipeline is not the one with the most stages. It is the one your team trusts.
It gives developers fast feedback while they still have context. It creates the release once and preserves its identity. It keeps production privileges away from untrusted code. It can explain exactly what is running. It knows the difference between “deployment command finished” and “application is healthy”. And when a release goes wrong — because eventually one will — recovery is a rehearsed operation rather than an improvised rescue mission.
Once those foundations are in place, the more advanced work becomes worthwhile: matrix builds, ephemeral test environments, progressive delivery, canary releases, artifact attestations, deeper supply-chain controls and deployment strategies tailored to the architecture.
But get the boring fundamentals right first. They are what make the clever parts safe.
Further reading
For implementation details and current security guidance, consult the primary documentation for your CI/CD platform and deployment provider. Particularly useful references for the principles in this guide are GitHub’s Security for GitHub Actions and Secure use reference, the OWASP CI/CD Security Cheat Sheet, DORA’s Continuous Delivery and Trunk-Based Development capability guides, and the SLSA build/provenance specification.
