10

I have a job in Jenkins and I need to trigger another one when it ends (if it ends right).

The second job is a multibranch, so I want to know if there's any way to, when triggering this job, pass the branch I want to. For example, if I start the first job in the branch develop, I need it to trigger the second one for the develop branch also.

Is there any way to achieve this?

Jaime Alcántara Arnela
  • 2,062
  • 5
  • 25
  • 56

3 Answers3

16

Just think about the multibranch job being a folder containing the real jobs named after the available branches:

Using Pipeline Job

When using the pipeline build step you'll have to use something like: build(job: 'JOB_NAME/BRANCH_NAME'). Of course you may use a variable to specify the branch name.

Using Freestyle Job

When triggering from a Freestyle job you most probably have to

  1. Use the parameterized trigger plugin as the plain old downstream build plugin still has issues triggering pipeline jobs (at least the version we're using)
  2. As job name use the same pattern as described above: JOB_NAME/BRANCH_NAME

Should be possible to use a job parameter to specify the branch name here. However I didn't give it a try, though.

Joerg S
  • 4,730
  • 3
  • 24
  • 43
  • I'm trying to understand it. So, how many jobs would I have in this situation? Would I still be able of building the multibranch job (both of them) independently? – Jaime Alcántara Arnela Jun 19 '18 at 10:32
  • Maybe I don’t really get your question. You‘ll have as many jobs as you need. You asked for a job to trigger the multibranch job. So I assume you have one trigger and one multibranch job. Alle of the can be triggered manually as with all jobs in Jenkins. – Joerg S Jun 20 '18 at 11:31
  • What if the branch name has slash (e.g feature/foo)? Should that be encoded? – James Selvakumar Aug 17 '18 at 09:31
  • 1
    Never tried. But as the job name for such jobs in Jenkins is encoded using `%252F` I'd assume that you'll need at least some special encoding. – Joerg S Aug 17 '18 at 10:55
0

Yes, you can call downstream job by adding post build step: Trigger/Call build on other projects(you may need to install "Parameterized Trigger Plugin"): enter image description here

  • where in Parameters section you define vars for the downstream job associated with vars from current job.

Also multibranch_PARAM1 and *PARAM2 must be configured in the downstreamjob:
enter image description here

hopetds
  • 435
  • 2
  • 6
0

Sometimes you want to call one or more subordinate multibranch jobs and have them build all of their branches, not just one. A script can retrieve the branch names and build them.

Because the script calls the Jenkins API, it should be in a shared library to avoid sandbox restrictions. The script should clear non-serializable references before calling the build step.

Shared library script jenkins-lib/vars/mbhelper.groovy:

def callMultibranchJob(String name) {
    def item = jenkins.model.Jenkins.get().getItemByFullName(name)
    def jobNames = item.allJobs.collect {it.fullName}
    item = null // CPS -- remove reference to non-serializable object
    for (jobName in jobNames) {
        build job: jobName
    }
}

Pipeline:

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    library 'jenkins-lib'
                    mbhelper.callMultibranchJob 'multibranch-job-one'
                    mbhelper.callMultibranchJob 'multibranch-job-two'
                }
            }
        }
    }
}
Steve Mitchell
  • 1,895
  • 1
  • 15
  • 12