Skip to main content

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.

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.

EventTriggerWhat happens
BuildPush to main (matching change detection)Build each service's image once, pin it by digest, and push it to the registry
Dev deploySuccessful build (automatic)Update the Dev configuration reference; GitOps syncs it
Staging promotionMerge of a promotion PRReflect the same digest into the Staging configuration
Prod promotionMerge of an approved promotion PRReflect the same digest into the Prod configuration
Release (feature exposure)Turning a feature flag ONExpose 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: main is 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 as latest
  • 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 a git 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."

EnvironmentTriggerAutomationApproval
BuildPush to main (with path conditions)AutomaticNot required
DevSuccessful buildAutomatic (configuration update commit)Not required
StagingMerge of a promotion PRSemi-automatic (PR creation automatic or manual)Optional
ProdMerge of an approved promotion PRManual approval + automatic applyRequired

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​

PatternCharacteristicsBest suited for
deploy/<env>/ in the monorepoSimple. Services and configuration complete in the same repositorySmall teams, early stage
A configuration repository per environmentClear permission separation and auditing. You can restrict writes to the Prod configurationOrganizations 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​

  1. Turn the feature flag OFF (seconds; effective for feature-level issues)
  2. Revert the promotion PR (GitOps syncs back to the previous digest; minutes)
  3. 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 CODEOWNERS per 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-PatternWhat Goes WrongCountermeasure
Rebuilding per environmentThe tested artifact no longer matches what runs in productionBuild once and promote by digest
Deploying with mutable tags such as latestYou cannot trace which commit is running, and rollback is ambiguousUse commit SHA tags + digest references
Expressing promotion via cherry-picks or mergesEnvironment branch drift and conflicts returnExpress promotion as a configuration reference update
Releasing the whole monorepo in lockstepA change in one service blocks all servicesMake per-service independent promotion the default
Bypassing GitOps with manual kubectl changesDrift occurs and Git stops being the source of truthRoute all changes through Git (detect drift)
Prod promotion passing through automaticallyControls are ineffective and incidents have a large impactRequire an approval gate and canary

11. Adoption Steps​

  1. Pin images by digest — Stop using tags; build once on push to main
  2. Automate Dev — Introduce GitOps and auto-deploy to Dev on merges to main
  3. Introduce Staging promotion PRs — Express configuration updates as PRs, keeping reviews and history
  4. Introduce Prod approval and canary — Approval gates + gradual rollout + automatic rollback
  5. 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.