I am trying to write a job that will trigger a new pipeline as a post action if the result of the job was success.
Doing this is the web interface is simple, as I can go into configure, add a "Post-Build Actions", specify the job name, and select "Trigger only if build is stable".
However I am not sure how to configure this in a groovy script.
I tried the following:
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."
}
}
}
}
}
}
However when I run the job configure I get the following error
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]
Please advise, as I am sure this is possible, just not confident what the exact syntax would be.
I'm not entirely sure about this format, but if you can use an
alwaysblock in yourpostBuildsection, you should be able to usesuccessin the same way: