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