📖 Scenario: You work as a DevOps engineer. Your team wants to automate software deployment to three environments: development, staging, and production. Each environment needs a separate step in the pipeline.This helps catch bugs early and safely release new features.
🎯 Goal: Create a Jenkins pipeline script that defines three stages: Dev, Staging, and Prod. Each stage should print a message showing deployment to that environment.This pipeline will help your team deploy code step-by-step.
📋 What You'll Learn
Create a Jenkins pipeline with three stages named exactly Dev, Staging, and Prod
Each stage must have a steps block that prints a message like Deploying to Dev environment
Use the declarative pipeline syntax with pipeline, agent any, and stages
Print the deployment message using echo inside each stage
💡 Why This Matters
🌍 Real World
Teams use Jenkins pipelines to automate software deployment to different environments. This reduces manual errors and speeds up delivery.
💼 Career
Knowing how to write multi-stage Jenkins pipelines is a key skill for DevOps engineers and automation specialists.
Progress0 / 4 steps
1
Create the Jenkins pipeline skeleton
Write the basic Jenkins pipeline structure with pipeline and agent any. Do not add any stages yet.
Jenkins
Hint
Start with pipeline { and inside it add agent any to run on any available agent.
2
Add the three deployment stages
Inside the pipeline, add a stages block with three stages named Dev, Staging, and Prod. Leave the steps blocks empty for now.
Jenkins
Hint
Use stages {} to group stages. Each stage uses stage('Name') { steps { } }.
3
Add echo commands to each stage
Inside each stage's steps block, add an echo command that prints exactly: Deploying to Dev environment in Dev stage, Deploying to Staging environment in Staging stage, and Deploying to Prod environment in Prod stage.
Jenkins
Hint
Use echo 'message' inside each steps block to print the deployment message.
4
Print the deployment messages
Run the pipeline and ensure it prints the deployment messages for Dev, Staging, and Prod stages in order.
Jenkins
Hint
Run the pipeline in Jenkins. The console output should show the deployment messages in order.
Practice
(1/5)
1. What is the main purpose of having separate dev, staging, and prod stages in a Jenkins deployment pipeline?
easy
A. To deploy only to production without checks
B. To run the same tests multiple times unnecessarily
C. To test and verify code in different environments before final release
D. To slow down the deployment process
Solution
Step 1: Understand pipeline stages
Each stage (dev, staging, prod) represents a different environment for testing and validation.
Step 2: Purpose of separate environments
Separate environments help catch issues early and ensure stability before production deployment.
Final Answer:
To test and verify code in different environments before final release -> Option C
Shell returns 'permission denied' error; stage fails and pipeline stops.
Final Answer:
The Staging stage will fail with a permission denied error -> Option B
Quick Check:
Missing execute permission = stage failure [OK]
Hint: Scripts must be executable before Jenkins runs them [OK]
Common Mistakes:
Assuming Jenkins fixes permissions automatically
Thinking stage will skip on error
Believing script runs without execute permission
5. You want to create a Jenkins pipeline that deploys to dev automatically, but requires manual approval before deploying to staging and prod. Which pipeline snippet correctly implements this?
hard
A. pipeline {
agent any
stages {
stage('Dev') {
steps { echo 'Deploying to dev' }
}
stage('Staging') {
steps {
input 'Approve Staging?'
echo 'Deploying to staging'
}
}
stage('Prod') {
steps {
input 'Approve Prod?'
echo 'Deploying to prod'
}
}
}
}
B. pipeline {
agent any
stages {
stage('Dev') {
steps { echo 'Deploying to dev' }
}
stage('Staging') {
steps { echo 'Deploying to staging' }
}
stage('Prod') {
steps { echo 'Deploying to prod' }
}
}
}
C. pipeline {
agent any
stages {
stage('Dev') {
steps {
input 'Approve Dev?'
echo 'Deploying to dev'
}
}
stage('Staging') {
steps { echo 'Deploying to staging' }
}
stage('Prod') {
steps { echo 'Deploying to prod' }
}
}
}
D. pipeline {
agent any
stages {
stage('Dev') {
steps { echo 'Deploying to dev' }
}
stage('Staging') {
steps {
input 'Approve Prod?'
echo 'Deploying to staging'
}
}
stage('Prod') {
steps {
input 'Approve Staging?'
echo 'Deploying to prod'
}
}
}
}
Solution
Step 1: Identify manual approval usage
Jenkins input step pauses pipeline for manual approval.
Step 2: Check stages with input
pipeline {
agent any
stages {
stage('Dev') {
steps { echo 'Deploying to dev' }
}
stage('Staging') {
steps {
input 'Approve Staging?'
echo 'Deploying to staging'
}
}
stage('Prod') {
steps {
input 'Approve Prod?'
echo 'Deploying to prod'
}
}
}
} uses input before staging and prod deployments, dev runs automatically.
Final Answer:
Pipeline with input steps before staging and prod deployments -> Option A
Quick Check:
Manual approval = input step before stage [OK]
Hint: Use input step to pause for approval in Jenkins [OK]
Common Mistakes:
Adding input to dev instead of staging/prod
Skipping input steps entirely
Mixing approval prompts between stages
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.