HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to trigger jenkins pipeline only on merge to develop branch from github webhook trigger?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
fromtriggermergejenkinsdevelopgithubhowbranchonlypipeline

Problem

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 imageTa

Solution

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
        }

Code Snippets

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

Context

StackExchange DevOps Q#9120, answer score: 2

Revisions (0)

No revisions yet.