Lucas Costa
Lucas Costa

Reputation: 113

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

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?

Upvotes: 0

Views: 1114

Answers (1)

user_9090
user_9090

Reputation: 1974

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

Upvotes: 1

Related Questions