groovy 如何在Jenkins CASC中成功构建时触发管道

anhgbhbe  于 8个月前  发布在  Jenkins
关注(0)|答案(2)|浏览(83)

我正在尝试编写一个作业,如果作业的结果是成功的,它将触发一个新的管道作为post操作。
这样做是因为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]

请告知,因为我相信这是可能的,只是没有信心什么确切的语法将是。

e7arh2l6

e7arh2l61#

我不完全确定这种格式,但如果你可以在postBuild部分使用always块,你应该可以以同样的方式使用success

postBuild {
  success {
    build(job: 'scm-test', propagate: false)
  }
}
dxxyhpgq

dxxyhpgq2#

我在我的构建中使用这个

post {
    success {
        build job: 'build-name',     
        parameters: [string(name: 'Parameter', value:  'Value')]
    }
}

您还可以将第一个构建设置为等待第二个构建完成

相关问题