Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Post-Build stage using Jenkins Pipeline plug-in?

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 the MAVEN_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 the MAVEN_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'? )

like image 486
luka5z Avatar asked Apr 15 '16 15:04

luka5z


People also ask

How do you get post build actions in Jenkins pipeline?

You can simply add post action to your pipeline script, in case of using declarative pipeline. It is explained in Pipeline syntax reference. Show activity on this post.

What are the uses of post build plugin in Jenkins?

This plugin allows the user to execute a shell/batch task depending on the build log output. Java regular expression are allowed. This feature allows user to associate shell or a batch scripts that perform some tasks on Jenkins depending on the build log output.

What is post in Jenkins pipeline?

Since the post section of a Pipeline is guaranteed to run at the end of a Pipeline's execution, we can add some notification or other steps to perform finalization, notification, or other end-of-Pipeline tasks. See Glossary - Build Status for the different build statuses: SUCCESS, UNSTABLE, and FAILED.


1 Answers

The best way is to use post build action in the pipeline script.

Handling Failures
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. The Pipeline Syntax section provides more detail on how to use the various post conditions.

Jenkinsfile (Declarative Pipeline)

pipeline {     agent any     stages {         stage('Test') {             steps {                 sh 'make check'             }         }     }     post {         always {             junit '**/target/*.xml'         }         failure {             mail to: [email protected], subject: 'The Pipeline failed :('         }     } } 

The documentation is below https://jenkins.io/doc/book/pipeline/syntax/#post

like image 150
Mohamed Thoufeeque Avatar answered Sep 19 '22 21:09

Mohamed Thoufeeque