📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process. The pipeline has stages, and each stage contains steps that perform specific tasks.Think of stages as chapters in a book, and steps as the sentences that tell the story in each chapter.
🎯 Goal: Build a Jenkins pipeline script with one stage named Build that contains two steps: one to print a message and another to run a shell command.
📋 What You'll Learn
Create a Jenkins pipeline with a pipeline block
Add a stage named Build
Inside the Build stage, add two steps: one echo and one sh
The echo step must print exactly Starting build...
The sh step must run the command echo Build complete
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software builds and tests, making development faster and less error-prone.
💼 Career
Understanding pipeline stages and steps is essential for DevOps engineers to create reliable CI/CD workflows.
Progress0 / 4 steps
1
Create the pipeline and stage
Write a Jenkins pipeline script that starts with a pipeline block and contains one stage named Build. Use the agent any directive inside the pipeline.
Jenkins
Hint
Start with pipeline {}, add agent any, then add stages {} with one stage('Build') {} inside.
2
Add the echo step inside the Build stage
Inside the steps block of the Build stage, add an echo step that prints exactly Starting build....
Jenkins
Hint
Use echo 'Starting build...' inside the steps block.
3
Add the shell command step inside the Build stage
Add a sh step inside the steps block of the Build stage that runs the shell command echo Build complete.
Jenkins
Hint
Use sh 'echo Build complete' to run the shell command inside steps.
4
Print the pipeline script
Print the entire Jenkins pipeline script you wrote so far.
Jenkins
Hint
In Jenkins, the pipeline script is saved as a Jenkinsfile. Here, just ensure your final script matches the previous steps.
Practice
(1/5)
1. What is the main purpose of steps inside a Jenkins stage?
easy
A. To define the actual commands or actions Jenkins will execute
B. To name the stage in the pipeline
C. To set environment variables for the pipeline
D. To configure Jenkins plugins
Solution
Step 1: Understand the role of stages
Stages organize the pipeline into sections but do not run commands themselves.
Step 2: Identify what steps do inside stages
Steps contain the actual commands or scripts Jenkins executes during that stage.
Final Answer:
To define the actual commands or actions Jenkins will execute -> Option A
Quick Check:
Steps = commands inside stages [OK]
Hint: Steps run commands; stages just group them [OK]
Common Mistakes:
Confusing stage name with steps
Thinking steps configure plugins
Believing steps set environment variables
2. Which of the following is the correct syntax to define a step that prints 'Hello World' inside a Jenkins declarative pipeline stage?
easy
A. script { print 'Hello World' }
B. steps { echo 'Hello World' }
C. steps: echo('Hello World')
D. stage { echo 'Hello World' }
Solution
Step 1: Recall declarative pipeline syntax for steps
Inside a stage, steps are defined with the steps { ... } block.
Step 2: Check the correct command to print
The echo command prints messages in Jenkins pipelines.
Final Answer:
steps { echo 'Hello World' } -> Option B
Quick Check:
steps block + echo command = correct syntax [OK]
Hint: Use steps { echo 'text' } to print in Jenkins [OK]
Common Mistakes:
Using stage instead of steps for commands
Wrong colon syntax in steps
Using print instead of echo
3. Given this Jenkins pipeline snippet, what will be the output in the console?