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)' -------------------#"
}
Manish_
  • 108
  • 1
  • 1
  • 7

1 Answers1

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
        }
avi
  • 1,279
  • 1
  • 13
  • 32
  • 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 '19 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 '19 at 13:09
  • The relevant branch for PR is develop branch for now. But I have to do the same for master as well.
  • This job does runs for develop branch as you have also seen the image.
  • – Manish_ Sep 12 '19 at 04:37
  • @Manish_ I've edited my answer. – avi Sep 12 '19 at 06:54
  • I checked it, but it is returning null for env.BRANCH_NAME – Manish_ Sep 12 '19 at 08:25
  • branchName will default to CHANGE_TARGET if the current code isn't working try ${env.CHANGE_TARGET} and ${env.BRANCH_NAME} instead. – avi Sep 12 '19 at 09:10
  • Thank you it solved the issue. What is the case for multiple branches? Where should I make changes? – Manish_ Sep 12 '19 at 10:24
  • Have conditions based in the value of branchame. – avi Sep 15 '19 at 20:51
  • 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_ Sep 16 '19 at 05:57
  • I'm not sure I'm understanding sorry. Maybe edit your question with the current code and problem? – avi Sep 16 '19 at 21:00
  • 1
    I have solved the issue. Thank you for your help. – Manish_ Sep 19 '19 at 10:15