' post'的语法是什么?在脚本管道中与声明性管道相比? https://jenkins.io/doc/book/pipeline/syntax/#post
答案 0 :(得分:29)
对于脚本化管道,所有内容都必须以编程方式编写,大部分工作都在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.previousBuild?.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
答案 1 :(得分:0)
您可以使用闭包修改@ jf2010解决方案,以使其看起来更整洁(我认为)
pipeline = {
stage('Test') {
sh 'echo "Fail!"; exit 1'
}
echo 'This will run only if successful'
}
postFailure = {
echo 'This will run only if failed'
}
postAlways = {
echo 'This will always run'
}
node{
try {
pipeline()
} catch (e) {
postFailure()
throw e
} finally {
postAlways()
}
}