Jenkins conditional post-based action in a pipeline stage

867 views
Skip to first unread message

Kai

unread,
Sep 20, 2017, 4:50:21 PM9/20/17
to Jenkins Users
Hi,

I have a pipeline with multiple stages and for my integration test stage, I trigger my integration test. I wanted to setup some post-step based logic where if the test job passes, it does some action and if the test job fails it performs a different action (e.g. send email) .

stage('Run Integration Tests') {
            steps {
                timeout(time: 30, unit: 'MINUTES') {
                    retry(1) {
                        build job: 'my-integration-test'
                    }
                } 
            }
        }
Enter code here...

Is it possible to accomplish this kind of setup ?

Thanks

Kai

unread,
Sep 21, 2017, 12:16:53 PM9/21/17
to Jenkins Users
It seems like using try/catch complicates the code and then I have to remember to still fail the pipeline in the exception block. Using the post functionality seems to be for the entire pipeline not for a specific step.

Is there a post-step functionality that can be invoked for both pass and fail of a step ?  Thanks

Robert Hales

unread,
Sep 21, 2017, 10:18:41 PM9/21/17
to Jenkins Users
You can use the post{} block within a stage in the declarative pipeline:  



stage('Run Integration Tests') {
           steps
{
               timeout
(time: 30, unit: 'MINUTES') {
                   
retry(1) {
                       build job
: 'my-integration-test'
                   
}
               
}
           
}

            post
{
                always
{

               
}
               
changed {

               
}

                success
{

               
}
                failure
{

               
}
                unstable
{

               
}
           
}
       
}

Reply all
Reply to author
Forward
0 new messages