2
votes

Can anyone help me with any solution for the below issue:

My developer is pushing the code in Dev branch of Bitbucket but in Jenkins, it triggers Dev, Test, Stage and Prod pipeline though there are no changes deployed in Other branches and it simply redeploys the last commit in Test, Stage, and Prod. But then why should any code push to the DEV branch trigger another pipeline.

Please note that every env pipeline is only checking out the respective env branch for deployment. I had raised this issue with the Bitbucket community as well but they confirmed that no issues are seen from Bitbucket's end and suggested to check from Jenkins's end.

In my Jenkins, every pipeline is triggered based on "Build whenever a change is pushed to Bitbucket"

1
What kind of pipeline job do you use? "Pipeline" or "Multibranch Pipeline"? To support multiple branches with a single Jenkins pipeline job, make sure you use the "Multibranch Pipeline" type of job. It runs the pipeline on specific branches, so when you push changes to one branch, only this branch gets executed. - Szymon Stepniak
I am using Pipeline job. Every branch has it own separate pipeline created. Not using "Multibranch Pipeline" - Monali
Consider using the "Multibranch Pipeline" job instead. A single Jenkins job, every branch handled separately, and pushing changes to one branch triggers only the target branch, not all of them. - Szymon Stepniak
Correct single Jenkins job for each branch should trigger only when changes are pushed to that branch but then other pipelines are also getting triggered for which even I finding an explanation as to why other branches are considering this web hook post request in repository and triggers the pipeline. Also this thing doesn't happen if I am creating new pipeline, this behavior is noticed only when pipeline that hasn't been triggered for quite sometime, is now being triggered for some new changes which in all is triggering all the pipelines. - Monali

1 Answers

1
votes

Multi-branch Pipeline should be the way to go here. Makes your file much easier.

Alternative would be to write a script block in your declarative pipelines (or write scripted pipeline directly) which would evaluate the git.branch env variable and only run the job when the branch is correct.

This would work and be somewhat maintainable when you have a fixed set of branches. If you have dev-s creating new branches here and there then this would become overwhelming very fast.