Deployment Pipeline and Environment Promotion Best Practices
In trunk based development, environments (Dev / Staging / Prod) are stages of the deployment pipeline rather than branches, and the same artifact is promoted through the environments in order (see "Handling Multiple Environments" in Trunk Based Development Best Practices).
This article assumes a setup of managing microservices in a monorepo and deploying to Kubernetes, and explains how to manage the deployment pipeline to dev/staging/prod, focusing on one recommended pattern: the GitOps artifact promotion pipeline. It covers trigger design, monorepo change detection, how to express promotion, approvals, and rollback.
Overview of the Recommended Pattern
The recommended pattern can be summarized in one sentence:
Starting from a merge to
main, "build once," and promote the same artifact from Dev → Staging → Prod through "promotion pull requests" that rewrite the image reference in each environment's configuration (Git). Let the GitOps operator (Argo CD / Flux) handle applying it to Kubernetes.
The essence of this pattern is treating build, deployment, and release as separate events.
| Event | Trigger | What happens |
|---|---|---|
| Build | Push to main (matching change detection) | Build each service's image once, pin it by digest, and push it to the registry |
| Dev deploy | Successful build (automatic) | Update the Dev configuration reference; GitOps syncs it |
| Staging promotion | Merge of a promotion PR | Reflect the same digest into the Staging configuration |
| Prod promotion | Merge of an approved promotion PR | Reflect the same digest into the Prod configuration |
| Release (feature exposure) | Turning a feature flag ON | Expose the deployed feature to users (see Feature Flag Management) |
Assumed Setup
- Monorepo: Each microservice lives under
services/<service-name>/ - Kubernetes: Workloads run on Kubernetes
- GitOps: Argo CD or Flux watches the configuration in Git and applies it to the cluster
- Trunk based development:
mainis always releasable; no environment branches - CI: A general-purpose CI such as GitHub Actions (examples are written for GitHub Actions)
1. Designing the Artifact (Make It Immutable)
The artifact is the foundation of the promotion pipeline.
- Tag images with the commit SHA (e.g.,
sha-4f9c2ab) and always reference them by digest (sha256:...). Never use mutable tags such aslatest - Keep artifacts independent per service. Even in a monorepo, keep the release unit per microservice
- If possible, generate SBOMs and sign artifacts (e.g., with cosign), and verify them during promotion
Why digests? Tags can be overwritten, but a digest is the content itself. It guarantees byte-for-byte that "what was verified in Staging is what gets promoted to Prod."
2. Change Detection in a Monorepo
In a monorepo, a mechanism to detect "which services need building" is essential.
- Path filters: Detect changes per service via
services/<service>/** - Dependency graph: Changes to shared libraries (e.g.,
libs/**) must include dependent services in the build targets (tools such as Nx, Bazel, or Turborepo, or agit diff-based detection script) - Matrix builds: Feed the detection result into the CI matrix and build/test services in parallel
# Concept of change detection (a git diff based example)
git diff --name-only "$BEFORE" "$AFTER" \
| grep '^services/' \
| cut -d/ -f2 \
| sort -u
The key is to record the detection results (service names and digests) in the artifact registry and deployment records so that the later promotion steps can reference them.
3. Trigger Design (Separate by Environment)
Separate the triggers clearly for "build," "Dev," "Staging," and "Prod."
| Environment | Trigger | Automation | Approval |
|---|---|---|---|
| Build | Push to main (with path conditions) | Automatic | Not required |
| Dev | Successful build | Automatic (configuration update commit) | Not required |
| Staging | Merge of a promotion PR | Semi-automatic (PR creation automatic or manual) | Optional |
| Prod | Merge of an approved promotion PR | Manual approval + automatic apply | Required |
What matters is not triggering builds per environment. "Deploying to Staging" means "rewriting the Staging configuration to the new digest," not rebuilding.
4. How to Express Promotion (Rewriting Environment Configuration)
Promotion is expressed by "updating the image reference in the next environment's configuration file to the same digest."
# deploy/staging/orders/values.yaml
image:
repository: registry.example.com/orders
# Promotion = just update this reference to the new digest (no rebuild)
digest: sha256:0f3d...c1
The diff of a promotion PR is only this reference change. In other words, the biggest advantage of this pattern is that "what is promoted to which environment" is reviewable and auditable as the PR diff itself.
Where to Put the Configuration
| Pattern | Characteristics | Best suited for |
|---|---|---|
deploy/<env>/ in the monorepo | Simple. Services and configuration complete in the same repository | Small teams, early stage |
| A configuration repository per environment | Clear permission separation and auditing. You can restrict writes to the Prod configuration | Organizations that need production controls |
Either way, the concept is the same. It is fine to migrate in stages: separate when the permission requirements for Prod become stronger.
Per-Service Promotion and Bundled Promotion
- By default, promote services independently (leverage the benefits of microservices)
- Only when multiple services must be released together (e.g., API compatibility), bundle multiple service reference changes into one promotion PR (bundled promotion)
- Avoid "lockstep" operations that pin the entire monorepo to a single version
5. Synchronization via GitOps
- Argo CD / Flux watches the per-environment configuration (Git) and keeps the cluster state in sync (continuous sync)
- Manage "service × environment" declaratively with
ApplicationSet/ Kustomize / Helm - GitOps handles "configuration → cluster" application, while the pipeline handles "build → configuration update." Do not mix the responsibilities
- Visualize sync status and drift (manual changes) detection, and alert on it
6. Concrete Pipeline Examples (GitHub Actions)
Build and Dev Deploy (Push to main)
# .github/workflows/build.yml (overview)
on:
push:
branches: [main]
paths: ['services/**', 'libs/**']
jobs:
detect:
# Detect changed services and their dependencies, and output JSON for the matrix
outputs:
services: ${{ steps.detect.outputs.services }}
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
- id: detect
run: ./ci/detect-changed-services.sh
build:
needs: detect
strategy:
matrix: ${{ fromJSON(needs.detect.outputs.services) }}
steps:
- uses: actions/checkout@v4
- run: |
IMAGE="$REGISTRY/${{ matrix.service }}:${{ github.sha }}"
docker build -t "$IMAGE" "services/${{ matrix.service }}"
docker push "$IMAGE"
- run: ./ci/update-dev-config.sh "${{ matrix.service }}" "${{ github.sha }}"
# Update the Dev configuration reference and commit (direct commit since no review is needed)
Promotion (Staging / Prod)
# .github/workflows/promote.yml (overview)
on:
workflow_dispatch:
inputs:
service:
required: true
digest:
required: true
target:
type: choice
options: [staging, prod]
jobs:
promote:
steps:
- uses: actions/checkout@v4
# Create a PR that updates the target environment's configuration to the specified digest
- run: ./ci/create-promotion-pr.sh "${{ inputs.service }}" "${{ inputs.digest }}" "${{ inputs.target }}"
- For Prod promotion, configure Required reviewers in GitHub Environments and require approval to merge the promotion PR
- Obtain cloud credentials via OIDC; do not keep long-lived keys
7. Quality Gates and Verification
- Dev: Unit/integration tests and smoke tests. Do not create a promotion PR when they fail
- Staging: E2E and acceptance tests. Record the results as checks (commit statuses) on the promotion PR so that "only verified digests can be promoted to Prod"
- Prod: Roll out traffic gradually with canary/blue-green (Argo Rollouts / Flagger), and configure automatic rollback based on metrics (error rate, latency)
- Treat the post-deploy smoke test and the flag ON/OFF as separate events
8. Rollback on Failure
- Turn the feature flag OFF (seconds; effective for feature-level issues)
- Revert the promotion PR (GitOps syncs back to the previous digest; minutes)
- Canary automatic rollback (automatic based on metrics)
Make database migrations backward compatible with Expand / Contract so the old code can still run during a rollback.
9. Monorepo Operating Rules
- Set up
CODEOWNERSper service and clarify the reviewers for promotion PRs - Make per-service independent deployment the default, and manage release notes and versions per service as well
- Propagate shared library changes to affected services automatically based on the dependency graph
- Keep CI fast with caching and parallelization (a clogged build queue increases lead time)
10. Anti-Patterns
| Anti-Pattern | What Goes Wrong | Countermeasure |
|---|---|---|
| Rebuilding per environment | The tested artifact no longer matches what runs in production | Build once and promote by digest |
Deploying with mutable tags such as latest | You cannot trace which commit is running, and rollback is ambiguous | Use commit SHA tags + digest references |
| Expressing promotion via cherry-picks or merges | Environment branch drift and conflicts return | Express promotion as a configuration reference update |
| Releasing the whole monorepo in lockstep | A change in one service blocks all services | Make per-service independent promotion the default |
| Bypassing GitOps with manual kubectl changes | Drift occurs and Git stops being the source of truth | Route all changes through Git (detect drift) |
| Prod promotion passing through automatically | Controls are ineffective and incidents have a large impact | Require an approval gate and canary |
11. Adoption Steps
- Pin images by digest — Stop using tags; build once on push to main
- Automate Dev — Introduce GitOps and auto-deploy to Dev on merges to main
- Introduce Staging promotion PRs — Express configuration updates as PRs, keeping reviews and history
- Introduce Prod approval and canary — Approval gates + gradual rollout + automatic rollback
- Improve with metrics — Measure lead time, promotion frequency, change failure rate, and time to restore
Final Checklist
- Building once on merges to main and pinning by digest
- Monorepo change detection works per service (including the dependency graph)
- Dev deploys automatically, and Staging/Prod promotion is expressed as PRs
- Promotion PR diffs show "which service's which digest goes to which environment"
- Prod promotion has an approval gate (Required reviewers, etc.)
- GitOps (Argo CD / Flux) syncs environment configuration and can detect drift
- Staging verification results are recorded as checks on the promotion PR
- Canary/blue-green and automatic rollback are configured
- Rollback procedures (flag OFF, promotion revert) are practiced
Conclusion
When in doubt about deployment pipeline design, return to the principle: "build once, promote by updating configuration references, apply with GitOps." By keeping independent per-service promotion even in a monorepo and clearly separating triggers per environment, you can achieve auditable and safe deployments without slowing down trunk based development.