I am trying to trigger a jenkins pipeline job only when a pull request has been approved & merged to a specific github branch in our CI/CD implementation.
currently whenever i merge a PR to release, jenkins job that is created to get triggered when PR is merged to master is also getting triggered, while I only want release jenkins job to be triggered.
strangely echo env.GIT_BRANCH cmd shows branch name as MASTER in master job, parallelly release job shows branch as RELEASE, when i only merged PR to release.
Things tried:
- Branches to build feature in Jenkins
- when condition in Jenkinsfile (its acting weird, both staging and production jobs are getting triggered when one branch is targetted)
Environment: Jenkins ver. 2.176.3, Docker,Generic webhook trigger for Jenkins
I'm convinced that when condition will work, but for me it isn't, following is my partial when condition in Jenkinsfile:
stage("Step 1: Build Docker"){}
stage("Step 2: Push to DockerHub"){}
stage("Step 3: Production Deployment"){
when {
expression { return params.current_status == "closed" && params.merged == true && env.GIT_BRANCH == "origin/master" }
}
steps{
-----
sh 'kubectl apply -f **.yaml'
}
}
In my jenkinsfile I'm writing logic to deploy my docker images to kubernetes cluster, so all that i am looking for is to accurately trigger the job based on the branch.
Any suggestions on how to get it to work, what am i missing here?