📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process for a software project. This pipeline will have stages to prepare, build, and test the project.
🎯 Goal: Build a Jenkins declarative pipeline script with three stages: Prepare, Build, and Test. Each stage will run a simple shell command to simulate the step.
📋 What You'll Learn
Use declarative pipeline syntax starting with pipeline block
Define an agent any to run the pipeline on any available agent
Create three stages named Prepare, Build, and Test
In each stage, run a shell command using sh step with exact commands provided
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software build, test, and deployment processes to save time and reduce errors.
💼 Career
Understanding declarative pipelines is essential for DevOps engineers and automation specialists working with Jenkins.
Progress0 / 4 steps
1
Create the pipeline block with agent
Write a Jenkins declarative pipeline script starting with pipeline block and add agent any inside it.
Jenkins
Hint
The pipeline block is the root of the script. Inside it, add agent any to specify the pipeline runs on any available agent.
2
Add the Prepare stage with shell command
Inside the pipeline block, add a stages block. Then add a stage named Prepare that runs the shell command echo Preparing environment using sh step.
Jenkins
Hint
Use stages block to group stages. Each stage has a steps block where you run commands with sh.
3
Add Build and Test stages with shell commands
Inside the existing stages block, add two more stages named Build and Test. In Build, run sh 'echo Building project'. In Test, run sh 'echo Running tests'.
Jenkins
Hint
Add multiple stage blocks inside stages. Each stage has its own steps with shell commands.
4
Print the pipeline script to verify
Print the entire Jenkins pipeline script to verify it contains all stages and commands.
Jenkins
Hint
Use println to show a confirmation message that the pipeline script is complete.
Practice
(1/5)
1. Which block is mandatory to start a Jenkins declarative pipeline?
easy
A. stage
B. agent
C. steps
D. pipeline
Solution
Step 1: Understand the pipeline structure
The pipeline block is the root block that wraps the entire declarative pipeline script.
Step 2: Identify mandatory blocks
While agent, stages, and steps are important, the pipeline block is always required to define the pipeline.
Final Answer:
<code>pipeline</code> -> Option D
Quick Check:
Root block = pipeline [OK]
Hint: Look for the root block that wraps everything [OK]
Common Mistakes:
Confusing stage as root block
Thinking steps is the top block
Assuming agent is mandatory alone
2. Which of the following is the correct syntax to define an agent that runs on any available node?
easy
A. agent any
B. agent: any
C. agent { any }
D. agent = any
Solution
Step 1: Recall agent syntax in declarative pipeline
The correct syntax to specify an agent that runs on any available node is agent any without punctuation.
Step 2: Check other options for syntax errors
Options with colons, braces, or equals signs are invalid in declarative pipeline syntax for agent.
Final Answer:
agent any -> Option A
Quick Check:
Agent syntax is simple: agent any [OK]
Hint: Remember: no punctuation, just 'agent any' [OK]
Common Mistakes:
Adding colons or braces around 'any'
Using equals sign instead of space
Confusing scripted pipeline syntax
3. What will be the output of this Jenkins declarative pipeline snippet?