Getting result of a stage in a pipeline job?

53 views
Skip to first unread message

Per Ostman

unread,
Apr 7, 2017, 6:41:29 AM4/7/17
to Jenkins Users
Hi!

I'm trying to figure out how to get hold of the result of a stage in a pipeline job to report progress/status in e g slack as the pipeline stages execute.

Does anyone have any pointers to how to accomplish this?

/P

Idan Adar

unread,
Apr 7, 2017, 7:14:18 AM4/7/17
to Jenkins Users
You could something like the following, where if all actions have worked, it'll simply read the slack action and perform it.

stage ("Publish CF app") {
         environment {
            JENKINSBOT = credentials('...')
         }
         
         when {
            branch "develop"
         }
         
         steps {
            script {
               STAGE_NAME = "Publish CF app"
            }
            
            // Login and push
            sh "cf api ..."
            sh "cf login ... -u $JENKINSBOT_USR -p $JENKINSBOT_PSW"
            sh "cf push"
      
            slackSend (
               color: '#199515',
               message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER> passed successfully."
            )
         }
      }

You could also use the POST directive inside a stage:

stage {
   steps
{
   
...
   
}
 
   post
{
       success
{
           slack
...
       
}
       failure
{
           slack
...
       
}
   
}
}

Now, once a stage fails, the whole job fails, so you don't really need to handle the failure in each stage. Instead you can also use POST globally:
And as you can see in my first example, I basically set a "STAGE_NAME" in each stage and use it in the failure directive globally

stages {
    stage
{
        steps
{
       
       
}
   
}


    stage
{
        steps
{
       
       
}
   
}


    post
{
        success
{
       
       
}
        failure
{
            
slackSend (
            color: '#F01717',
            message: "$JOB_NAME: <$BUILD_URL|Build #$BUILD_NUMBER>, '$STAGE_NAME' stage failed."
         )
       
}
   
}
}

Per Ostman

unread,
Apr 7, 2017, 7:34:18 AM4/7/17
to jenkins...@googlegroups.com
Ah! So you're using the fact that if one reaches the end of the stage, the stage has succeeded... I feel stupid - I was trying to get hold of the stage result through the java api...

Thanks! I'll try it out!

--
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/FZqiw3_LZ5M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/0c9ade5c-4541-4711-bd49-7cd5f8add805%40googlegroups.com.

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



--

All the best,
/Per
"If we have data, let’s look at data. If all we have are opinions, let’s go with mine." —Jim Barksdale

Per Östman
Manager, Quality Control
O  +46 31 7504897  |  M  +46 766 477602  |  E  per.o...@tibco.com |
TIBCO Software  Första Långgatan 26 41328 Gothenburg, Sweden |  www.tibco.com      |
 
 
 
The information transmitted in this electronic communication is intended only for the person or entity to whom it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited.
Reply all
Reply to author
Forward
0 new messages