Grouping post conditions in a Jenkins declarative pipeline

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 21:48:28

问题


Is there a way to group post conditions in a Jenkins declarative pipeline ?

For instance, I want to do the same thing for statuses aborted failure and success. Is there a shorter way to do it than the following ?

post {
  aborted { sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS") 
  failure { sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS")     
  success { sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS")
}

回答1:


There is the 'always' condition:

post {
  always {sendNotification(currentBuild.result, "$LIST_NOTIFICATION_JENKINS")}
}

The 'always' condition will run regardless of the stage result.

See the documentation on the post section.

If you want a set of common actions between just a few conditions, for example if you wanted to do the same thing for failure and aborted, I would recommend creating a function in your script to call from the failure and aborted post conditions.

You can also do something like the following:

always {
    script{
        if (currentBuild.currentResult == "ABORTED" || currentBuild.currentResult == "FAILURE")
        {
            echo "was aborted or failed"
        }
    }
}


来源:https://stackoverflow.com/questions/48545400/grouping-post-conditions-in-a-jenkins-declarative-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!