Reputation: 845
I'm using the current version of the Git plugin (SCM provider and Publisher) on Jenkins 1.500. The SCM polling works fine, using my git HTTP URL and "branches to build" setting of "feature-*". This picks up changes to any branch, e.g., "feature-1234", runs the build/test/coverage tasks, and reports on success or failure. All of this works fine, including a merge from the integration branch where the code should end up after a successful build and code review.
The problem is in trying to push the completed build branch BACK to origin, on the same "feature-1234" branch. The build variable "GIT_BRANCH" in this case contains "origin/feature-1234", which produces the following error and failure in the Git Publisher after an otherwise successful build:
Pushing HEAD to branch origin/feature-1234 at repo origin
ERROR: Failed to push branch origin/feature-1234 to origin
hudson.plugins.git.GitException: Command "/usr/bin/git push https://jenkins:[email protected]/git/project HEAD:origin/feature-1234" returned status code 1:
stdout:
stderr: error: unable to push to unqualified destination: origin/feature-1234
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'https://jenkins:[email protected]/git/project'
at hudson.plugins.git.GitAPI.launchCommandIn(GitAPI.java:897)
at hudson.plugins.git.GitAPI.launchCommand(GitAPI.java:858)
at hudson.plugins.git.GitAPI.push(GitAPI.java:915)
at hudson.plugins.git.GitPublisher$4.invoke(GitPublisher.java:351)
at hudson.plugins.git.GitPublisher$4.invoke(GitPublisher.java:333)
at hudson.FilePath.act(FilePath.java:865)
at hudson.FilePath.act(FilePath.java:838)
at hudson.plugins.git.GitPublisher.perform(GitPublisher.java:333)
at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:36)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:810)
at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:785)
at hudson.model.Build$BuildExecution.post2(Build.java:183)
at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:732)
at hudson.model.Run.execute(Run.java:1582)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:236)
Build step 'Git Publisher' changed build result to FAILURE
Build step 'Git Publisher' marked build as failure
See the extra "origin" in there? origin/feature-1234 <== that's the current value of ${GIT_BRANCH}, and while I understand it's a remote branch and all, it's stopping me from running the process we want to follow.
If I'm missing something simple, I'd love to hear it. But I have tried many different settings for the various git-related portions of my build and nothing seems to allow me to commit the merged and tested code back to the work branch (as opposed to the integration branch, which is easy to do.
Upvotes: 10
Views: 17193
Reputation: 1
First, look for GIT_LOCAL_BRANCH
variable
(https://plugins.jenkins.io/git/#branch-variables)
In my case it was missing, so I extracted it from GIT_BRANCH
variable, as a dirty workaround
$ GIT_LOCAL_BRANCH=$(echo ${GIT_BRANCH} | awk -F\/ '{print $2}')
$ echo $GIT_LOCAL_BRANCH
feature-1234
Upvotes: 0
Reputation: 607
In the build section Add "Execute Shell Script" use this shell script:
echo current Branch is ${GIT_BRANCH#*/}
git checkout ${GIT_BRANCH#*/}
git commit -am 'Your commit message'
git push origin ${GIT_BRANCH#*/}
This should be the last step in the build section.
Upvotes: 0
Reputation: 5959
I recommend to configure fully qualified /refs/remotes/origin/feature-1234
as your Git "Branch to build". This way it seems more understandable to me.
In such case your $GIT_BRANCH would get magically set by Jenkins to origin/feature-1234
same as you observed.
Then, instead of using overly complicated (but portable) GitPublisher, just add a post-build step "Execute Shell":
echo Remote branch is $GIT_BRANCH, replacing origin with refs/heads.
git push --follow-tags "$GIT_URL" "+HEAD:${GIT_BRANCH/#origin\//refs/heads/}"
Upvotes: 1
Reputation: 1861
I push changes by hand. You can use the windows environment variable as below in batch scripts:
This evaluates to eg. 'git checkout feature-1234'
git checkout %GIT_BRANCH:origin/= %
This evaluates to eg. 'git push origin feature-1234'
git push %GIT_BRANCH:/= %
You can also do something similar with token macros such as ${GIT_BRANCH##origin/}
and ${GIT_BRANCH#*/}
. These work in some Jenkins plugins, but not all, so it might not work in the Git Publisher.
Upvotes: 1
Reputation: 529
In the build configuration, "Source Code Management" => Advance There is a field "Name" You need to specify the ID of the repository and then to use it at the git publisher plugin: "Target remote name"
Those two names need to be identical look at the "?" - to get more information
Upvotes: 1
Reputation: 546
A workaround for me seems to push the branch manually, at least when it is going to be the same each time.
Upvotes: 1