<p>You'll want to use the CHANGE_TARGET environment variable.  If that variable exists then you are dealing with a merge request. </p>

<p>You can use the following code snipped to then determine the branch name and alter the job's logic depending on which branch the code is being merged into.</p>

<p><code>branchName = env.CHANGE_TARGET ? env.CHANGE_TARGET : env.BRANCH_NAME</code></p>

<p>In your code example you would replace  </p>

<pre><code>        expression {
         return env.BRANCH_NAME != 'feature_cicd'
         echo env.BRANCH_NAME
        }
</code></pre>

<p>with   </p>

<pre><code>        expression {
         branchName = env.CHANGE_TARGET ? env.CHANGE_TARGET : env.BRANCH_NAME
         return branchName != 'feature_cicd'
         echo branchName
        }
</code></pre>

CC BY-SA 4.0
2019-09-11T11:00:58.597Z
where should I define this.
Can you help me with that?
Since I want it to be triggered based on different branches.
Say, if any push or merge is done in the master branch, I want to trigger only master branch jobs and for changes in develop branch, I want to trigger only develop branch jobs.
Manish_
CC BY-SA 4.0
2019-09-11T13:09:20.233Z
There are two seperate issues.
1. What is the relevant branch for this PR
2. Does the job run at all?
For issue 2 you need to change the jenkins pipeline setttings in the job configuration. There's a few plugins for that.
avi
CC BY-SA 4.0
2019-09-12T04:37:26.947Z
1. The relevant branch for PR is develop branch for now. But I have to do the same for master as well.
2. This job does runs for develop branch as you have also seen the image.
Manish_
CC BY-SA 4.0
2019-09-12T06:54:00.600Z
@Manish_ I've edited my answer.
avi
CC BY-SA 4.0
2019-09-12T08:25:26.323Z
I checked it, but it is returning null for env.BRANCH_NAME
Manish_
CC BY-SA 4.0
2019-09-12T09:10:05.880Z
branchName will default to CHANGE_TARGET if the current code isn't working try ${env.CHANGE_TARGET} and ${env.BRANCH_NAME} instead.
avi
CC BY-SA 4.0
2019-09-12T10:24:04.153Z
Thank you it solved the issue.
What is the case for multiple branches? Where should I make changes?
Manish_
CC BY-SA 4.0
2019-09-15T20:51:48.063Z
Have conditions based in the value of branchame.
avi
CC BY-SA 4.0
2019-09-16T05:57:18.817Z
When i use this and instead of return branchName != 'feature_cicd', when I use  return branchName == 'master', my steps does not execute with the console output of Jenkins referring to, skipping stage due to when condition.
Am I doing something worng or is it the way Jenkins behave?
Manish_
CC BY-SA 4.0
2019-09-16T21:00:35.267Z
I'm not sure I'm understanding sorry.  Maybe edit your question with the current code and problem?
avi
CC BY-SA 4.0
2019-09-19T10:15:43.237Z
I have solved the issue.
Thank you for your help.
Manish_
CC BY-SA 4.0
2019-09-09T06:54:09.213Z
2019-09-12T06:53:43.790Z
2019-09-12T06:53:43.790Z
avi
<p>I am having a problem to trigger Jenkins pipeline job based on merge any branches to develop branch only. I also did not find any webhook for merge as like bitbucket. I also tried using pull request but that triggered the job on any changes to the PR branch which is not my requirement.</p>

<p>I have also shared my code below and my Jenkins Configuration.
<a href="https://i.sstatic.net/nhcDV.png" rel="nofollow noreferrer">Jenkins Configuration</a> also please note that it is develop branch but in the image I've shown <code>*/feature</code> I have changed it to develop but still the issue is the same.</p>

<pre><code>def CONTAINER_NAME=""
def CONTAINER_TAG="${env.BUILD_NUMBER}"
def CONTAINER_PORT=""
def ECR_REPO=""
def HOST_PORT=""
def URL=""
def INSTANCE_IP=""

