How do you trigger a pipeline on successful build in Jenkins CASC

196 Views Asked by At

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.

2

There are 2 best solutions below

0
M B On

I'm not entirely sure about this format, but if you can use an always block in your postBuild section, you should be able to use success in the same way:

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

I use this in my builds

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

You can additionally set the first build to wait until the second build is done to finish