Can I have a reusable “post” block for my jenkins pipelines?

49 views
Skip to first unread message

red 888

unread,
Jul 26, 2018, 7:22:53 PM7/26/18
to Jenkins Users

I have many jenkins pipelines for several different platforms but my "post{}" block for all those pipelines is pretty samey. And its quite large at this point because I include success,unstable,failure and aborted in it.


Is there a way to parameterize a reusable post{} block I can import in all my pipelines? I'd like to be able to import it and pass it params as well (because while its almost the same it varies very slightly for different pipelines).


Here is an example post block that is currently copy and pasted inside all my pipeline{}s


post {
    success{
        script {
            // I'd like to be able to pass in values for param1 and param2
            someGroovyScript {
                param1 = 'blah1'
                param2 = 'blah2'
            }
            // maybe id want a conditional here that does something with a passed in param
            if (param3 == 'blah3') {
                echo 'doing something'
            }
        }
    }
    unstable{
        ... you get the idea
    }
    aborted{
        ... you get the idea
    }
    failure{
        ... you get the idea
    }
}

Johan Abildskov

unread,
Jul 30, 2018, 11:33:21 AM7/30/18
to Jenkins Users
You should be able to do this with Shared Libraries: https://jenkins.io/doc/book/pipeline/shared-libraries/

red 888

unread,
Aug 2, 2018, 4:04:20 PM8/2/18
to Jenkins Users
how does that work though? My pipelines are already in shared libraries. I want a shared post block specifically.

Jan Monterrubio

unread,
Aug 2, 2018, 7:13:56 PM8/2/18
to jenkins...@googlegroups.com
We do this in our CI. Basically the shared library has some variable parameters you can override and it lets you control what happens based on those. I’ll see if I can scrounge up a sample later today.

--
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/aa1f5f13-a8c3-42b6-bccc-a35047f0c293%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

red 888

unread,
Aug 3, 2018, 1:20:20 PM8/3/18
to Jenkins Users
wait there seems to be confusion about what i am asking for look at my SO post: https://stackoverflow.com/questions/51508234/can-i-have-a-reusable-post-block-for-my-jenkins-pipelines 

I also ready use shared libs, all my pipelines are there. Between those several pipelines they all have a post block that is nearly identical. I want to reference a parameterized post block ONLY. My post block now is like 80 lines of code at the end of each pipeline thats nearly tthe same. even after moving most of the post block steps into separate groovy files (that are referenced in the post block) the whole post block is still large.

Jan Monterrubio

unread,
Aug 7, 2018, 12:10:56 PM8/7/18
to jenkins...@googlegroups.com
So, I think what you want is:

Project 1 Jenkins File:

pipeline {

stages {
Stage(‘’npm i’){}
commonPost()
}

Project 2 Jenkins file
Pipeline{
Script {
Echo{
}
}
commonPost()
}

Am I getting that correctly? 


red 888

unread,
Aug 20, 2018, 7:51:46 PM8/20/18
to Jenkins Users
Hello yes thats what im after (although your second example doesn't have any steps). Sorry didn't check this thread in a while. 

It would also be great if I could actually pass in args so it could be like
commonPost{arg1 = 'blah', arg2 = 'balh'}

Craig Rodrigues

unread,
Aug 20, 2018, 8:31:44 PM8/20/18
to Jenkins Users
red 888,

I want the exact same thing, i.e. a common post method which I can pass arguments to, such as:

commonPost{email='ema...@somewhere.blah', notifySlack=true}

etc.

If you get something to work, please post a reference to an existing pipeline with this.
Thanks!
--
Craig
Reply all
Reply to author
Forward
0 new messages