3

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.

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)' -------------------#"
}

1 Answer 1

2

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
        }
11
  • 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_
    Sep 11, 2019 at 11:00
  • 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
    Sep 11, 2019 at 13:09
  • 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_
    Sep 12, 2019 at 4:37
  • @Manish_ I've edited my answer.
    – avi
    Sep 12, 2019 at 6:54
  • 1
    I have solved the issue. Thank you for your help.
    – Manish_
    Sep 19, 2019 at 10:15

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.