【发布时间】:2018-02-26 13:05:26
【问题描述】:
与声明式管道相比,脚本管道中的“post”的语法是什么? https://jenkins.io/doc/book/pipeline/syntax/#post
【问题讨论】:
标签: jenkins-pipeline
与声明式管道相比,脚本管道中的“post”的语法是什么? https://jenkins.io/doc/book/pipeline/syntax/#post
【问题讨论】:
标签: jenkins-pipeline
对于脚本化管道,一切都必须以编程方式编写,并且大部分工作都在 finally 块中完成:
Jenkinsfile(脚本化管道):
node {
try {
stage('Test') {
sh 'echo "Fail!"; exit 1'
}
echo 'This will run only if successful'
} catch (e) {
echo 'This will run only if failed'
// Since we're catching the exception in order to report on it,
// we need to re-throw it, to ensure that the build is marked as failed
throw e
} finally {
def currentResult = currentBuild.result ?: 'SUCCESS'
if (currentResult == 'UNSTABLE') {
echo 'This will run only if the run was marked as unstable'
}
def previousResult = currentBuild.getPreviousBuild()?.result
if (previousResult != null && previousResult != currentResult) {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
echo 'This will always run'
}
}
https://jenkins.io/doc/pipeline/tour/running-multiple-steps/#finishing-up
【讨论】:
您可以修改@jf2010 解决方案,使其看起来更整洁(在我看来)
try {
pipeline()
} catch (e) {
postFailure(e)
} finally {
postAlways()
}
def pipeline(){
stage('Test') {
sh 'echo "Fail!"; exit 1'
}
println 'This will run only if successful'
}
def postFailure(e) {
println "Failed because of $e"
println 'This will run only if failed'
}
def postAlways() {
println 'This will always run'
}
【讨论】: