Environments, Approvals, Blue-Green, Canary and Rollback Strategies in Jenkins Pipelines

CI CD Pipelines with Jenkins 40 min min read Updated: Nov 17, 2026 Advanced
Environments, Approvals, Blue-Green, Canary and Rollback Strategies in Jenkins Pipelines
Advanced Topic 2 of 3

Environments and Promotion

text build ─► DEV (auto, every commit) ─► STAGING (auto after tests) ─► [approval] ─► PRODUCTION ▲ same artifact, different configuration ─────────┘
EnvironmentDeploysGate to enter
DevEvery merge to develop or mainBuild and unit tests pass
Staging / UATEvery successful build of the release branchQuality gates, integration tests, smoke test
ProductionA chosen build, on demand or on scheduleApproval, change window, passing staging, same artifact
groovy pipeline { agent none environment { TAG = "${env.BUILD_NUMBER}-${env.GIT_COMMIT.take(7)}" } stages { stage('Build and test') { agent { label 'linux' } steps { sh './ci.sh' } } stage('Deploy dev') { agent { label 'deploy' } steps { sh "./deploy.sh dev ${TAG}" } } stage('Deploy staging') { when { branch 'main' } agent { label 'deploy' } options { lock(resource: 'staging-env') } // one deployment at a time (Lockable Resources plugin) steps { sh "./deploy.sh staging ${TAG}"; sh './smoke.sh staging' } } stage('Approval') { when { branch 'main' } steps { timeout(time: 2, unit: 'DAYS') { input message: "Deploy ${TAG} to production?", ok: 'Deploy', submitter: 'release-managers', parameters: [string(name: 'CHANGE_TICKET', description: 'Change request number')] } } } stage('Deploy production') { when { branch 'main' } agent { label 'deploy-prod' } options { lock(resource: 'prod-env') } steps { sh "./deploy.sh prod ${TAG}"; sh './smoke.sh prod' } post { failure { sh './rollback.sh prod' } } } } }
  • agent none at the top and input in a stage without an agent: the waiting approval holds no executor
  • lock (Lockable Resources plugin) stops two pipelines from deploying to the same environment at the same time
  • Send a ticket or change number with the approval for audit
  • Production deploys use dedicated agents with production credentials

Strategy 1: Rolling Update

Replace instances gradually. The default for Kubernetes Deployments and Swarm. Good for most stateless services. Needs backward-compatible versions because old and new run together for a while.

Strategy 2: Blue-Green

Run two identical environments. Blue serves users. Deploy the new version to green, test it privately, then switch traffic at the load balancer or Service selector. Rollback is switching back.

groovy stage('Blue-green switch on Kubernetes') { steps { sh ''' ACTIVE=$(kubectl -n shop get svc shop -o jsonpath='{.spec.selector.color}') NEXT=$([ "$ACTIVE" = "blue" ] && echo green || echo blue) echo "active=$ACTIVE next=$NEXT" helm upgrade --install shop-$NEXT chart/ -n shop --set color=$NEXT --set image.tag=$TAG --wait --atomic kubectl -n shop run smoke --rm -i --restart=Never --image=curlimages/curl -- curl -fsS http://shop-$NEXT/health kubectl -n shop patch svc shop -p '{"spec":{"selector":{"app":"shop","color":"'$NEXT'"}}}' echo "switched to $NEXT; old color $ACTIVE kept for rollback" ''' } }
ProsCons
Instant switch and instant rollbackDouble the resources during the switch
Test the real environment before users see itDatabases need care: both versions share data, so changes must be compatible

Strategy 3: Canary

Send a small share of traffic (1, 5, 25 percent) to the new version, watch metrics, then increase or abort. Needs a smart traffic router: a service mesh, Gateway API weights, an ingress controller with canary support, or Argo Rollouts and Flagger.

groovy stage('Canary') { steps { script { for (weight in [5, 25, 50, 100]) { sh "./set-traffic-weight.sh shop-new ${weight}" sleep time: 5, unit: 'MINUTES' def errRate = sh(script: "./query-error-rate.sh shop-new", returnStdout: true).trim() as Double echo "weight ${weight} error rate ${errRate}" if (errRate > 1.0) { sh './set-traffic-weight.sh shop-new 0' error "Canary failed at ${weight} percent" } } } } }

Feature Flags

  • Deploy code turned off, then enable it for a few users, then everyone, without a new deployment
  • Separates deploy (technical) from release (business decision)
  • Remember to remove old flags, they become hidden complexity

Database Changes

RuleWhy
Use expand and contract: first add the new column, deploy code that works with both, later remove the old columnOld and new versions run at the same time during a rollout or rollback
Never combine a destructive migration with a code deployA rollback cannot restore deleted data
Run migrations as a separate, tracked step (Flyway, Liquibase, Alembic)Visible, repeatable, and can be approved on its own
Take a backup or snapshot before risky migrationsA way back

Rollback Plan

PlatformRollback
Kubernetes Deploymentkubectl rollout undo deployment/NAME
Helmhelm rollback RELEASE REVISION
Swarmdocker service rollback NAME
Server with symlinksPoint the symlink to the previous release and restart
Blue-greenSwitch traffic back
Feature flagTurn the flag off
groovy pipeline { agent any parameters { string(name: 'ROLLBACK_TO', defaultValue: '', description: 'Tag to restore, for example 128-3f9c2ab') } stages { stage('Rollback') { when { expression { params.ROLLBACK_TO } } steps { sh "./deploy.sh prod ${params.ROLLBACK_TO}" // redeploy a previously built, tested image: no rebuild sh './smoke.sh prod' } } } }
Practice rollbacks: Run a rollback drill on staging every month. A rollback you have never tried is a plan on paper only.

Notifications and Audit Trail

groovy post { success { slackSend channel: '#releases', color: 'good', message: "Deployed ${env.TAG} to ${env.TARGET_ENV}: ${env.BUILD_URL}" } failure { slackSend channel: '#releases', color: 'danger', message: "FAILED deploy ${env.TAG} to ${env.TARGET_ENV}: ${env.BUILD_URL}" } }
  • Record who approved, which ticket, which artifact digest and when (Jenkins keeps the approver in the build log; also write a line to a deployment log or ticket system)
  • Add change windows: block production deploys outside agreed hours with a when { expression { ... } } check or a Jenkins plugin

Practice Tasks

  1. Create a pipeline with dev, staging and production stages and an approval before production
  2. Use a lock so two builds cannot deploy to staging at the same time
  3. Implement a blue-green switch on Kubernetes using two Helm releases and one Service
  4. Add a failing health check and show an automatic rollback
  5. Write a rollback pipeline that redeploys an earlier tag

Conclusion

Promote one artifact through dev, staging and production with gates, manual approvals without holding executors, locks and notifications. Choose rolling, blue-green or canary depending on risk, make database changes backward compatible, and always have a practised rollback.

Quick Summary: environments + gates; agent none + input + lock; rolling/blue-green/canary code; feature flags; DB expand-contract; rollback table + pipeline; notifications + audit.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators