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

22,308

Solution 1

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.

Solution 2

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"
Share:
22,308
Karim Varela
Author by

Karim Varela

Technology executive, investor, and advisor with a career history of producing highly scalable digital consumer products. Exceptional record of success in building and leading top performing, global engineering teams, managing budgets as large as $5MM, and bringing order and agility to growing engineering teams. Excels at implementing agile engineering practices, building product-focused engineering teams, and delivering software on time. Outstanding problem solving, leadership, and team motivation skills. —————— Core skills —————— Transformational Leadership: Carried out agile restructurings and process improvements which have resulted in massive improvements to engineering productivity. Platform Migration: Carried out multiple platform and database migrations resulting in cost-savings, improved operational efficiency, and better system performance. Global Workforce Management: Supervised as many as 25 people, across distributed teams in the US, Mexico, Argentina, Dominican Republic, Poland, Ukraine, London, India, and The Netherlands. Scaling Products Globally: Played key roles in localizing and distributing mobile apps and services to key international markets, growing user bases to tens of millions of users. System Architecture: Architected and built multiple secure and performant systems from the ground up to handle workloads in social, banking, and travel. —————— Specific Skills —————— Process Optimization Strategy, Planning, & Execution Engineering Operations Budget & Cost Control Product Management Lean & Agile Methodologies Cloud Architecture Microservices ETL Web Services Relational Databases NoSQL Databases Coaching & Mentoring Machine Learning

Updated on July 10, 2022

Comments

  • Karim Varela
    Karim Varela almost 2 years

    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!