0
0
Jenkinsdevops~10 mins

Node and stage blocks in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Node and stage blocks
Start Pipeline
Enter node block
Enter stage block
Execute steps inside stage
Exit stage block
More stages?
YesEnter next stage block
No
Exit node block
Pipeline ends
The pipeline starts, enters a node block to allocate an agent, then runs one or more stage blocks sequentially, executing steps inside each stage, then exits the node and ends.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps { echo 'Building...' }
    }
  }
}
This pipeline runs on any available agent, enters a 'Build' stage, and prints 'Building...'.
Process Table
StepBlock EnteredActionOutputNext Step
1pipelineStart pipeline executionPipeline startedEnter node block
2nodeAllocate agentAgent allocatedEnter stage 'Build'
3stage 'Build'Run stepsPrints 'Building...'Exit stage 'Build'
4stage 'Build'Exit stage blockStage 'Build' completedCheck for more stages
5nodeExit node blockAgent releasedPipeline ends
6pipelineEnd pipelinePipeline finishedStop execution
💡 Pipeline ends after all stages run and node block exits
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
agentAllocatedfalsetruetruefalsefalse
currentStagenoneBuildBuildnonenone
pipelineStatusnot startedrunningrunningrunningfinished
Key Moments - 3 Insights
Why do we need a node block before stages?
The node block allocates an agent (a machine) where the stages run. Without it, the pipeline has no place to execute commands. See execution_table step 2 where agent allocation happens before stages.
Can stages run outside a node block?
No, stages must be inside a node block to run on an agent. The execution flow shows stages inside the node block (steps 3 and 4).
What happens after all stages complete?
After all stages finish, the node block exits releasing the agent (step 5), then the pipeline ends (step 6). This ensures resources are freed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the agent allocated?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Check the 'Action' column for 'Allocate agent' in the execution_table.
At which step does the pipeline print 'Building...'?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for 'Run steps' and output 'Prints Building...' in execution_table.
If we add another stage after 'Build', which step would change?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Adding stages affects the 'Check for more stages' step in execution_table.
Concept Snapshot
pipeline {
  agent any
  stages {
    stage('Name') {
      steps { ... }
    }
  }
}

- node block allocates agent
- stages run inside node
- steps execute commands
- pipeline ends after stages
Full Transcript
This visual execution shows how a Jenkins pipeline runs using node and stage blocks. First, the pipeline starts and enters a node block to allocate an agent machine. Then it enters a stage block named 'Build' and runs the steps inside, printing 'Building...'. After the stage completes, the pipeline checks for more stages. If none remain, it exits the node block, releasing the agent, and ends the pipeline. Variables like agentAllocated and currentStage track the state changes. Key points include that stages must run inside a node block to have an agent, and the pipeline ends only after all stages and node exit. The quizzes test understanding of when agent allocation and stage execution happen, and how adding stages affects the flow.