#
Handling Jenkins Pipeline Failures
This tutorial explains how the failures could be handled.
First of all I want to highlight that a prerequisite for this article is Create Declarative Pipeline tutorial.
Now we modify the Jenkinsfile
in GitHub. The new Jenkinsfile
will be like this:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building ...'
bat "dir"
}
}
}
post {
always {
echo 'Always do this.'
}
success {
echo 'Do this on success.'
}
failure {
echo 'Do this on FAILURE.'
}
}
}
We run the job, and we will see the following in the Console Output:
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Building ...
[Pipeline] bat
C:\ProgramData\Jenkins\.jenkins\workspace\DeclarativePipeline>dir
Volume in drive C has no label.
Volume Serial Number is 82BA-7949
Directory of C:\ProgramData\Jenkins\.jenkins\workspace\DeclarativePipeline
01/26/2023 11:28 PM <DIR> .
01/26/2023 11:28 PM <DIR> ..
01/26/2023 11:28 PM 414 Jenkinsfile
1 File(s) 414 bytes
2 Dir(s) 240,220,401,664 bytes free
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Always do this.
[Pipeline] echo
Do this on success.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
We modify Jenkinsfile
, and instead bat "dir"
we put sh "dir"
. This will make the pipeline fail.
We run the job again, and we will see the following in the Console Output:
[Pipeline] { (Build)
[Pipeline] echo
Building ...
[Pipeline] sh
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
Always do this.
[Pipeline] echo
Do this on FAILURE.
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessImpl.create(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:420)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:151)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1107)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)
at hudson.Proc$LocalProc.<init>(Proc.java:254)
at hudson.Proc$LocalProc.<init>(Proc.java:223)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:997)
....
at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:70)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
Finished: FAILURE
Info
Declarative Pipeline supports robust failure handling by default via its post section which allows
declaring a number of different "post conditions" such as: always
, unstable
, success
, failure
, and changed
.