1

I have an upstream freestyle job and a downstream pipeline job.

The freestyle job is triggered via a gitHub webhook to run whenever code is pushed to gitHub branch.

The freestyle job should capture the branch name and pass to the pipeline job and should trigger the build for that particular branch.

How to pass the branch name to the pipeline job dynamically? enter image description here

2 Answers 2

1

The "Add Parameters" can help you. You should set up your downstream pipeline with "GIT_BRANCH" parameter to build with that branch, and transfer it from this upstream job.

1

Can't tell you why this does not work, I'm not using the Jenkins UI for build configuration anymore. I guess the Jenkins UI is blocking you because it does not recognize tha $GIT_BRANCH is a variable.

We do exactly what you are trying with a script-step in the pipeline, and that works:

stage('Trigger downstream job') {
   steps {
      script {
         build(job: 'SomeJob/' + env.GIT_BRANCH.replaceAll('/', '%2F'),
               parameters: [...],
               propagate: true,
               wait: true)
      }
   }
}

Note that / in the branchname must be escaped.

So I recommend to rewrite your job with a Jenkinsfile :-)

1
  • Note it's sometimes best to add a downstream job not as a 'post' stage e.g. success { script { build {...
    – JohnZaj
    Jun 15, 2021 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.