0
0
Jenkinsdevops~10 mins

Blue-green deployment pattern in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Blue-green deployment helps you update your app without downtime by running two identical environments. One is live (blue), and the other (green) is updated and tested. Then traffic switches to green, making the update seamless.
When you want to update your web app without making users wait or see errors.
When you need to test a new version of your app in a real environment before making it live.
When you want a quick way to roll back to the old version if the new one has problems.
When your app runs on servers or containers and you want zero downtime during updates.
When you want to reduce risks during deployment by switching traffic instead of replacing code live.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Deploy to Green') {
      steps {
        echo 'Deploying new version to green environment'
        // Simulate deployment commands
        sh 'echo Deploying app version 2 to green environment'
      }
    }
    stage('Test Green') {
      steps {
        echo 'Running tests on green environment'
        // Simulate tests
        sh 'echo Tests passed on green environment'
      }
    }
    stage('Switch Traffic') {
      steps {
        echo 'Switching traffic from blue to green environment'
        // Simulate traffic switch
        sh 'echo Traffic switched to green environment'
      }
    }
  }
  post {
    success {
      echo 'Deployment successful! Blue-green switch complete.'
    }
    failure {
      echo 'Deployment failed! Keeping blue environment live.'
    }
  }
}

This Jenkinsfile defines a pipeline with three main stages:

  • Deploy to Green: Deploys the new app version to the green environment without affecting users.
  • Test Green: Runs tests on the green environment to ensure the new version works correctly.
  • Switch Traffic: Switches user traffic from the blue (old) environment to the green (new) environment.

The post section handles success or failure messages to inform about deployment status.

Commands
This command updates the Jenkins job with the new Jenkinsfile that contains the blue-green deployment pipeline.
Terminal
jenkins-jobs --conf jenkins.ini update Jenkinsfile
Expected OutputExpected
Job updated successfully
Starts the blue-green deployment pipeline job and waits for it to finish, showing the console output.
Terminal
jenkins-cli build blue-green-deployment -s
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Deploying new version to green environment [Pipeline] sh + echo Deploying app version 2 to green environment Deploying app version 2 to green environment [Pipeline] echo Running tests on green environment [Pipeline] sh + echo Tests passed on green environment Tests passed on green environment [Pipeline] echo Switching traffic from blue to green environment [Pipeline] sh + echo Traffic switched to green environment Traffic switched to green environment [Pipeline] echo Deployment successful! Blue-green switch complete. [Pipeline] End of Pipeline Finished: SUCCESS
-s - Waits for the job to finish and shows the console output
Checks the HTTP response headers from the live app URL to confirm the green environment is serving traffic.
Terminal
curl -I http://myapp.example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Type: text/html Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: run two identical environments and switch user traffic to the new one only after testing it.

Common Mistakes
Deploying the new version directly to the live environment without a separate green environment.
This causes downtime or errors for users during deployment.
Always deploy to a separate green environment first, test it, then switch traffic.
Switching traffic to green environment before running tests.
If the new version has bugs, users will experience failures immediately.
Run thorough tests on the green environment before switching traffic.
Not having a rollback plan to switch back to blue if green fails.
You cannot quickly recover from a bad deployment, causing prolonged downtime.
Keep the blue environment intact until green is confirmed stable, so you can switch back if needed.
Summary
Create a Jenkins pipeline that deploys the new app version to a green environment.
Test the green environment to ensure the new version works correctly.
Switch user traffic from the blue environment to the green environment after successful tests.