📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process for a software project. The build process has multiple steps that need to run in order.
🎯 Goal: Build a Jenkins pipeline script that defines three build steps: checkout, build, and test. Each step should print a message indicating it is running.
📋 What You'll Learn
Create a Jenkins pipeline script using the pipeline and stages syntax
Define three stages named Checkout, Build, and Test
Each stage should have a steps block that prints a message using echo
The messages must be exactly: 'Checking out code...', 'Building the project...', and 'Running tests...'
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software builds, tests, and deployments in many companies to save time and reduce errors.
💼 Career
Understanding build steps execution is essential for DevOps engineers and automation specialists working with CI/CD tools like Jenkins.
Progress0 / 4 steps
1
Create the Jenkins pipeline skeleton
Write the initial Jenkins pipeline script with a pipeline block and an empty stages block.
Jenkins
Hint
Start by writing pipeline { then specify agent any and add an empty stages { } block inside.
2
Add the Checkout stage
Inside the stages block, add a stage named Checkout with a steps block that uses echo to print 'Checking out code...'.
Jenkins
Hint
Use stage('Checkout') and inside it add steps { echo 'Checking out code...' }.
3
Add the Build and Test stages
Add two more stages inside stages: Build and Test. Each should have a steps block that prints 'Building the project...' and 'Running tests...' respectively using echo.
Jenkins
Hint
Add stage('Build') and stage('Test') with steps { echo ... } inside each.
4
Run the pipeline and display output
Print the entire Jenkins pipeline script so it can be run and shows the three messages in order.
Jenkins
Hint
When you run this Jenkins pipeline, it will print the three messages in order during the build.
Practice
(1/5)
1. What is the main purpose of build steps in a Jenkins pipeline?
easy
A. To manage Jenkins plugins and updates
B. To create user accounts on the Jenkins server
C. To monitor server hardware performance
D. To run commands that compile, test, and package software automatically
Solution
Step 1: Understand what build steps do
Build steps are commands executed during a Jenkins pipeline to automate tasks.
Step 2: Identify the correct purpose
They help compile, test, and package software automatically as part of the build process.
Final Answer:
To run commands that compile, test, and package software automatically -> Option D
Quick Check:
Build steps = automate build tasks [OK]
Hint: Build steps automate software tasks in order [OK]
Common Mistakes:
Confusing build steps with server management
Thinking build steps create users
Assuming build steps monitor hardware
2. Which of the following is the correct syntax to add a shell build step in a Jenkins pipeline script?
easy
A. sh 'echo Hello World'
B. shell: echo Hello World
C. execute shell { echo Hello World }
D. run shell echo Hello World
Solution
Step 1: Recall Jenkins pipeline shell step syntax
In Jenkins scripted or declarative pipelines, shell commands run with the sh step and a string argument.
Step 2: Match the correct syntax
The correct syntax is sh 'command', so sh 'echo Hello World' is valid.
Final Answer:
sh 'echo Hello World' -> Option A
Quick Check:
Shell step uses sh 'command' [OK]
Hint: Use sh 'command' for shell steps in Jenkins [OK]
Common Mistakes:
Using incorrect keywords like shell: or execute shell
Omitting quotes around the command
Writing commands without the sh step
3. Given this Jenkins pipeline snippet, what will be the output in the console?
stage('Build') {
steps {
sh 'echo Start Build'
sh 'echo Build Complete'
}
}
medium
A. Start Build only
B. Build Complete\nStart Build
C. Start Build\nBuild Complete
D. No output, commands are skipped
Solution
Step 1: Understand step execution order
Jenkins runs build steps in the order they appear inside the stage.
5. You want to run two build steps in a Jenkins pipeline: first compile code, then run tests only if compile succeeds. Which pipeline snippet correctly implements this?
hard
A. stage('Build') {
steps {
sh 'compile.sh'
script {
if (currentBuild.result != 'FAILURE') {
sh 'test.sh'
}
}
}
}
B. stage('Build') {
steps {
sh 'compile.sh || true'
sh 'test.sh'
}
}
C. stage('Build') {
steps {
sh 'compile.sh'
}
}
stage('Test') {
steps {
sh 'test.sh'
}
}
Step 1: Understand conditional execution in Jenkins pipeline
To run tests only if compile succeeds, you need to check the build status before running tests.
Step 2: Analyze options
stage('Build') {
steps {
sh 'compile.sh'
script {
if (currentBuild.result != 'FAILURE') {
sh 'test.sh'
}
}
}
} uses a script block to check if the build is not failed before running tests, ensuring tests run only after successful compile.
Final Answer:
sh 'compile.sh'; script { if (currentBuild.result != 'FAILURE') { sh 'test.sh' } } -> Option A
Quick Check:
Conditional test run uses script and build status check [OK]
Hint: Use script block to conditionally run steps [OK]
Common Mistakes:
Running all steps unconditionally
Using shell || true which ignores compile failure
Splitting stages without controlling flow
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.