Execute a function from post actions in Jenkinsfile

14 views
Skip to first unread message

Kernel Panic

unread,
Dec 20, 2020, 12:35:49 PM12/20/20
to Jenkins Users

Hello there.

I looking for an elagant way to execute code from the post {} section,
I want to write a clean Jenkinsfile and execute code from shared libraries as
posible.
 
In the post section I have the tipical  clean working directory like this:

always {
      cleanWs()
    }

I also send notification based on pipeline completion, I mean, aobrted , failed,
changed and so on,  but I want to call a function to do that, I want to remove the
email code I have something like this:

emailext body: 'Check console output at $BUILD_URL to view the results. \n\n ${CHANGES} \n\n --------------------------------- \n${BUILD_LOG, maxLines=50, escapeHtml=false}',
                to: "${EMAIL_ADDRESSES}",
                subject: 'Pipeline Execution  Failed: $PROJECT_NAME - #$BUILD_NUMBER'

The problem seems that from the post you only can call an expected function of
type: always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup.

Is there another way to accomplish this to write a more clean Post section?


Thanks
Regards

Gianluca Massera

unread,
Dec 20, 2020, 12:51:45 PM12/20/20
to jenkins...@googlegroups.com, Kernel Panic

Hi,

I'm not sure I understood your issue on calling functions. Maybe there is only a bit of confusion about the Jenkins pipeline syntax.

From what you said, you are already calling functions into the post sections: "cleanWs()" is a function, "emailext body: ... " is a function.

So, maybe you are confused by the fact Jenkins syntax in post doesn't allow something like:

post {

    aFunction()

}

So, nope, the above doesn't work. Because the "post" syntax are blocks based on the result of the pipeline and you actually listed all possible options:

"always, changed, fixed, regression, aborted, success, unsuccessful, unstable, failure, notBuilt, cleanup"

And if I understood, you want to call a function that does different things depending on the pipeline result ... then, you need to used "always":

post {

   always {

       aFunction(BUILD_RESULT)

   }

}

And then the function will do different things depending of the BUILD_RESULT value:

if (BUILD_RESULT == "SUCCESS") {

   message = "Pipeline failed"

} else {

   message = "Pipeline ... whatever"

}

I hope that helps.

Cheers,

Gianluca.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ea74e553-88a7-4925-ade2-27462aaceab1n%40googlegroups.com.

Kernel Panic

unread,
Dec 20, 2020, 4:12:50 PM12/20/20
to Jenkins Users
Hello
Yes, that's what I want to do
Thank you so much.
Reply all
Reply to author
Forward
0 new messages