Home > Mobile >  How to add the option of 'Triger parametrized build on other project' on Post Build in Jen
How to add the option of 'Triger parametrized build on other project' on Post Build in Jen

Time:12-07

How can I add the below example to my Jenkins pipeline in Post Build: part

CodePudding user response:

Assuming you are referring to declarative pipelines you can achieve this using the pipeline post directive alongisde the built in build step that will allow you to trigger the parameterized build.

The Jenkins post section is used to execute commands after the build steps of your pipeline have completed, you can control the post execution based on several predefined conditions that determine when the steps in the post section will be executed. In your case you will want to use the always condition.

The build step allows you to trigger a new build for a given job, while passing the relevant parameters of the job, the type of the parameters should match the type of the parameters defined in the job you are triggering. In addition there is a wait parameter to determine if your job should wait until the triggered job has finished.

Your resulting pipeline will look something like (assuming string parameters):

pipeline {
    agent any
    stages {
      // your pipeline steps
    }
    post {
       always {
           build job: 'YourJobName', wait: false, 
                 parameters:[string(name: 'JobName', value: JOB_NAME), string(name: 'JobID', value: BUILD_ID)]
       }
    }
}

If you are using a scripted pipeline, the post section is not available and you will need to wrap your code with a try catch block and run the build step in the finally section:

node {
    try {
        // Your pipeline steps
    } catch (err) {
        // optional error handeling
        echo "Failed: ${err}"
    } finally {
        // will always be executed
        build job: 'YourJobName', wait: false, 
              parameters:[string(name: 'JobName', value: JOB_NAME), string(name: 'JobID', value: BUILD_ID)]
    }
}
  • Related