<p>Do you have 2 different jenkins jobs configured for master and release branches? Since github webhook event is sent to Jenkins irrespective of branch, both your jenkins jobs may be getting triggered by this github webhook event. And since both jobs are configured with separate branches, they may be checking out those branches during jenkins job execution which is why you may be seeing respective branches being printed in both jenkins jobs.</p>
<p>Checkout <a href="https://www.jenkins.io/doc/book/pipeline/multibranch/" rel="nofollow noreferrer">jenkins multibranch pipeline</a> if you haven't already so you don't have to maintain multiple jenkins jobs for branches.</p>
<p>Now coming back to your Jenkinsfile code, you can try following:</p>
<ol>
<li><p>Replace <code>env.GIT_BRANCH</code> with <code>env.BRANCH_NAME</code></p>
</li>
<li><p>Use <code>allOf</code> inside when condition instead of <code>&amp;&amp;</code>  -</p>
<pre><code>stage(&quot;Step 3: Production Deployment&quot;){
    when {
        allOf {
            expression { env.BRANCH_NAME == &quot;origin/master&quot; }
            expression { params.merged == true }
            expression { params.current_status == &quot;closed&quot; }
        }
    }
    steps{
        -----
        sh 'kubectl apply -f **.yaml'
    } 
}
</code></pre>
</li>
</ol>
<p>Note that github webhook will trigger the jenkins job for every branch push or PR merge event (irrespective of branch), so your jenkins job would have to handle multi-branch scenarios like explained in this <a href="https://www.jenkins.io/doc/tutorials/build-a-multibranch-pipeline-project/#add-deliver-and-deploy-stages-to-your-pipeline" rel="nofollow noreferrer">official doc example</a>.</p>

CC BY-SA 4.0
2020-11-28T22:26:18.220Z
2020-11-28T22:26:18.220Z
drcloudycoder
<p>I am trying to trigger a jenkins pipeline job only when a pull request has been approved &amp; merged to a specific github branch in our CI/CD implementation.</p>
<p>currently whenever i merge a PR to <em>release</em>, jenkins job that is created to get triggered when PR is merged to <em>master</em> is also getting triggered, while I only want <em>release</em> jenkins job to be triggered.</p>
<p>strangely <em>echo env.GIT_BRANCH</em> cmd shows branch name as MASTER in <em>master</em> job, parallelly <em>release</em> job shows branch as RELEASE, when i only merged PR to release.</p>
<p>Things tried:</p>
<ol>
<li><em>Branches to build</em> feature in Jenkins</li>
<li><em>when</em> condition in Jenkinsfile (its acting weird, both staging and production jobs are getting triggered when one branch is targetted)</li>
</ol>
<p>Environment:
<em>Jenkins ver. 2.176.3, Docker,Generic webhook trigger</em> for Jenkins</p>
<p>I'm convinced that when condition will work, but for me it isn't, following is my partial <em>when</em> condition in Jenkinsfile:</p>
<pre><code>stage(&quot;Step 1: Build Docker&quot;){}
stage(&quot;Step 2: Push to DockerHub&quot;){}
stage(&quot;Step 3: Production Deployment&quot;){
            when {
                expression { return params.current_status == &quot;closed&quot; &amp;&amp; params.merged == true &amp;&amp; env.GIT_BRANCH == &quot;origin/master&quot; }
            }
            steps{
                -----
                sh 'kubectl apply -f **.yaml'
            }
}
</code></pre>
<p>In my jenkinsfile I'm writing logic to deploy my docker images to kubernetes cluster, so all that i  am looking for is to accurately trigger the job based on the branch.</p>
<p>Any suggestions on how to get it to work, what am i missing here?</p>

<jenkins><kubernetes><jenkins-pipeline><continuous-deployment>
branch specific jenkins pipeline trigger
cross_handle
# branch specific jenkins pipeline trigger

(Asked by: username_0 on 2020-11-03)

I am trying to trigger a jenkins pipeline job only when a pull request has been approved & merged to a specific github branch in our CI/CD implementation.
currently whenever i merge a PR to release, jenkins job that is created to get triggered when PR is merged to master is also getting triggered, while I only want release jenkins job to be triggered.
strangely echo env.GIT_BRANCH cmd shows branch name as MASTER in master job, parallelly release job shows branch as RELEASE, when i only merged PR to release.
Things tried:

Branches to build feature in Jenkins
when condition in Jenkinsfile (its acting weird, both staging and production jobs are getting triggered when one branch is targetted)

Environment:
Jenkins ver. 2.176.3, Docker,Generic webhook trigger for Jenkins
I'm convinced that when condition will work, but for me it isn't, following is my partial when condition in Jenkinsfile:
stage("Step 1: Build Docker"){}
stage("Step 2: Push to DockerHub"){}
stage("Step 3: Production Deployment"){
            when {
                expression { return params.current_status == "closed" && params.merged == true && env.GIT_BRANCH == "origin/master" }
            }
            steps{
                -----
                sh 'kubectl apply -f **.yaml'
            }
}

In my jenkinsfile I'm writing logic to deploy my docker images to kubernetes cluster, so all that i  am looking for is to accurately trigger the job based on the branch.
Any suggestions on how to get it to work, what am i missing here?

**Tags:** jenkins, kubernetes, jenkins-pipeline, continuous-deployment


---

## 1 answers

### Answer by username_1 on 2020-11-28. Score: 2

Do you have 2 different jenkins jobs configured for master and release branches? Since github webhook event is sent to Jenkins irrespective of branch, both your jenkins jobs may be getting triggered by this github webhook event. And since both jobs are configured with separate branches, they may be checking out those branches during jenkins job execution which is why you may be seeing respective branches being printed in both jenkins jobs.
Checkout jenkins multibranch pipeline if you haven't already so you don't have to maintain multiple jenkins jobs for branches.
Now coming back to your Jenkinsfile code, you can try following:

Replace env.GIT_BRANCH with env.BRANCH_NAME

Use allOf inside when condition instead of &&  -
stage("Step 3: Production Deployment"){
    when {
        allOf {
            expression { env.BRANCH_NAME == "origin/master" }
            expression { params.merged == true }
            expression { params.current_status == "closed" }
        }
    }
    steps{
        -----
        sh 'kubectl apply -f **.yaml'
    } 
}



Note that github webhook will trigger the jenkins job for every branch push or PR merge event (irrespective of branch), so your jenkins job would have to handle multi-branch scenarios like explained in this official doc example.


---

