'How to trigger multibranch downstream job with jobname/{env.BRANCH_NAME}

I've 2 multibranch jenkins pipeline jobs connecting same github project. I'm triggering first MB job ex. dev. branch, now on success of this job I want to trigger second MB job with same branch name. (I've separate Jenkinsfile fot both MB job).

I've tried below options but didn't work:

(1)

build  job: 'jobName', parameters: [[$class: 'StringParameterValue', name: 'BRANCH_NAME', value: env.BRANCH_NAME]] 

This gives me error "Waiting for non-job items is not supported"

(2)

build  job: 'jobName/${branch_name}'

This gives me jobname/${branch_name} does not exist means variable here is not resolving, I've created this variable in environment directive

Simple jobName/dev if I'd give hardcode in Jenkinsfile then it's working, but I'd need it with automatically selecting branch name.

please advise, thanks.



Solution 1:[1]

I've found another solution for this issue:

In the jenkinsfile triggering pipeline job with gitParameter:

build   job: 'jobName', parameters: [gitParameter(name: 'GITBRANCH', value: env.BRANCH_NAME)]

Installed "git parameter" plugin, and configure in job which is being triggered in jenkinsfile

parameters {
    gitParameter branchFilter: 'origin/(.*)', defaultValue: 'master', name: 'GITBRANCH', type: 'PT_BRANCH'
    }

And calling this parameter while checkout code

stage("Checkout Branch wise"){
            steps{
                git branch: "${params.GITBRANCH}", url: 'https://xxx/xxx/xxxx.git'
            }
        }

which solved the problem.

Solution 2:[2]

When you want to use variables inside your string you need to use double quotes, example:

$"BRANCH_NAME"

From the groovy docs:

"Any Groovy expression can be interpolated in all string literals, apart from single and triple-single-quoted strings. "

More here: https://docs.groovy-lang.org/latest/html/documentation/#all-strings

An alternative could also be to use concatenation:

build job:'job/'+somebranchvariable, parameters:[string(name: 'BUILD_NAME', value: build_name)]

Solution 3:[3]

I have tried this format, it is working.

build(job: "JOBNAME" + "/" + "${BRANCH_NAME}".replaceAll('/', '%2F'))

Forward Slash (/) is converted to %2f in the job name: It’s a common practice to use a forward slash (/) when creating feature branches within developer teams, so whenever a new Pipeline gets created automatically with the creation of a new branch, / will get converted into %2f. Whenever you are referencing the branch name in the job or calling any job as a downstream, you will have to make sure of this conversion. Look at the below example where we are trying to call a downstream project.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jaydeep Soni
Solution 2
Solution 3 Raja S