Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins + Git Publisher - How to push back to {GIT_BRANCH}?

Tags:

git

jenkins

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.

like image 631
mtutty Avatar asked Feb 08 '13 05:02

mtutty


People also ask

How do I push changes to GitHub after Jenkins build completes?

1 Answer. There is an option - "Checkout to specific local branch" that can be found in the section - "Additional Behaviors" for setting the "Branch name" as master. Mind you, git commit -am "blah" is still necessary. Now you could use the 'Git Publisher' under the 'Post-build Actions' for pushing changes.

How does Jenkins push code to GitHub?

Step 1: go to your GitHub repository and click on 'Settings'. Step 2: Click on Webhooks and then click on 'Add webhook'. Step 3: In the 'Payload URL' field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/.

Is Jenkins pulling or pushing the code?

That's it, you've setup your Jenkins job and it will run every time you push your code into your Git repository.


3 Answers

A workaround for me seems to push the branch manually, at least when it is going to be the same each time.

like image 83
Marius K Avatar answered Sep 17 '22 16:09

Marius K


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

like image 44
kazerm Avatar answered Sep 20 '22 16:09

kazerm


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.

like image 30
cedd Avatar answered Sep 20 '22 16:09

cedd