Bird
Raised Fist0
Jenkinsdevops~10 mins

Steps within stages in Jenkins - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Steps within stages
Start Pipeline
↓
Enter Stage
↓
Execute Step 1
↓
Execute Step 2
↓
Execute Last Step
↓
Exit Stage
↓
Next Stage or End Pipeline
The pipeline starts, enters a stage, runs each step in order, then exits the stage and moves on.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Compiling code'
        sh 'make build'
      }
    }
  }
}
This pipeline runs a 'Build' stage with two steps: printing a message and running a build command.
Process Table
StepActionCommand/MessageResult/Output
1Enter StageBuildStage 'Build' started
2Execute Stepecho 'Compiling code'Prints: Compiling code
3Execute Stepsh 'make build'Runs build command, outputs build logs
4Exit StageBuildStage 'Build' completed
💡 All steps in the 'Build' stage executed successfully, stage ends.
Status Tracker
VariableStartAfter Step 2After Step 3Final
Stage StatusNot startedRunningRunningCompleted
Build OutputNoneCompiling code printedBuild logs generatedBuild logs available
Key Moments - 2 Insights
Why do steps run one after another inside a stage?
Steps inside a stage run sequentially as shown in the execution_table rows 2 and 3, ensuring each command finishes before the next starts.
What happens if a step fails inside a stage?
If a step fails, the pipeline stops executing further steps in that stage, so the exit step (row 4) would not be reached.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after Step 2?
ARuns build command
BPrints: Compiling code
CStage 'Build' completed
DNo output yet
💡 Hint
Check the 'Result/Output' column for Step 2 in the execution_table.
At which step does the stage status change to 'Completed'?
AStep 2
BStep 3
CStep 4
DStart of pipeline
💡 Hint
Look at the variable_tracker row for 'Stage Status' and see when it becomes 'Completed'.
If the 'sh make build' command fails, what happens next?
AStage exits immediately without completing all steps
BStage restarts automatically
CPipeline continues to next step
DPipeline skips to next stage
💡 Hint
Refer to key_moments explanation about step failure stopping the stage.
Concept Snapshot
Jenkins pipeline stages contain steps.
Steps run one by one inside a stage.
Each step executes a command or action.
If a step fails, the stage stops.
Stages help organize pipeline tasks clearly.
Full Transcript
In Jenkins pipelines, a stage groups related steps. When the pipeline runs, it enters a stage and executes each step in order. For example, in the 'Build' stage, it first prints a message, then runs a build command. Each step must finish before the next starts. If any step fails, the stage stops immediately. After all steps complete, the stage ends and the pipeline moves on. This flow helps organize tasks and track progress clearly.

Practice

(1/5)
1. What is the main purpose of steps inside a Jenkins stage?
easy
A. To define the actual commands or actions Jenkins will execute
B. To name the stage in the pipeline
C. To set environment variables for the pipeline
D. To configure Jenkins plugins

Solution

  1. Step 1: Understand the role of stages

    Stages organize the pipeline into sections but do not run commands themselves.
  2. Step 2: Identify what steps do inside stages

    Steps contain the actual commands or scripts Jenkins executes during that stage.
  3. Final Answer:

    To define the actual commands or actions Jenkins will execute -> Option A
  4. Quick Check:

    Steps = commands inside stages [OK]
Hint: Steps run commands; stages just group them [OK]
Common Mistakes:
  • Confusing stage name with steps
  • Thinking steps configure plugins
  • Believing steps set environment variables
2. Which of the following is the correct syntax to define a step that prints 'Hello World' inside a Jenkins declarative pipeline stage?
easy
A. script { print 'Hello World' }
B. steps { echo 'Hello World' }
C. steps: echo('Hello World')
D. stage { echo 'Hello World' }

Solution

  1. Step 1: Recall declarative pipeline syntax for steps

    Inside a stage, steps are defined with the steps { ... } block.
  2. Step 2: Check the correct command to print

    The echo command prints messages in Jenkins pipelines.
  3. Final Answer:

    steps { echo 'Hello World' } -> Option B
  4. Quick Check:

    steps block + echo command = correct syntax [OK]
Hint: Use steps { echo 'text' } to print in Jenkins [OK]
Common Mistakes:
  • Using stage instead of steps for commands
  • Wrong colon syntax in steps
  • Using print instead of echo
3. Given this Jenkins pipeline snippet, what will be the output in the console?
stage('Build') {
  steps {
    echo 'Starting build'
    sh 'echo Build complete'
  }
}
medium
A. Syntax error in pipeline
B. Build complete\nStarting build
C. Starting build only
D. Starting build\nBuild complete

Solution

  1. Step 1: Understand the order of steps execution

    Steps run sequentially: first echo 'Starting build', then shell command sh 'echo Build complete'.
  2. Step 2: Determine console output

    The first prints 'Starting build', the shell prints 'Build complete', so output is both lines in order.
  3. Final Answer:

    Starting build\nBuild complete -> Option D
  4. Quick Check:

    Steps run in order, echo then shell output [OK]
Hint: Steps run top to bottom, echo then shell output [OK]
Common Mistakes:
  • Assuming shell output appears before echo
  • Thinking only echo prints
  • Confusing syntax causing errors
4. Identify the error in this Jenkins pipeline stage and how to fix it:
stage('Test') {
  steps
    echo 'Running tests'
}
medium
A. stage keyword should be inside steps
B. echo command is invalid; use print instead
C. Missing curly braces {} after steps; add steps { ... }
D. No error; pipeline is correct

Solution

  1. Step 1: Check syntax for steps block

    The steps block must have curly braces enclosing commands: steps { ... }.
  2. Step 2: Identify missing braces causing syntax error

    Here, steps is followed directly by echo without braces, which is invalid.
  3. Final Answer:

    Missing curly braces {} after steps; add steps { ... } -> Option C
  4. Quick Check:

    steps block requires braces { } [OK]
Hint: Always wrap steps commands inside steps { } [OK]
Common Mistakes:
  • Omitting braces after steps
  • Using print instead of echo
  • Placing stage inside steps
5. You want to run multiple shell commands sequentially inside a Jenkins stage. Which is the best way to organize these steps?
hard
A. Use a single sh step with commands separated by semicolons inside steps { }
B. Write multiple stage blocks, each with one sh command
C. Put all commands inside environment block
D. Use multiple steps blocks inside one stage

Solution

  1. Step 1: Understand how to run multiple shell commands

    Multiple commands can be run in one sh step by separating them with semicolons.
  2. Step 2: Identify correct pipeline structure

    Only one steps block is allowed per stage; multiple commands go inside one sh step or multiple steps inside the block.
  3. Final Answer:

    Use a single sh step with commands separated by semicolons inside steps { } -> Option A
  4. Quick Check:

    One steps block + one sh with multiple commands [OK]
Hint: Combine shell commands with semicolons inside one sh step [OK]
Common Mistakes:
  • Using multiple stages for commands
  • Trying multiple steps blocks inside one stage
  • Putting commands in environment block
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard