Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Git Publisher: How to commit code back to master after build?

I'm having some difficulty with Jenkins Git Publisher committing and pushing code back to master after my build. My build process increases a version number in one of my files and then I want to commit this file back into the repo, but I can't seem to get it to work.

In Source Code Management->Git, these are my settings:

  • Repository Name: Android
  • Branch Specifier: master
  • Checkout/merge to local branch: master

Then, in Git Publisher, these are my settings:

  • Push Only If Build Succeeds: checked
  • Merge Results: checked
  • Branch to push: master
  • Target remote name: Android
  • Notes: Note to push: Updating version
  • Notes: Target remote name: Android
  • Notes: Note's namespace: master

This is the output from Jenkins:

Pushing HEAD to branch master at repo Android
Adding note to namespace "master":
Updating version

Please help!

like image 751
Karim Varela Avatar asked Jul 18 '13 19:07

Karim Varela


People also ask

Can I use Git publisher with Jenkins plugins?

These work in some Jenkins plugins, but not all, so it might not work in the Git Publisher. I recommend to configure fully qualified /refs/remotes/origin/feature-1234 as your Git "Branch to build".

How to use Jenkins auto-build when git commit?

In this tutorial, How to use Jenkins auto-build when git commit. You use a webhook to capture when a new git commit was made and Jenkins will start to build jobs. Ref to Install Jenkins on AWS EC2. Install the Git and Github plugins.

How do I commit a Jenkins job to a repository?

Under Source Code Management, choose ‘Git’. Add the URL of the repository and the credentials. (Jenkins will attempt to authenticate against this URL as a test, so it should give you an error promptly if the authentication fails.) Use the commit hash given when the job was started by typing $ {COMMIT_HASH} in the Branch Specifier field.

How do I merge branches in Jenkins with Git?

From the Jenkins home page, click on Demo in the Name column. Make sure “Branch Specifier” for Branches to build is blank. For Additional Behaviors, add Merge before Build. Set the name of the repository to origin. Set the branch to merge to master. Add the Post Build Action Git Publisher. Select Push Only If Build Succeeds. Select Merge Results.


2 Answers

I think jenkins git publisher plugin is not doing anything like

git add .
git commit -m 'xxx'

Plugin only perform push and optionally add note using git-notes.

See notes here:

https://github.com/hamsterready/jenkins-git-publisher-test/tree/refs/notes/master

To achieve something like this: https://github.com/hamsterready/jenkins-git-publisher-test/commit/d80a1eef2133bee6d7a57b1b229ccd5990d2d606

I have added post-build step (execute shell script) with:

git add .
git commit -m 'Updating git.properties'

And then enabled git publisher post-build action which pushed local commit to origin.

like image 184
Maciej Łopaciński Avatar answered Sep 27 '22 21:09

Maciej Łopaciński


If you are using also Gradle for your builds, there is a Git plugin for it.

Here is the complete build.gradle:

buildscript {
  repositories { mavenCentral() }
  dependencies { classpath "org.ajoberstar:gradle-git:0.6.3" }
}
import org.ajoberstar.gradle.git.tasks.*

task tag(type: GitTag) {
    tagName = version
    message = "Release of $version"
}

task pushWithTags(type: GitPush){
    credentials{
        username = "karim"
        password = gitPassword
    }
    setPushTags(true)
}
task add(type: GitAdd){
    include("yourVersionFile.txt") 
    // or add everything with include("*") 
}
task commit(type: GitCommit){
    setMessage(commitMsg)
}
task pushNewVersion(){
    tasks.add.execute()
    tasks.commit.execute()
    tasks.tag.execute()
    tasks.pushWithTags.execute()
}

This is how you add, tag, commit, and push using the script (there is a plugin for doing that from within Jenkins):

gradle pushNewVersion "-PcommitMsg=hi" "-Pversion=0.1.1" "-PgitPassword=secret"
like image 23
Matthias Braun Avatar answered Sep 27 '22 21:09

Matthias Braun