Overview and What You Will Learn
Environment promotion means one image is built once, then moved through dev, staging and production, with automation up to staging and a named human approval before production. The thing you promote is the immutable image digest, never a rebuild from the same commit.
In this lab, you will:
- Lay out a config repo with a shared Kustomize base and one overlay per environment
- Verify each overlay locally with
kustomize build - Create GitHub environments, with required reviewers on production
- Build a workflow that promotes the same digest through all three environments
- Read the audit trail and practise a rollback
Why This Matters in Production
A bad change in dev breaks one developer's afternoon, while the same change in production can break every customer. Picture a payments company like PhonePe: staging is deployed within minutes of every merge so it always mirrors production, while production needs a named approver on record for compliance. The automation removes toil, and the human gate means someone senior has looked at exactly what is about to ship.
Common MistakeRebuilding the image for each environment from the same commit. A rebuild can pull a newer base image or different dependencies, so the binary that passed staging is not the binary that reaches production. Promote the digest that passed.
Core Principles
The Three-Environment Flow
Every merge builds one image, which flows through automated stages and stops at a production approval gate.
once] B --> C[Deploy to dev] C --> D[Deploy to staging] D --> E[Smoke tests] E --> F{{Reviewer approval}} F --> G[Deploy to production] classDef start fill:#475569,color:#FFFFFF classDef auto fill:#326CE5,color:#FFFFFF classDef gate fill:#F59E0B,color:#111827 classDef prod fill:#10B981,color:#FFFFFF class A start class B,C,D,E auto class F gate class G prod
Separate the App Repo from the Config Repo
The app repo holds code and CI. The config repo holds the desired state of every environment, and CI changes it by committing a new image digest. GitOps tools such as ArgoCD watch the config repo and sync the cluster to match.
code and CI] -->|Builds| R[(Image registry)] A -->|Commits digest| C[Config repo
overlays] G[ArgoCD] -->|Watches| C G -->|Syncs| K[Cluster] classDef repo fill:#8B5CF6,color:#FFFFFF classDef store fill:#475569,color:#FFFFFF classDef tool fill:#F97316,color:#FFFFFF classDef cloud fill:#326CE5,color:#FFFFFF class A,C repo class R store class G tool class K cloud
Keeping them apart keeps the Git history of the config repo a clean record of what changed in each environment, and lets you give CI write access to config without giving it to every developer.
Kustomize Overlays Hold Only the Differences
A shared base holds the manifests, and each overlay patches only what differs, such as replicas and the image.
config-repo/ base/ deployment.yaml # shared spec service.yaml kustomization.yaml overlays/ dev/kustomization.yaml # 1 replica staging/kustomization.yaml # 2 replicas production/kustomization.yaml # 10 replicasapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources: - ../../baseimages: - name: order-gateway newName: 123456789012.dkr.ecr.ap-south-1.amazonaws.com/order-gateway digest: sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefpatches: - patch: |- - op: replace path: /spec/replicas value: 2 target: kind: Deployment name: order-gatewaySecurityPin a digest, not a tag. A tag such as
v1.4.2can be re-pushed to point at a different image, but a digest always names the same bytes. Setting ECR tag immutability on the repository protects the SHA tags as well.
GitHub Environment Protection Rules
A GitHub Environment can require named reviewers before any job that targets it starts. The approval, with who approved and when, is stored against the workflow run. Required reviewers are available on all plans for public repositories, and need a paid plan for private repositories.
RememberRe-running a deployment job asks for approval again, and enabling "prevent self-review" stops the person who triggered the run from approving their own deploy.
Drift Detection and the Audit Trail
ArgoCD flags any difference between the config repo and the live cluster, so a manual kubectl apply in production shows up as OutOfSync. Every promotion also leaves three linked records:
- The GitHub run, showing who approved, when, and which commit.
- The config repo commit, showing exactly which digest changed in which environment.
- The ArgoCD sync history, showing when the cluster converged.
Together they answer "who wrote it, who approved it, and when did it reach production".
Detailed Step-by-Step Practical Lab
This lab assumes an ECR repository, an IAM role GitHub can assume through OIDC, an ArgoCD instance watching the config repo, and kustomize and gh on your machine. Replace the account ID, organization and repo names with your own.
- Create the config repo layout and a minimal base:
mkdir -p config-repo/base && cd config-repo cat > base/deployment.yaml << 'EOF'apiVersion: apps/v1kind: Deploymentmetadata: name: order-gatewayspec: replicas: 1 selector: matchLabels: app: order-gateway template: metadata: labels: app: order-gateway spec: containers: - name: order-gateway image: order-gateway ports: - containerPort: 8080EOF cat > base/service.yaml << 'EOF'apiVersion: v1kind: Servicemetadata: name: order-gatewayspec: selector: app: order-gateway ports: - port: 80 targetPort: 8080EOF cat > base/kustomization.yaml << 'EOF'apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources: - deployment.yaml - service.yamlEOF- Generate the three overlays with a different replica count each:
for pair in dev:1 staging:2 production:10; do ENV=${pair%%:*} REPLICAS=${pair##*:} mkdir -p overlays/$ENV cat > overlays/$ENV/kustomization.yaml << EOFapiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationnamespace: order-gateway-$ENVresources: - ../../basepatches: - patch: |- - op: replace path: /spec/replicas value: $REPLICAS target: kind: Deployment name: order-gatewayEOFdone- Verify each overlay renders before CI ever touches it:
for ENV in dev staging production; do echo "== $ENV" kustomize build overlays/$ENV | grep -E "replicas|namespace:"done- Create the GitHub environments. Dev and staging have no rules. Production gets a reviewer, no self-review, and a 5 minute wait timer. Replace the reviewer ID with the numeric ID of your on-call lead:
export REPO=devops-platform/order-gateway gh api --method PUT repos/$REPO/environments/devgh api --method PUT repos/$REPO/environments/staging cat > prod-env.json << 'EOF'{ "wait_timer": 5, "prevent_self_review": true, "reviewers": [{ "type": "User", "id": 12345678 }]}EOFgh api --method PUT repos/$REPO/environments/production --input prod-env.json- Write one reusable deploy workflow, so dev, staging and production run identical logic. It commits the promoted digest to the matching overlay. Save it as
.github/workflows/deploy.yamlin the app repo:
name: Deploy to environment on: workflow_call: inputs: environment: required: true type: string digest: required: true type: string jobs: deploy: runs-on: ubuntu-latest environment: ${{ inputs.environment }} # Production pauses here for approval steps: - uses: actions/checkout@v4 with: repository: devops-platform/config-repo token: ${{ secrets.CONFIG_REPO_TOKEN }} - name: Pin the promoted digest working-directory: overlays/${{ inputs.environment }} run: | kustomize edit set image \ order-gateway=123456789012.dkr.ecr.ap-south-1.amazonaws.com/order-gateway@${{ inputs.digest }} if git diff --quiet; then echo "Already at this digest, nothing to commit" exit 0 fi git config user.email "ci@devops.in" git config user.name "CI Bot" git commit -am "${{ inputs.environment }}: promote ${{ inputs.digest }}" git pull --rebase git push- Write the promotion workflow. It builds once and passes the digest to every deploy. Save it as
.github/workflows/promote.yaml:
name: Build and Promote on: push: branches: [main] permissions: id-token: write contents: read concurrency: group: promote-main cancel-in-progress: false # Never cancel a deploy halfway jobs: build: runs-on: ubuntu-latest outputs: digest: ${{ steps.push.outputs.digest }} steps: - uses: actions/checkout@v4 - uses: docker/setup-buildx-action@v3 - uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: arn:aws:iam::123456789012:role/cicd-ecr-push aws-region: ap-south-1 - uses: aws-actions/amazon-ecr-login@v2 - id: push uses: docker/build-push-action@v5 with: push: true tags: 123456789012.dkr.ecr.ap-south-1.amazonaws.com/order-gateway:${{ github.sha }} deploy-dev: needs: build uses: ./.github/workflows/deploy.yaml with: environment: dev digest: ${{ needs.build.outputs.digest }} secrets: inherit deploy-staging: needs: deploy-dev uses: ./.github/workflows/deploy.yaml with: environment: staging digest: ${{ needs.build.outputs.digest }} secrets: inherit verify-staging: needs: deploy-staging runs-on: ubuntu-latest env: ARGOCD_SERVER: argocd.devops.in ARGOCD_AUTH_TOKEN: ${{ secrets.ARGOCD_TOKEN }} steps: - name: Install the ArgoCD CLI run: | curl -sSL -o argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64 chmod +x argocd && sudo mv argocd /usr/local/bin/ - name: Wait for staging to sync and be healthy run: argocd app wait order-gateway-staging --health --timeout 180 --grpc-web - name: Smoke test run: curl -sf --retry 10 --retry-delay 5 --retry-connrefused https://staging.order-gateway.devops.in/healthz deploy-production: needs: [build, verify-staging] uses: ./.github/workflows/deploy.yaml with: environment: production digest: ${{ needs.build.outputs.digest }} secrets: inherit- Push to main and watch the run. Dev and staging deploy on their own, the smoke test runs, and production waits for the reviewer and the wait timer:
gh run watch- After production deploys, read the audit trail from all three sources:
export RUN_ID=$(gh run list --workflow promote.yaml --limit 1 --json databaseId --jq '.[0].databaseId') # Who approved the production deployment, and whengh api repos/$REPO/actions/runs/$RUN_ID/approvals --jq '.[] | {environment: .environments[0].name, state, user: .user.login}' # What changed in the config repogit -C config-repo log --oneline overlays/production/ # When the cluster convergedargocd app history order-gateway-production --grpc-web- Practise a rollback. Revert the config repo commit for production and let ArgoCD sync the previous digest, which is faster and safer than rebuilding:
cd config-repogit revert --no-edit HEADgit push- Clean up the lab environments:
gh api --method DELETE repos/$REPO/environments/devgh api --method DELETE repos/$REPO/environments/staginggh api --method DELETE repos/$REPO/environments/productionProduction Best Practices & Common Pitfalls
TipTag images with the commit SHA and promote by committing the digest to the config repo. The SHA is permanent and traceable, and the overlay files tell you exactly what is running where, so mutable
stagingandproductiontags are not needed.
SecurityGive CI a token scoped to the config repo only, ideally from a GitHub App, and keep production secrets on the production environment so only approved runs can read them.
- Promote digests, never tags. A digest cannot be re-pointed, and it is the artifact staging actually tested.
- Keep the config repo separate. Mixing code and cluster state makes the GitOps diff noisy and complicates access control.
- Use a wait timer on production. Five minutes gives the team a window to cancel after an approval if something looks wrong.
- Automate staging smoke tests. Production approval should never depend on a human checking staging by hand.
- Use
kustomize editinstead ofsed. It edits the file in a structured, repeatable way and survives format changes. - Serialize config repo writes. A concurrency group and
git pull --rebasestop two runs racing to push the same overlay.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
kustomize build overlays/staging |
Render an overlay locally |
kustomize edit set image NAME=REPO@sha256:... |
Pin an image digest in an overlay |
gh run watch |
Follow the current workflow run |
gh api repos/OWNER/REPO/actions/runs/ID/approvals |
See who approved a run |
argocd app get order-gateway-staging |
Check sync and health status |
argocd app history order-gateway-production |
List past syncs |
| Problem | Likely cause | Fix |
|---|---|---|
| Production job never starts | Waiting for reviewer or wait timer | Check the run page and have a reviewer approve |
| Staging runs an unexpected image | Overlay not updated or sync pending | Check the overlay digest and ArgoCD sync state |
| ArgoCD shows OutOfSync | Auto-sync is off or sync failed | Enable auto-sync or run a manual sync |
| Push to config repo is rejected | Two runs pushed at once | Add git pull --rebase and a concurrency group |
| Digest not found | Build job did not push | Confirm the push step ran and the output is not empty |