Hello Team,
I am trying to merge a release branch with the “main” branch via jenkins pipeline.
Some points to note:
main branch does not have a JenkinsFile. However, the release branch has. and thats what needs to be merged
In such a case how can I merge the release branch into main branch using Pipeline SCM. I am using SCM to give the user the flexibility to select the Source branch and Destination branch. (Can this be achieved without SCM?, if yes please provide a way out.)
I tried to checkout the release (source) branch and merged the main into it, but it got detached and found nothing to update, since the release branch was created from the main branch.
Alternatively, I tried to vice versa (checking out the main branch and then merging the release branch). But the jenkins got failed for the obvious reason that it did not find the “JenkinsFile” using which the Jenkins will perform the execution.
In such a scenario, I cant find a way around and hence requesting your assistance…
The checkout configuration that you’ve used is missing some crucial items if you want to use that repository to merge changes from other branches into the working branch of that job, especially if you intend to push the resulting merge to another repository (like GitHub, Bitbucket, GitLab, or Gitea).
The checkout command by default uses a “detached head” checkout because it assumes that a working branch configuration is not used by most Jenkins jobs. You can see that if you run the command git status in the Jenkins workspace immediately after checkout. It will report something like:
HEAD detached at c06af323
Most merge operations need a destination branch, so the checkout needs to add “checkout to specific local branch” with the name of the branch that will be the destination of the merge. Since you are merging from main to release, the branch name for local checkout will be release.
When pushing the changes back to the central repository, you’ll need to use either the Pipeline step withCredentials (if the repository URL uses https) or the sshAgent step (if the repository URL uses ssh).
Users that run a freestyle job, will use the Git publisher. Pipeline is better. Use Pipeline.
Let’s call the branch that has the extra content the “source branch”. Let’s call the branch that should receive the extra content the “destination branch”. The extra content from the source branch can be brought into the destination branch by performing a checkout of the destination branch, then performing a git merge source-branch.
Thus, if the source branch is main and the destination branch is release, the steps would be:
If the source branch is release and the destination branch is main, then the commands would be:
git checkout -b main -t origin/main
git merge origin/release
git push origin main
In order for the git plugin to best approximate the first command, git checkout -b destination-branch -t origin/destination-branch, you need to add the “checkout to specific local branch” option to the checkout step in Pipeline. If you don’t add that option, git will complain that it is using a detached head.
Since the repository URL uses ssh, you’ll need to use the sshAgent step rather than the withCredentials step. I’m not aware of any limitation for that step on Windows.
Hello Mark,
Thank you very much for the detailed explanation.
Not sure but I am not able to use sshagent . (It says could not find a suitable ssh-agent provider)
Secondly when I use withCredentials over ssh, I get “remote rejected”.
Everything works fine (incl merge), except git push where I get the “remote rejected” error.
My “main” branch has a restriction to commit (to an email address - devops@xx.com) and the ssh key is created for the same email address.
I attach the jenkins file. Line # 42-48 gives the error :