0
0
Jenkinsdevops~10 mins

Groovy syntax in pipelines in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Groovy syntax in pipelines
Start Pipeline Script
Define Variables & Functions
Use Steps & Conditions
Execute Pipeline Stages
Handle Success or Failure
End Pipeline
This flow shows how a Jenkins pipeline script using Groovy syntax starts, defines variables and functions, executes steps and stages, handles conditions, and ends.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Example') {
      steps {
        echo "Hello, Groovy!"
      }
    }
  }
}
A simple Jenkins pipeline script that runs a stage named 'Example' and prints a message.
Process Table
StepActionGroovy Code EvaluatedResult/Output
1Start pipelinepipeline { ... }Pipeline initialized
2Set agentagent anyAgent set to any available node
3Enter stages blockstages { ... }Stages block ready
4Enter stage 'Example'stage('Example') { ... }Stage 'Example' started
5Execute stepssteps { echo "Hello, Groovy!" }Prints: Hello, Groovy!
6Complete stage}Stage 'Example' completed
7Complete pipeline}Pipeline finished successfully
💡 Pipeline script completes after all stages and steps run successfully
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
agentundefinedanyanyanyany
stageNameundefinedundefinedExampleExampleExample
echoOutputundefinedundefinedundefinedHello, Groovy!Hello, Groovy!
Key Moments - 3 Insights
Why do we use 'agent any' in the pipeline?
The 'agent any' line tells Jenkins to run the pipeline on any available worker node. This is shown in execution_table step 2 where the agent is set.
What does the 'echo' step do inside the steps block?
The 'echo' step prints a message to the console output. In execution_table step 5, it prints 'Hello, Groovy!'. This is how you output text in a pipeline.
Why is the pipeline wrapped inside 'pipeline { ... }'?
The 'pipeline { ... }' block defines the entire pipeline script structure. It organizes the agent, stages, and steps. This is the root block shown in execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output at step 5?
APipeline initialized
BPrints: Hello, Groovy!
CStage 'Example' started
DAgent set to any available node
💡 Hint
Check the 'Result/Output' column for step 5 in the execution_table.
At which step does the pipeline set the agent to 'any'?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Look at the 'Action' and 'Groovy Code Evaluated' columns in execution_table for agent setting.
If we add another stage before 'Example', how would the execution_table change?
AThere would be additional steps for the new stage before step 4
BThe pipeline would fail to start
CThe 'agent any' would move to step 5
DThe echo output would change to the new stage name
💡 Hint
Adding a stage adds new steps in the stages block before existing stages, reflected in execution_table steps.
Concept Snapshot
Groovy syntax in Jenkins pipelines:
- Use 'pipeline { agent any stages { stage('name') { steps { ... } } } }'
- 'agent any' runs on any node
- 'stage' groups steps
- 'steps' contain commands like 'echo'
- Script runs top to bottom
- Use simple Groovy expressions inside steps
Full Transcript
This visual execution shows a Jenkins pipeline script using Groovy syntax. The pipeline starts with the 'pipeline' block, sets the agent to 'any' node, defines stages, and runs steps inside stages. The example prints 'Hello, Groovy!' using the echo step. Variables like agent and stageName track the current state. Key moments clarify why 'agent any' is used, what echo does, and the purpose of the pipeline block. The quiz tests understanding of step outputs, agent setting, and adding stages.