How to push changes to github after jenkins build completes?
I have a jenkins job that clones the repository from github, then runs the powershell script that increments the version number in the file. I'm now trying to publish that update file back to the original repository on github, so when developer pulls the changes he gets the latest version number.
I tried using Git Publisher in the post build events, and I can publish tags with no issues, but it doesn't seem to publish any files.
Solution 1:
The git checkout master
of the answer by Woland isn't needed. Instead use the "Checkout to specific local branch" in the "Additional Behaviors" section to set the "Branch name" to master.
The git commit -am "blah"
is still needed.
Now you can use the "Git Publisher" under "Post-build Actions" to push the changes. Be sure to specify the "Branches" to push ("Branch to push" = master, "Target remote name" = origin).
"Merge Results" isn't needed.
Solution 2:
Found an answer myself, this blog helped: http://thingsyoudidntknowaboutjenkins.tumblr.com/post/23596855946/git-plugin-part-3
Basically need to execute:
git checkout master
before modifying any files
then
git commit -am "Updated version number"
after modified files
and then use post build action of Git Publisher with an option of Merge Results which will push changes to github on successful build.
Solution 3:
Actually, the "Checkout to specific local branch"
from Claus's answer isn't needed as well.
You can just do changes, execute git commit -am "message"
and then use "Git Publisher" with "Branch to push" = /refs/heads/master
(or develop or whatever branch you need to push to), "Target remote name" = origin.
Solution 4:
Once you set your Global Jenkins credentials, you can apply this step:
stage('Update GIT') {
steps {
script {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
withCredentials([usernamePassword(credentialsId: 'example-secure', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
def encodedPassword = URLEncoder.encode("$GIT_PASSWORD",'UTF-8')
sh "git config user.email [email protected]"
sh "git config user.name example"
sh "git add ."
sh "git commit -m 'Triggered Build: ${env.BUILD_NUMBER}'"
sh "git push https://${GIT_USERNAME}:${encodedPassword}@github.com/${GIT_USERNAME}/example.git"
}
}
}
}
}
Solution 5:
I followed the below Steps. It worked for me.
In Jenkins execute shell under Build, creating a file and trying to push that file from Jenkins workspace to GitHub.
Download Git Publisher Plugin and Configure as shown below snapshot.
Click on Save and Build. Now you can check your git repository whether the file was pushed successfully or not.