📖 Scenario: You are a developer who wants to automate your software build process using Jenkins. You will create a Jenkinsfile that defines a simple pipeline with stages for building and testing your code.
🎯 Goal: Build a Jenkins pipeline using a Jenkinsfile that has two stages: Build and Test. Each stage will print a message to show it is running.
📋 What You'll Learn
Create a Jenkinsfile with a pipeline block
Add an agent to run the pipeline on any available node
Add a Build stage that prints 'Building the project...'
Add a Test stage that prints 'Running tests...'
Print a final message 'Pipeline completed!' after the stages
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software builds and tests, saving time and reducing errors in real projects.
💼 Career
Knowing how to write Jenkinsfiles is essential for DevOps roles to create reliable CI/CD pipelines.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Create a Jenkinsfile with a pipeline block and add an agent any line inside it.
Jenkins
Hint
The pipeline block is the main container. The agent any means Jenkins can run this pipeline on any available machine.
2
Add the Build stage
Inside the pipeline block, add a stages block. Inside stages, add a stage named Build that runs a steps block with a echo command printing 'Building the project...'.
Jenkins
Hint
The stages block holds all the stages. Each stage has a name and steps to run commands.
3
Add the Test stage
Inside the existing stages block, add another stage named Test with a steps block that runs echo 'Running tests...'.
Jenkins
Hint
Add a new stage inside stages just like the Build stage.
4
Print a final completion message
Add a post block inside the pipeline block with a always section that runs a steps block. Inside steps, add echo 'Pipeline completed!' to print the final message.
Jenkins
Hint
The post block runs actions after all stages finish. The always section runs no matter what.
Practice
(1/5)
1. What is the main purpose of a Jenkinsfile in Jenkins?
easy
A. To configure Jenkins server settings
B. To define the steps and stages of a build process in code
C. To store Jenkins user credentials securely
D. To create a backup of Jenkins jobs
Solution
Step 1: Understand Jenkinsfile role
A Jenkinsfile is a text file that defines the build process in Jenkins using code.
Step 2: Compare options with Jenkinsfile purpose
Only To define the steps and stages of a build process in code describes defining build steps and stages, which is the Jenkinsfile's main role.
Final Answer:
To define the steps and stages of a build process in code -> Option B
Quick Check:
Jenkinsfile = build process code [OK]
Hint: Jenkinsfile scripts the build steps in code [OK]
Common Mistakes:
Confusing Jenkinsfile with Jenkins server config
Thinking Jenkinsfile stores credentials
Assuming Jenkinsfile is for backup
2. Which of the following is the correct way to start a declarative pipeline in a Jenkinsfile?
easy
A. pipeline {
B. stage {
C. node {
D. build {
Solution
Step 1: Recall declarative pipeline syntax
A declarative pipeline always starts with the keyword pipeline { to define the pipeline block.
Step 2: Check other options
stage { defines a stage inside the pipeline, node { is used in scripted pipelines, and build { is not a valid top-level block.
Final Answer:
pipeline { -> Option A
Quick Check:
Declarative pipeline starts with pipeline { [OK]
Hint: Declarative pipelines always start with 'pipeline {' [OK]
Common Mistakes:
Starting with stage instead of pipeline
Using node block in declarative pipeline
Using invalid block names like build
3. Given this Jenkinsfile snippet, what will be the output in the console?
The pipeline has one stage named 'Example' with a step that echoes 'Hello, Jenkins!'.
Step 2: Understand echo step behavior
The echo step prints the given message to the Jenkins console output during the build.
Final Answer:
Hello, Jenkins! -> Option C
Quick Check:
echo prints message = Hello, Jenkins! [OK]
Hint: echo prints text to console output [OK]
Common Mistakes:
Assuming syntax error without checking code
Thinking pipeline skips stages by default
Confusing echo with failure
4. Identify the error in this Jenkinsfile snippet:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
}
post {
always {
echo 'Build finished'
}
}
}
medium
A. sh step requires double quotes instead of single quotes
B. Incorrect placement of post block outside pipeline
C. Missing closing brace for stages block
D. No error, syntax is correct
Solution
Step 1: Check block structure
All blocks (pipeline, stages, stage, steps, post) are properly opened and closed.
Step 2: Validate syntax rules
The post block is correctly placed inside the pipeline block. The sh step accepts single or double quotes.
Final Answer:
No error, syntax is correct -> Option D
Quick Check:
Proper block structure = no syntax error [OK]
Hint: Check matching braces and block placement [OK]
Common Mistakes:
Assuming post block must be inside stages
Thinking single quotes cause error in sh step
Missing braces in nested blocks
5. You want to add a stage in your Jenkinsfile that only runs tests if the branch is 'main'. Which snippet correctly implements this conditional stage?
hard
A. stage('Test') {
when {
branch 'main'
}
steps {
echo 'Running tests'
}
}
B. stage('Test') {
if (env.BRANCH_NAME == 'main') {
steps {
echo 'Running tests'
}
}
}
C. stage('Test') {
steps {
if (branch == 'main') {
echo 'Running tests'
}
}
}
D. stage('Test') {
when {
environment 'main'
}
steps {
echo 'Running tests'
}
}
Solution
Step 1: Understand Jenkins conditional syntax
Declarative pipelines use the when block with branch 'branchName' to conditionally run stages.
Step 2: Evaluate each option
stage('Test') {
when {
branch 'main'
}
steps {
echo 'Running tests'
}
} correctly uses when { branch 'main' }. stage('Test') {
if (env.BRANCH_NAME == 'main') {
steps {
echo 'Running tests'
}
}
} uses scripted style inside declarative, which is invalid. stage('Test') {
steps {
if (branch == 'main') {
echo 'Running tests'
}
}
} tries to use if inside steps, which is not supported. stage('Test') {
when {
environment 'main'
}
steps {
echo 'Running tests'
}
} uses environment incorrectly.
Final Answer:
stage('Test') { when { branch 'main' } steps { echo 'Running tests' } } -> Option A
Quick Check:
Use when { branch 'name' } for conditional stages [OK]
Hint: Use when { branch 'main' } to run stage conditionally [OK]
Common Mistakes:
Using if statements inside declarative stages
Misusing environment instead of branch in when
Placing condition inside steps block
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.