How to trigger another Jenkins pipeline that needs a $BRANCH variable?

旧巷老猫 提交于 2021-02-11 12:32:29

问题


I have 3 pipelines. Say:

build-and-release build release

I'm refactoring my pipelines, so that I can just call the build and release jobs, from within the build-and-release. Something along these lines:

// build-and-release JenkinsFile
node('master') {
  build job: "build", propagate: true, wait: true
  build job: "release", propagate: true, wait: true
}

// build JenkinsFile
node('master') {
  stage('Build') {
    // do stuff
  }
}

// release JenkinsFile
node('master') {
  stage('Release') {
    // do other stuff
  }
}

This seems to work as it is, but the build and release pipelines always assume the branch is MASTER. When I run the build-and-release job, I pick what branch I want to build.

How do I make the other jobs use the same $BRANCH that I pick from build-and-release?


回答1:


You can configure build and release jobs as parameterized job and then you can send the Branch name as a prameter, as shown below:-

// build-and-release JenkinsFile
node('master') {
  build job: 'build', parameters: [string(name: 'Branch', value: "${env.BRANCH_NAME}")], propagate: true, wait: true
  build job: 'release', parameters: [string(name: 'Branch', value: "${env.BRANCH_NAME}")], propagate: true, wait: true
}

And then you can use the Branch variable in Jenkinsfile of build and release.

Note:- You can configure any other parameter type in above example String Parameter has been configured, for more information please refer the link



来源:https://stackoverflow.com/questions/58703438/how-to-trigger-another-jenkins-pipeline-that-needs-a-branch-variable

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