After reading Jenkins tutorial explaining Pipeline plug-in, it seems that plug-in should make it possible to implement Post-Build steps. However documentation is rather limited in regard to specific instructions.
For example I wonder how to implement:
- Run only if build succeeds
- Run only if build succeeds or is unstable
- Run regardless of build result
Run only if build succeeds
stage 'build' ... build ... tests stage 'post-build' ...
(Or add
-Dmaven.test.failure.ignore=false
to theMAVEN_OPTS
)Run only if build succeeds or is unstable
stage 'build' ... build try { ... tests } catch { ... } stage 'post-build' ...
(Or add
-Dmaven.test.failure.ignore=true
to theMAVEN_OPTS
)Run regardless of build result - could it be done using
try / catch / finally
?try { stage 'build' ... } catch { ... } finally { stage 'post-build' ... }
(I've noticed that final build status is set as SUCCESS even though some stages, ie. 'build', have failed as it set based on last stage. Does that mean final build status need to explicitly set, ie.currentBuild.result = 'UNSTABLE'
? )
The best way is to use post build action in the pipeline script.
Jenkinsfile (Declarative Pipeline)
The documentation is below https://jenkins.io/doc/book/pipeline/syntax/#post
If you are using try/catch and you want a build to be marked as unstable or failed then you must use currentBuild.result = 'UNSTABLE' etc. I believe some plugins like the JUnit Report plugin will set this for you if it finds failed tests in the junit results. But in most cases you have to set it your self if you are catching errors.
The second option if you don't want to continue is to rethrow the error.
try-catch
blocks can be set up to handle errors like in real application code.For example:
And here is an example output with two aborts.
Of course, this works for any exceptions happening during the execution.