📖 Scenario: You work as a DevOps engineer managing a web application deployment. To avoid downtime during updates, you want to use the blue-green deployment pattern. This means you have two identical environments: blue and green. Only one environment serves live traffic at a time. When deploying a new version, you update the inactive environment, then switch traffic to it.
🎯 Goal: Build a Jenkins pipeline script that defines two environments blue and green, sets the active environment, deploys the new version to the inactive environment, and switches traffic to it.
📋 What You'll Learn
Create a variable environments with values ['blue', 'green']
Create a variable activeEnv set to 'blue'
Write a function getInactiveEnv that returns the environment not currently active
Print the inactive environment, deploy to it, then switch activeEnv to it
Print the new active environment after switching
💡 Why This Matters
🌍 Real World
Blue-green deployment is used in real companies to update applications without downtime by switching traffic between two identical environments.
💼 Career
Understanding this pattern helps DevOps engineers automate safe deployments and reduce risks during updates.
Progress0 / 4 steps
1
Define environments list
Create a variable called environments and set it to the list ['blue', 'green'].
Jenkins
Hint
Use square brackets to create a list with two strings: 'blue' and 'green'.
2
Set active environment
Create a variable called activeEnv and set it to the string 'blue'.
Jenkins
Hint
Assign the string 'blue' to the variable activeEnv.
3
Create function to get inactive environment
Write a function called getInactiveEnv that returns the environment from environments that is not equal to activeEnv. Use a for loop with variable env to check each environment.
Jenkins
Hint
Loop over environments and return the one not equal to activeEnv.
4
Deploy to inactive environment and switch active environment
Call getInactiveEnv() and store the result in inactiveEnv. Print "Deploying to {inactiveEnv} environment". Then set activeEnv to inactiveEnv. Finally, print "Switched active environment to {activeEnv}".
Jenkins
Hint
Use print statements with string formatting to show deployment and switching messages.
Practice
(1/5)
1. What is the main goal of the blue-green deployment pattern in Jenkins?
easy
A. To deploy only on weekends
B. To run tests faster by using multiple agents
C. To backup data before deployment
D. To avoid downtime by switching between two identical environments
Solution
Step 1: Understand blue-green deployment concept
This pattern uses two identical environments called blue and green to deploy new versions without downtime.
Step 2: Identify the main goal in Jenkins context
Jenkins automates switching traffic between these environments to keep the service available during updates.
Final Answer:
To avoid downtime by switching between two identical environments -> Option D
Quick Check:
Blue-green deployment = avoid downtime [OK]
Hint: Blue-green means two environments for zero downtime [OK]
Common Mistakes:
Confusing blue-green with backup strategies
Thinking it speeds up tests
Assuming deployment timing is the main goal
2. Which Jenkins pipeline step correctly switches traffic from the blue environment to the green environment?
easy
A. sh 'kubectl patch service myapp -p "{\"spec\":{\"selector\":{\"env\":\"green\"}}}'
B. sh 'kubectl apply -f green-service.yaml'
C. sh 'kubectl scale deployment blue --replicas=0'
D. sh 'kubectl delete pod blue-pod-1'
Solution
Step 1: Understand traffic switching in Kubernetes
Switching traffic means updating the service selector to point to the new environment pods.
Step 2: Identify the correct kubectl command
The patch command changes the service selector to 'green', redirecting traffic from blue to green.
Final Answer:
sh 'kubectl patch service myapp -p "{\"spec\":{\"selector\":{\"env\":\"green\"}}}' -> Option A
Quick Check:
Patch service selector = switch traffic [OK]
Hint: Patch service selector to switch environments [OK]
Common Mistakes:
Scaling down blue does not switch traffic immediately
Applying green service config alone doesn't switch traffic
Deleting pods causes downtime
3. Given this Jenkins pipeline snippet, what will be the output if the green environment is healthy?
stage('Deploy Green') {
steps {
sh 'kubectl apply -f green-deployment.yaml'
sh 'kubectl rollout status deployment/green'
}}
stage('Switch Traffic') {
steps {
sh 'kubectl patch service myapp -p "{\"spec\":{\"selector\":{\"env\":\"green\"}}}'
}}
medium
A. Traffic remains on blue environment
B. Deployment fails because green environment is missing
C. Deployment applies and traffic switches to green with no errors
D. Rollout status command causes syntax error
Solution
Step 1: Analyze deployment commands
The green deployment is applied and rollout status waits for it to be ready.
Step 2: Analyze traffic switch command
The patch command updates the service selector to green, switching traffic successfully.
Final Answer:
Deployment applies and traffic switches to green with no errors -> Option C
Quick Check:
Healthy green + patch service = success [OK]
Hint: Healthy rollout then patch service switches traffic [OK]
Common Mistakes:
Assuming rollout status causes errors
Thinking traffic stays on blue after patch
Confusing deployment apply with service patch
4. You have a Jenkins pipeline that deploys to green, but users report downtime after switching traffic. What is the most likely cause?
medium
A. The Jenkinsfile syntax is incorrect
B. The green deployment rollout was not completed before switching traffic
C. The blue environment was deleted before switching
D. The service selector was not patched to green
Solution
Step 1: Understand downtime cause in blue-green
Downtime happens if traffic switches before the new environment is ready.
Step 2: Identify pipeline mistake
If rollout status is skipped or incomplete, green pods may not be ready, causing downtime.
Final Answer:
The green deployment rollout was not completed before switching traffic -> Option B
Quick Check:
Incomplete rollout before switch = downtime [OK]
Hint: Wait for rollout status before switching traffic [OK]
Common Mistakes:
Deleting blue early does not cause downtime if green is ready
Syntax errors cause pipeline failure, not downtime
Not patching service keeps old traffic, no downtime
5. In a Jenkins pipeline implementing blue-green deployment, how can you safely clean up the old environment after switching traffic to green?
hard
A. Scale down the blue deployment to zero replicas after verifying green is stable
B. Delete the blue deployment immediately after patching the service
C. Keep both blue and green running indefinitely
D. Restart Jenkins to clear old environment
Solution
Step 1: Understand cleanup in blue-green deployment
After switching traffic, the old environment should be scaled down to free resources but kept until stability is confirmed.
Step 2: Identify safe cleanup method in Jenkins pipeline
Scaling down blue to zero replicas after green is stable avoids downtime and allows rollback if needed.
Final Answer:
Scale down the blue deployment to zero replicas after verifying green is stable -> Option A
Quick Check:
Scale down old env after stable new env [OK]
Hint: Scale down old env only after new env is stable [OK]
Common Mistakes:
Deleting old deployment immediately risks downtime
Keeping both running wastes resources
Restarting Jenkins does not affect deployments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.