How to trigger parameterized build on successful build in Jenkins?

懵懂的女人 提交于 2019-12-07 15:54:05

问题


I have three pipeline projects, project-a, project-b and project-c. project-c takes a parameter. On successful completion of either project-a or project-b I want to trigger a build of project-c with a parameter.

I can do this in project-a and project-b with this code in the pipeline:

stage('trigger-project-c') {
    def job = build job: 'project-c', parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: 'somevalue']]
}  

But this requires two executors. I want project-a or project-b to completely finish before running project-c with parameters.


回答1:


Your pipeline most likely looks like this:

node {
  stage('build') {
    // sh "make"
  }

  // ...

  stage('trigger-project-c') {
      def job = build job: 'project-c', parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: 'somevalue']]
  }
}

By wrapping everything inside the node closure, the downstream job project-c is triggered inline, without the upstream job being paused / releasing an executor.

Therefore, things that do essentially nothing for a long time should not be wrapped within a node step, in order to not block an executor. A very similar case is when using the input step to wait for user feedback.

Instead, your pipeline should look e.g. as follows, which is - so to say - the best practice (as you don't block your executor):

stage('build') {
  node {
    // sh "make"
  }
}

// or 

node {
  stage('build') {
    // sh "make"
  }

  stage('unit') {
    // sh "make"
  }
} // node

// note: the following code is _not_ wrapped inside a `node` step 
stage('trigger-project-c') {
  def job = build job: 'project-c', parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: 'somevalue']]
}

There is no need to wrap the build step within a node, i.e., block an executor for it. For other steps (like sh), pipeline execution would trigger an error and remind you that it cannot be run outside of a node allocation.




回答2:


Add the parameter wait: false to the build step to continue pipeline execution without waiting for the downstream job.

EDIT: this would help, if you don't care for the success of the other downstream job for this pipeline. If you need to wait until it is finished and then continue the own (upstream job's) pipeline, then see my other answer.



来源:https://stackoverflow.com/questions/41521287/how-to-trigger-parameterized-build-on-successful-build-in-jenkins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!