我正在尝试编写一个作业,如果作业结果成功,它将触发一个新的管道作为后期操作。
在 Web 界面上执行此操作很简单,因为我可以进入配置,添加“构建后操作”,指定作业名称,然后选择“仅在构建稳定时触发”。
但是我不确定如何在 groovy 脚本中配置它。
我尝试了以下方法:
freeStyleJob('Test_Poll_Github') {
wrappers {
preBuildCleanup()
credentialsBinding {
usernamePassword('userVariableName', 'passwordVariableName', 'jenkins api')
}
}
environmentVariables {
env('QUAY_USERNAME', '${userVariableName}')
env('QUAY_PASSWORD', '${passwordVariableName}')
}
steps {
shell('''printenv''')
shell(readFileFromWorkspace('scripts/github/poll.sh'))
}
publishers {
// Add a post-build action to trigger the "scm-test" job only if the build is stable
postBuild {
always {
script {
// Check if the build is stable before triggering the "scm-test" job
if (currentBuild.resultIsBetterOrEqualTo(hudson.model.Result.SUCCESS)) {
build(job: 'scm-test', propagate: false)
} else {
echo "Build result is not stable. Not triggering 'scm-test' job."
}
}
}
}
}
}
但是,当我运行作业配置时,出现以下错误
ERROR: (unknown source) No signature of method: javaposse.jobdsl.dsl.helpers.publisher.PublisherContext.postBuild() is applicable for argument types: (script$_run_closure1$_closure5$_closure7) values: [script$_run_closure1$_closure5$_closure7@f9ca920]
请告知,因为我确信这是可能的,只是不确定确切的语法是什么。
我不太确定这种格式,但是如果您可以在
always
部分中使用 postBuild
块,那么您应该能够以相同的方式使用 success
:
postBuild {
success {
build(job: 'scm-test', propagate: false)
}
}