pipeline {
  agent any
  stages {
    stage('Image Build Develop') {
      when {
        expression {
         return env.BRANCH_NAME != 'feature_cicd'
         echo env.BRANCH_NAME
        }
      } 
      steps {
        git branch: "develop", url: 'https://github.com/jenkinsci/git-parameter-plugin.git' 
        imageBuildDevelop(CONTAINER_NAME, CONTAINER_TAG)
        }
    }

    stage('Image Tag') {
      steps {
        imageTag(CONTAINER_NAME, CONTAINER_TAG, ECR_REPO)
      }
    }

    stage('Image Push') {
      steps {
        imagePush(CONTAINER_NAME, CONTAINER_TAG, ECR_REPO)
      }
    }

    stage('Image Deploy') {
      steps {
        imageDeploy(HOST_PORT, env.BUILD_NUMBER, INSTANCE_IP)
      }
    }
  }
}

def imageBuildDevelop(containerName, tag) {
  echo "#------------------- Checkout Develop Branch -------------------#"
  git branch: "feature_cicd", url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
  echo "#------------------- Login into ECR Repository -------------------#"
  sh "`/var/jenkins_home/.local/bin/aws ecr get-login --no-include-email`"
  echo "#------------------- Build Docker Image for Media-API -------------------#"
  sh "docker build -t $containerName:$tag -t $containerName ."
  echo "#------------------- Image Build Complete -------------------#"
}

def imageTag(containerName, tag, repo) {
  echo "#------------------- Tag media-api image -------------------#"
  sh "docker tag $containerName:$tag $repo:$containerName-$tag"
  echo "#------------------- Image Tag Complete ------------------#"
}

def imagePush(containerName, tag, repo) {
  echo "#------------------- Login into ECR Repository -------------------#"
  sh "`/var/jenkins_home/.local/bin/aws ecr get-login --no-include-email`"
  echo "#------------------- Push Image into ECR Repository -------------------#"
  sh "docker push $repo:$containerName-$tag"
  echo "#------------------- Image Push to ECR Repository Complete -------------------#"
}

def imageDeploy(hostPort, tag, ip) {
  echo "#------------------- SSH into media-api instance -------------------#"
  sh "ssh ubuntu@$ip \"BUILD_NUMBER=$tag\" \
  ' cd reactApi_test/ &amp;&amp;\
    echo \"TAG=${BUILD_NUMBER}\" &gt; .env &amp;&amp;\
    `aws ecr get-login --no-include-email` &amp;&amp;\
    docker-compose up -d --build &amp;&amp;\
    ./test.sh '"
  echo "#------------------- Media-API container started on Port '${hostPort} (http)' -------------------#"
}
</code></pre>

<jenkins-pipeline>
How to trigger jenkins pipeline only on merge to develop branch from github webhook trigger?
Manish_
# How to trigger jenkins pipeline only on merge to develop branch from github webhook trigger?

(Asked by: username_0 on 2019-09-08)

I am husername_1ng a problem to trigger Jenkins pipeline job based on merge any branches to develop branch only. I also did not find any webhook for merge as like bitbucket. I also tried using pull request but that triggered the job on any changes to the PR branch which is not my requirement.
I have also shared my code below and my Jenkins Configuration.
Jenkins Configuration also please note that it is develop branch but in the image I've shown */feature I have changed it to develop but still the issue is the same.
def CONTAINER_NAME=""
def CONTAINER_TAG="${env.BUILD_NUMBER}"
def CONTAINER_PORT=""
def ECR_REPO=""
def HOST_PORT=""
def URL=""
def INSTANCE_IP=""

pipeline {
  agent any
  stages {
    stage('Image Build Develop') {
      when {
        expression {
         return env.BRANCH_NAME != 'feature_cicd'
         echo env.BRANCH_NAME
        }
      } 
      steps {
        git branch: "develop", url: 'https://github.com/jenkinsci/git-parameter-plugin.git' 
        imageBuildDevelop(CONTAINER_NAME, CONTAINER_TAG)
        }
    }

    stage('Image Tag') {
      steps {
        imageTag(CONTAINER_NAME, CONTAINER_TAG, ECR_REPO)
      }
    }

    stage('Image Push') {
      steps {
        imagePush(CONTAINER_NAME, CONTAINER_TAG, ECR_REPO)
      }
    }

    stage('Image Deploy') {
      steps {
        imageDeploy(HOST_PORT, env.BUILD_NUMBER, INSTANCE_IP)
      }
    }
  }
}

