0
0
Jenkinsdevops~10 mins

Idempotent pipeline steps in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Idempotent pipeline steps
Start Pipeline Step
Check if Step Already Done?
YesSkip Step
No
Execute Step Actions
Mark Step as Done
End Step
The pipeline step first checks if it has already run successfully. If yes, it skips running again. If no, it runs and marks itself done to avoid repeats.
Execution Sample
Jenkins
stage('Build') {
  steps {
    script {
      if (!fileExists('build.done')) {
        sh 'make build'
        writeFile file: 'build.done', text: 'done'
      } else {
        echo 'Build step skipped'
      }
    }
  }
}
This Jenkins pipeline stage runs the build only if a marker file does not exist, ensuring idempotency.
Process Table
StepCheck ConditionCondition ResultAction TakenOutput
1Does 'build.done' file exist?NoRun 'make build' and create 'build.done'Build runs, 'build.done' created
2Does 'build.done' file exist?YesSkip build stepOutput: 'Build step skipped'
3Pipeline ends--Pipeline completes without re-running build
💡 Step is skipped when 'build.done' file exists, ensuring the build runs only once.
Status Tracker
VariableStartAfter Step 1After Step 2Final
build.done fileDoes not existCreatedExistsExists
Key Moments - 3 Insights
Why does the pipeline check for 'build.done' before running the build?
To avoid running the build multiple times unnecessarily. The execution_table row 1 shows the build runs only if 'build.done' is missing.
What happens if the 'build.done' file is accidentally deleted after the first run?
The pipeline will run the build again because the check in execution_table row 1 will be false, causing the build to execute again.
Is the build step guaranteed to run only once ever?
No, it runs once per pipeline workspace state. If the marker file is removed or workspace cleaned, the build runs again as shown in execution_table rows 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what action is taken when the 'build.done' file exists?
ADelete 'build.done' file
BRun 'make build' again
CSkip the build step
DRestart the pipeline
💡 Hint
Refer to execution_table row 2 under 'Action Taken'
At which step does the pipeline create the 'build.done' file?
AStep 3
BStep 1
CStep 2
DIt never creates it
💡 Hint
Check execution_table row 1 'Action Taken' column
If the 'build.done' file is deleted after step 1, what will happen on the next pipeline run?
AThe build step will run again
BThe build step will be skipped
CThe pipeline will fail
DThe pipeline will exit immediately
💡 Hint
See key_moments explanation about file deletion and execution_table row 1 condition
Concept Snapshot
Idempotent pipeline steps check if their work is already done before running.
Use a marker (like a file) to track completion.
If marker exists, skip the step to save time.
If not, run the step and create the marker.
This avoids repeating costly or unsafe actions.
Full Transcript
In Jenkins pipelines, idempotent steps avoid repeating work by checking a marker like a file. If the marker exists, the step is skipped. If not, the step runs and creates the marker. This ensures the step runs only once per workspace state, saving time and preventing errors. The example shows a build step that runs only if 'build.done' file is missing. If the file exists, the build is skipped. If the file is deleted, the build runs again. This pattern helps pipelines be safe and efficient.