How do I tag the current git changeset from inside the Jenkinsfile?
I want to share my Jenkins Pipeline Setup and my solution to publish changes/tags to git repo via SSH (While Git Publish Support is under development). Please check it out for more info, any improvement ideas are welcome.
In short you just add file git_push_ssh.groovy
to your project and call method pushSSH()
from Jenkinsfile like this:
env.BRANCH_NAME = "mycoolbranch"// BRANCH_NAME is predefined in multibranch pipeline job
env.J_GIT_CONFIG = "true"
env.J_USERNAME = "Jenkins CI"
env.J_EMAIL = "[email protected]"
env.J_CREDS_IDS = '02aa92ec-593e-4a90-ac85-3f43a06cfae3' // Use credentials id from Jenkins
def gitLib = load "git_push_ssh.groovy"
...
gitLib.pushSSH(commitMsg: "Jenkins build #${env.BUILD_NUMBER}", tagName: "build-${env.BUILD_NUMBER}", files: "changelog.txt someotherfile.txt");
Here is the way I was able to implement this this way, but if you know a better way I am more than willing to hear it.
#!groovy
stage 'build'
node {
repositoryCommiterEmail = '[email protected]'
repositoryCommiterUsername = 'examle.com'
checkout scm
sh "echo done"
if (env.BRANCH_NAME == 'master') {
stage 'tagging'
sh("git config user.email ${repositoryCommiterEmail}")
sh("git config user.name '${repositoryCommiterUsername}'")
sh "git remote set-url origin [email protected]:..."
// deletes current snapshot tag
sh "git tag -d snapshot || true"
// tags current changeset
sh "git tag -a snapshot -m \"passed CI\""
// deletes tag on remote in order not to fail pushing the new one
sh "git push origin :refs/tags/snapshot"
// pushes the tags
sh "git push --tags"
}
}