pipeline post-action in Jenkinsfile ?

8,193 views
Skip to first unread message

Mike Caspar

unread,
May 5, 2016, 11:56:10 AM5/5/16
to Jenkins Users
Hi there,

I am converting a series of jobs to pipeline jobs.

I am not having any luck in finding the post-action stage  or finding a way to do this.

a 'step' isn't appropriate to the situation (as it won't trigger on failed).

If someone knows the answer to this, I would be happy to go over to the docs and update once I get this working.

Ideas?





Baptiste Mathus

unread,
May 5, 2016, 3:12:29 PM5/5/16
to jenkins...@googlegroups.com

Hi,

There's no such concept in pipeline.

As Jesse explained: "the idea is that a Pipeline script is just a script: it runs the stuff you specified if and when you want it to. Whereas plugins contributing options to freestyle projects hardcode a lot of logic about when and whether something will happen, or make it a GUI configuration option, a Pipeline script is expected to define its own logic. We can always add convenience methods and DSLs on top of that for common usage patterns."

In your case, to do something on failure, you would typically use a try/catch/finally block.

If you are more specific on what you're trying to achieve, we might be able to help you more.

Cheers

--
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/8b403f7e-a245-4e47-bd8f-185b5094d71a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Caspar

unread,
May 5, 2016, 3:25:03 PM5/5/16
to jenkins...@googlegroups.com
Thanks.

I like the try/catch/finally option if that would work.

I have setup a Pipeline Organization to auto-import my Scripts.

it sets up all the necessary repos  to build each one.

The problem occurs that I have no way to set the typical "email on success/email on failure" (I am also a contributor to a plugin that does Notification).

I could hardcode all my jobs or even use the Jenkins config DSL, but that would defeat the purpose of having the auto-import of a Multi-Branch Company setup.

I like the idea of helpers for other DSL items..  Maybe one to simulate the Config DSL from to set properties, endpoints, notifications somehow ?

What I really would like to do is use the DSL Script to reproduce the Notifications, emails, etc. within my script. (identified as post-action).

For now, I'll try to do it all in each script based on your exception.

Here's what I would like to do (from auto-import of Jenkinsfile to create new builds)....

- If an error occurs, continue anyways....

So, I guess what I'm looking for is.

try {
step 1
step 2
step 3
step 4
step 4
}
catch
{
task {send email of build status}
}
finally
{
task {send email of build status}
}


Thanks !! for your help.. If anything, an example of try/catch finally would be helpful. I would see what I could do to share back.

Much appreciated.

Mike Caspar
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/5sN7BR7daOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CANWgJS6Y%2BdXKsAU2_0tTommH-DLjqDa1h8NGx%2B_1wRycW3odVQ%40mail.gmail.com.

Mike Caspar

unread,
May 5, 2016, 3:30:35 PM5/5/16
to jenkins...@googlegroups.com

Hi,

Things I am trying to define as post-action items (pass or fail).

slack update
email
html publish of test results
ssh of build results to a remote ssh
ironmq-notifier of results.
http post of JSON data to remote testing services (ie: ghostscript and runscope)
(stuff like that).

Craig Rodrigues

unread,
May 5, 2016, 3:38:54 PM5/5/16
to Jenkins Users
Hi,

Yes, in your pipeline script, you need to catch exceptions for what you
want to do.  Be careful, if the build fails, you will need to set currentBuild.status and re-throw the exception in order for the build to be marked as failed.

Look at this example which I wrote:
https://github.com/freebsd/freebsd-ci/blob/master/scripts/build/build-test.groovy

--
Craig

Mike Caspar

unread,
May 5, 2016, 3:47:53 PM5/5/16
to jenkins...@googlegroups.com

Awesome.

This worked great. 

I managed to simply wrap a try around my whole file and then a catch/finally for success and failure.

Craig, I'll look at the example. Thanks

I did figure out that I needed to use the error tag with some text during my catch block and that properly set it the status to failed.. Cool.

Where would be a good place to put this as an example in the docs for others  ?

Craig, any suggestions where I might put a section for try/catch and "simulating" what was there before for others who will no doubt find themselves in similar transitions ? Any thoughts are a good place to start.

Mike

Oh.. thanks again to Craig Rodrigues and Baptiste Mathus

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/5sN7BR7daOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.

Antonio Muñiz

unread,
May 6, 2016, 7:57:09 AM5/6/16
to jenkins...@googlegroups.com
Take special precautions about unexpected exceptions thrown inside the finally block. 
In that case script-security-plugin is not going to interpret approval requests correctly.

--
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/a2bd3ac9-2a99-af64-bbee-3c403f0dfa13%40caspar.com.

For more options, visit https://groups.google.com/d/optout.



--
Antonio Muñiz
Software Engineer
CloudBees, Inc.

Mike Caspar

unread,
May 6, 2016, 4:16:06 PM5/6/16
to jenkins...@googlegroups.com
Thanks.

That explains some of the weirdness I am having. Much appreciated.

Mike

Baptiste Mathus

unread,
May 7, 2016, 9:31:49 AM5/7/16
to jenkins...@googlegroups.com


Le 5 mai 2016 9:47 PM, "Mike Caspar" <mi...@caspar.com> a écrit :
>
> Awesome.
>
> This worked great. 
>
> I managed to simply wrap a try around my whole file and then a catch/finally for success and failure.
>
> Craig, I'll look at the example. Thanks
>
> I did figure out that I needed to use the error tag with some text during my catch block and that properly set it the status to failed.. Cool.
>
> Where would be a good place to put this as an example in the docs for others  ?

Please add your examples there:
https://github.com/jenkinsci/pipeline-examples

Thanks a lot!

Mike Caspar

unread,
May 7, 2016, 11:12:36 AM5/7/16
to jenkins...@googlegroups.com
Cool.

Created a PR for a Jenkinsfile example that has a few steps and a try/catch block.

https://github.com/jenkinsci/pipeline-examples/pull/30

It would be great if someone did a quick check before putting it into master.

I had to move the mail into the try and catch blocks as putting the results into finally caused script access problems.

Mike
--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/5sN7BR7daOs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages