📖 Scenario: You are working as a DevOps engineer in a small software team. Your team wants to automate the process of building and testing their code whenever a developer makes changes. You will use Jenkins, a popular automation tool, to create a simple pipeline that runs a build and test job automatically.
🎯 Goal: Build a basic Jenkins pipeline script that defines a job with stages for building and testing code. This pipeline will help your team automate their software delivery process.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Define an agent to run the pipeline
Add two stages: Build and Test
In the Build stage, run a shell command to print 'Building the project...'
In the Test stage, run a shell command to print 'Running tests...'
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software builds, tests, and deployments, saving time and reducing errors.
💼 Career
Understanding Jenkins pipelines is essential for DevOps roles to implement continuous integration and continuous delivery.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline {} and inside it, add an agent any block to specify that the pipeline can run on any available agent.
Jenkins
Hint
Start with pipeline {} and add agent any inside it.
2
Add the Build stage
Inside the pipeline block, add a stages block. Then add a stage named Build that runs a shell command echo 'Building the project...' inside a steps block.
Jenkins
Hint
Use stages {} to group stages, then add stage('Build') { steps { sh "echo 'Building the project...'" } }.
3
Add the Test stage
Inside the existing stages block, add another stage named Test. In this stage, add a steps block that runs the shell command echo 'Running tests...'.
A. Multiple 'stages' blocks are not allowed in one pipeline
B. Missing 'agent' declaration
C. Echo commands are invalid in Jenkinsfile
D. Pipeline must have at least three stages
Solution
Step 1: Review pipeline structure rules
A Jenkins pipeline can have only one 'stages' block containing multiple 'stage' blocks.
Step 2: Identify error in given pipeline
Here, there are two separate 'stages' blocks, which is invalid syntax.
Final Answer:
Multiple 'stages' blocks are not allowed in one pipeline -> Option A
Quick Check:
Only one 'stages' block per pipeline = B [OK]
Hint: Only one 'stages' block allowed in Jenkinsfile [OK]
Common Mistakes:
Thinking multiple 'stages' blocks are valid
Confusing missing agent with stages error
Assuming echo commands cause errors
5. You want to create a Jenkins pipeline that builds your project only if the branch is 'main' and runs tests on all branches. Which pipeline snippet correctly implements this logic?