📖 Scenario: You are setting up a Jenkins pipeline to automate a simple build process. Jenkins uses node blocks to allocate an executor and workspace, and stage blocks to organize the pipeline into steps.This project will guide you to create a Jenkins pipeline script with one node block and two stage blocks: one for building and one for testing.
🎯 Goal: Build a Jenkins pipeline script that uses a node block to allocate the workspace and contains two stage blocks named Build and Test. Each stage should echo a message indicating the current stage.
📋 What You'll Learn
Create a node block in the Jenkins pipeline script
Inside the node block, create two stage blocks named Build and Test
Inside each stage, use echo to print a message indicating the stage name
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software build, test, and deployment processes in real projects.
💼 Career
Understanding <code>node</code> and <code>stage</code> blocks is essential for DevOps engineers and automation specialists working with Jenkins.
Progress0 / 4 steps
1
Create a node block
Write a Jenkins pipeline script that starts with a node block. Inside the node block, add a comment // Pipeline steps will go here.
Jenkins
Hint
The node block allocates an executor and workspace for your pipeline.
2
Add a stage block named Build
Inside the existing node block, add a stage block with the name Build. Inside this stage, add an echo statement that prints "Building the project...".
Jenkins
Hint
The stage block groups steps and the echo command prints messages in the Jenkins console.
3
Add a stage block named Test
Still inside the node block, after the Build stage, add another stage block named Test. Inside this stage, add an echo statement that prints "Running tests...".
Jenkins
Hint
Add the Test stage after the Build stage inside the node block.
4
Print the pipeline completion message
After the Test stage, still inside the node block, add an echo statement that prints "Pipeline completed successfully.".
Jenkins
Hint
The final echo prints a message after all stages finish.
Practice
(1/5)
1. What is the main purpose of a node block in a Jenkins pipeline?
easy
A. To declare environment variables for the pipeline
B. To define a named step grouping in the pipeline
C. To specify the machine or agent where the pipeline steps run
D. To send notifications after the pipeline finishes
Solution
Step 1: Understand the role of node block
The node block tells Jenkins which machine or agent will run the enclosed commands.
Step 2: Differentiate from stage block
The stage block is for grouping steps with a name, not for selecting the machine.
Final Answer:
To specify the machine or agent where the pipeline steps run -> Option C
Quick Check:
Node block = machine selection [OK]
Hint: Node block = where commands run, stage block = what step runs [OK]
Common Mistakes:
Confusing node with stage blocks
Thinking node defines step names
Assuming node sets environment variables
2. Which of the following is the correct syntax to define a stage inside a Jenkins pipeline?
easy
A. stage: Build { echo 'Building...' }
B. node('Build') { echo 'Building...' }
C. steps { stage('Build') { echo 'Building...' } }
D. stage('Build') { steps { echo 'Building...' } }
Solution
Step 1: Recall correct stage syntax
The correct syntax uses stage('name') { steps { ... } } to define a stage with steps inside.
Step 2: Check each option
stage('Build') { steps { echo 'Building...' } } matches the correct syntax. Options A, B, and D misuse keywords or structure.
A. The node block is incorrectly placed inside stage
B. Missing agent inside the stage block
C. The steps block should be outside node
D. The echo command is invalid inside steps
Solution
Step 1: Review pipeline block structure
Declarative pipelines require agent to specify where stages run; node is not valid inside stage in declarative syntax.
Step 2: Identify correct usage
Instead of node, the stage should have an agent block or rely on global agent any.
Final Answer:
The node block is incorrectly placed inside stage -> Option A
Quick Check:
Declarative stage needs agent, not node [OK]
Hint: Use agent, not node, inside declarative stage blocks [OK]
Common Mistakes:
Using node block inside declarative stage
Confusing steps placement
Thinking echo is invalid inside steps
5. You want to run two stages in your Jenkins pipeline: 'Build' on a Linux node and 'Test' on a Windows node. Which pipeline snippet correctly uses node and stage blocks to achieve this?