How to use Jenkins pre-build branch merging only for branches I want to merge?
I am reading https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin#GitPlugin-AdvancedFeatures which allows pre-builds of feature branches before pushing commits to the master branch, effectively implementing a pre-commit build/test queue.
It says under
Using Git, Jenkins and pre-build branch merging:
- How to Run TestNG Tests on Jenkins
- how do i get the latest build in jenkns with a specified parameter
- git push not working from jenkins docker
- Conditional OR for multiple Jenkins parameters
- Monitor jenkins jobs health using nagios GUI
- Jenkins - git global config not loaded
Set up your Jenkins project, and leave the ‘branch’ field in the Git
SCM blank. This will cause Jenkins to consider any change on any
branch for building.
Next, pick a particular branch name as the integration target in the
‘Advanced’ section – (e.g. ‘master’, or ‘stable’), and select ‘Merge
Select ‘Push GIT tags back to origin repository’ from the post-build
actions (this is required to update your centralised git repo with the
results of the build).
Now, developers should never commit directly to your integration
branch (the ‘master’ or ‘stable’). Instead, they should either use
feature branches, or create new remote branches on commit (e.g : “git
push origin HEAD:refs/heads/myNewFeature”). You could also set up your
GIT repository to only accept commits onto the integration branch from
You’re done. Commits should now be automatically merged with the
integration branch (they will fail if they do not merge cleanly), and
built. If the build succeeds, the result of the merge will be pushed
back to the remote git repository.
Now I often have feature branches that I would like to continue developing on, and only merge them into
As far as I understand, this setup will merge and push any feature branch into
master as soon as it builds.
(How) Can Jenkins support my use case, building all feature branches, but merging only those into
master that I indend to be merged?
2 Solutions collect form web for “How to use Jenkins pre-build branch merging only for branches I want to merge?”
I have a build pipeline where I only want the develop branch to merge back to my stable branch. All others should not be merged. The way I’ve solved this is to have a separate job for the merge. When the build job is complete, the ‘merge’ job is only triggered if the environment variable GIT_BRANCH equals ‘develop’.
Jenkins doesn’t have a very good conditional trigger that works the way I want. Here is how I set up my build job to conditionally trigger the merge step.
- Add a build step to remove a file called trigger.properties (I use the Execute Shell build step)
- Add a build step (conditional-buildstep plugin) Conditional step (single) to run if $GIT_BRANCH equals “origin/develop”. When true, it runs a shell command to create the trigger.properties file. (touch trigger.properties)
- Add a post-build action (Parameterized Trigger plugin) to trigger my merge job when the build is stable. In the Parameters from properties file section, fill in the field with “trigger.properties”. There is an option “Don’t trigger if any files are missing”, so check that.
So the process is that the trigger.properties file is first removed to make sure a previous run didn’t conflict. Then, check to see if the git branch is develop. If it is, recreate the trigger.properties file. The post-build step requires that file to exist for the next job to get triggered.
Now you can perform the kinds of merging you like in a job made just for that. You can also modify this process so that more than one branch makes it to the merge job.
Have you had a look at the pretested commit plugin for Jenkins?
It was designed for exactly the purpose you described.