DevOps__1903
DevOps__1903

Reputation: 77

How to trigger a new pipeline job only on completion of current job using groovy script

Using groovy script how can another pipeline job be triggered as soon as the build completes. Suppose there are two pipeline A and B, and as soon as pipeline A completes successfully it has to pass its parameters to B and B has to start running. (B has to run only after A terminates)

I tried to trigger the build using the below code, this triggers the job B but job A remains running util the complete execution of job B

stage('Artifactory_Publish_Job') {
        try {
    def job = build job: 'Jfrog_Artifactory_Publish', parameters: [[$class: 'StringParameterValue', name: 'buildDateString', value: 'default'], [$class: 'StringParameterValue', name: 'WORKSPACE_DIR', value: 'default'], [$class: 'StringParameterValue', name: 'Artifactory_Directory', value: 'default']]
        } catch (any) {

            throw any
          }
        }

Upvotes: 1

Views: 401

Answers (1)

ycr
ycr

Reputation: 14574

You can run the second Job in the Post Step, which is executed once the Job is completed. Additionally you can add following options to the build command propagate: false, wait: false

pipeline {
    agent any
    stages {
        stage('Example') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post { 
        success { 
            echo 'Will run only when successful.'
            build job: 'Jfrog_Artifactory_Publish', parameters: [[$class: 'StringParameterValue', name: 'buildDateString', value: 'default'], [$class: 'StringParameterValue', name: 'WORKSPACE_DIR', value: 'default'], [$class: 'StringParameterValue', name: 'Artifactory_Directory', value: 'default']],  propagate: false, wait: false 
        }
    }
}

Upvotes: 0

Related Questions