3

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:

  1. Branches to build feature in Jenkins
  2. 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?

1 Answer 1

2

Do you have 2 different jenkins jobs configured for master and release branches? Since github webhook event is sent to Jenkins irrespective of branch, both your jenkins jobs may be getting triggered by this github webhook event. And since both jobs are configured with separate branches, they may be checking out those branches during jenkins job execution which is why you may be seeing respective branches being printed in both jenkins jobs.

Checkout jenkins multibranch pipeline if you haven't already so you don't have to maintain multiple jenkins jobs for branches.

Now coming back to your Jenkinsfile code, you can try following:

  1. Replace env.GIT_BRANCH with env.BRANCH_NAME

  2. Use allOf inside when condition instead of && -

    stage("Step 3: Production Deployment"){
        when {
            allOf {
                expression { env.BRANCH_NAME == "origin/master" }
                expression { params.merged == true }
                expression { params.current_status == "closed" }
            }
        }
        steps{
            -----
            sh 'kubectl apply -f **.yaml'
        } 
    }
    

Note that github webhook will trigger the jenkins job for every branch push or PR merge event (irrespective of branch), so your jenkins job would have to handle multi-branch scenarios like explained in this official doc example.

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.