Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

post equivalent in scripted pipeline?

What is the syntax of 'post' in scripted pipeline comparing to declarative pipeline? https://jenkins.io/doc/book/pipeline/syntax/#post

like image 748
kol23 Avatar asked Feb 26 '18 13:02

kol23


People also ask

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.

What is a pipeline post?

The post section defines one or more additional steps that are run upon the completion of a Pipeline's or stage's run (depending on the location of the post section within the Pipeline).

What are the 3 types of pipelines in Jenkins?

Declarative versus Scripted Pipeline syntax Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which: provides richer syntactical features over Scripted Pipeline syntax, and.

What is the difference between descriptive and scripted pipeline?

Declarative pipelines break down stages into individual stages that can contain multiple steps. Scripted pipelines use Groovy code and references to the Jenkins pipeline DSL within the stage elements without the need for steps.


2 Answers

For scripted pipeline, everything must be written programmatically and most of the work is done in the finally block:

Jenkinsfile (Scripted Pipeline):

node {     try {         stage('Test') {             sh 'echo "Fail!"; exit 1'         }         echo 'This will run only if successful'     } catch (e) {         echo 'This will run only if failed'          // Since we're catching the exception in order to report on it,         // we need to re-throw it, to ensure that the build is marked as failed         throw e     } finally {         def currentResult = currentBuild.result ?: 'SUCCESS'         if (currentResult == 'UNSTABLE') {             echo 'This will run only if the run was marked as unstable'         }          def previousResult = currentBuild.getPreviousBuild()?.result         if (previousResult != null && previousResult != currentResult) {             echo 'This will run only if the state of the Pipeline has changed'             echo 'For example, if the Pipeline was previously failing but is now successful'         }          echo 'This will always run'     } } 

https://jenkins.io/doc/pipeline/tour/running-multiple-steps/#finishing-up

like image 76
bp2010 Avatar answered Sep 18 '22 13:09

bp2010


You can modify @jf2010 solution by using closures so that it looks a little neater (in my opinion)

try {     pipeline() } catch (e) {     postFailure(e) } finally {     postAlways() }   def pipeline(){     stage('Test') {         sh 'echo "Fail!"; exit 1'     }     println 'This will run only if successful' }  def postFailure(e) {     println "Failed because of $e"     println 'This will run only if failed'  }  def postAlways() {     println 'This will always run' } 
like image 44
smelm Avatar answered Sep 20 '22 13:09

smelm