0
0
Jenkinsdevops~5 mins

Node and stage blocks in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you automate software builds and tests, you need to organize tasks clearly and decide where they run. Node blocks tell Jenkins which machine to use, and stage blocks divide the work into clear steps.
When you want to run your build on a specific machine or environment.
When you want to split your pipeline into clear steps like build, test, and deploy.
When you want to see progress and results for each step separately in Jenkins.
When you want to control resources by choosing which node runs which part of the pipeline.
When you want to make your pipeline easier to read and maintain.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the application'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying the application'
      }
    }
  }
}

This Jenkinsfile defines a pipeline with three stages: Build, Test, and Deploy.

The agent any means Jenkins can run this pipeline on any available node.

Each stage block groups related steps, making the pipeline organized and easy to follow.

Commands
Check that Jenkins CLI or Jenkins environment is ready to run pipelines.
Terminal
jenkins-jobs --version
Expected OutputExpected
2.319.1
Start the Jenkins pipeline named 'my-pipeline' which uses node and stage blocks to organize tasks.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins-agent-1 in /var/jenkins_home/workspace/my-pipeline [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Building the application [Pipeline] } [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo Running tests [Pipeline] } [Pipeline] stage [Pipeline] { (Deploy) [Pipeline] echo Deploying the application [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
View the console output of the pipeline to see the progress and results of each stage.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] node Running on Jenkins-agent-1 [Pipeline] stage [Pipeline] { (Build) Building the application [Pipeline] } [Pipeline] stage [Pipeline] { (Test) Running tests [Pipeline] } [Pipeline] stage [Pipeline] { (Deploy) Deploying the application [Pipeline] } [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: node blocks choose where to run, and stage blocks organize what to run in clear steps.

Common Mistakes
Not wrapping steps inside stage blocks
Without stage blocks, the pipeline lacks clear steps, making it hard to track progress and debug.
Always group related steps inside stage blocks to organize the pipeline.
Using node blocks incorrectly or missing them
Without node blocks, Jenkins doesn't know which machine to run the tasks on, causing pipeline failures.
Use node blocks to specify the agent or machine where the pipeline or stages run.
Using 'agent any' but expecting tasks to run on a specific machine
'agent any' lets Jenkins pick any available node, which may not have required tools or environment.
Specify a particular node label in the agent block if you need a specific machine.
Summary
Node blocks tell Jenkins which machine or agent to use for running tasks.
Stage blocks divide the pipeline into clear, named steps for better organization and tracking.
Using node and stage blocks together makes pipelines easier to read, maintain, and debug.