def imageBuildDevelop(containerName, tag) {
  echo "#------------------- Checkout Develop Branch -------------------#"
  git branch: "feature_cicd", url: 'https://github.com/jenkinsci/git-parameter-plugin.git'
  echo "#------------------- Login into ECR Repository -------------------#"
  sh "`/var/jenkins_home/.local/bin/aws ecr get-login --no-include-email`"
  echo "#------------------- Build Docker Image for Media-API -------------------#"
  sh "docker build -t $containerName:$tag -t $containerName ."
  echo "#------------------- Image Build Complete -------------------#"
}

def imageTag(containerName, tag, repo) {
  echo "#------------------- Tag media-api image -------------------#"
  sh "docker tag $containerName:$tag $repo:$containerName-$tag"
  echo "#------------------- Image Tag Complete ------------------#"
}

def imagePush(containerName, tag, repo) {
  echo "#------------------- Login into ECR Repository -------------------#"
  sh "`/var/jenkins_home/.local/bin/aws ecr get-login --no-include-email`"
  echo "#------------------- Push Image into ECR Repository -------------------#"
  sh "docker push $repo:$containerName-$tag"
  echo "#------------------- Image Push to ECR Repository Complete -------------------#"
}

def imageDeploy(hostPort, tag, ip) {
  echo "#------------------- SSH into media-api instance -------------------#"
  sh "ssh ubuntu@$ip \"BUILD_NUMBER=$tag\" \
  ' cd reactApi_test/ &&\
    echo \"TAG=${BUILD_NUMBER}\" > .env &&\
    `aws ecr get-login --no-include-email` &&\
    docker-compose up -d --build &&\
    ./test.sh '"
  echo "#------------------- Media-API container started on Port '${hostPort} (http)' -------------------#"
}

**Tags:** jenkins-pipeline


---

## 1 answers

### Answer by username_1 on 2019-09-09. Score: 2 | Accepted answer

You'll want to use the CHANGE_TARGET environment variable.  If that variable exists then you are dealing with a merge request. 
You can use the following code snipped to then determine the branch name and alter the job's logic depending on which branch the code is being merged into.
branchName = env.CHANGE_TARGET ? env.CHANGE_TARGET : env.BRANCH_NAME
In your code example you would replace  
        expression {
         return env.BRANCH_NAME != 'feature_cicd'
         echo env.BRANCH_NAME
        }

with   
        expression {
         branchName = env.CHANGE_TARGET ? env.CHANGE_TARGET : env.BRANCH_NAME
         return branchName != 'feature_cicd'
         echo branchName
        }

> **Comments:**
> **username_0**: where should I define this. Can you help me with that? Since I want it to be triggered based on different branches. Say, if any push or merge is done in the master branch, I want to trigger only master branch jobs and for changes in develop branch, I want to trigger only develop branch jobs.  
> **username_1**: There are two seperate issues. 1. What is the relevant branch for this PR 2. Does the job run at all? For issue 2 you need to change the jenkins pipeline setttings in the job configuration. There's a few plugins for that.  
> **username_0**: 1. The relevant branch for PR is develop branch for now. But I have to do the same for master as well. 2. This job does runs for develop branch as you have also seen the image.  
> **username_1**: @username_0 I've edited my answer.  
> **username_0**: I checked it, but it is returning null for env.BRANCH_NAME  
> **username_1**: branchName will default to CHANGE_TARGET if the current code isn't working try ${env.CHANGE_TARGET} and ${env.BRANCH_NAME} instead.  
> **username_0**: Thank you it solved the issue. What is the case for multiple branches? Where should I make changes?  
> **username_1**: Have conditions based in the value of branchame.  
> **username_0**: When i use this and instead of return branchName != 'feature_cicd', when I use  return branchName == 'master', my steps does not execute with the console output of Jenkins referring to, skipping stage due to when condition. Am I doing something worng or is it the way Jenkins behave?  
> **username_1**: I'm not sure I'm understanding sorry.  Maybe edit your question with the current code and problem?  
> **username_0**: I have solved the issue. Thank you for your help.  

---

