Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tag the current git changeset from inside the Jenkinsfile?

I want to tag the current git changeset and push the tag from inside the Jenkinsfile. If the tag already exists it must be replaced.

I want to use this logic in order to tag the build that passed with the snapshot tag, which would be a mobile tag.

How can I do this?

like image 999
sorin Avatar asked Apr 01 '16 00:04

sorin


People also ask

How do I tag in Git?

To create a Git tag with a message, use the “git tag” command with the “-a” option for “annotated” and the “-m” option for message. Note : if you don't provide the “-m” option, your default text editor will open in order for you to type the tag message.

How do I pass tag name in Jenkins pipeline?

In the "Git Repository" section of your job, under the "Source Code Management" heading, click "Advanced". Under "Branches to build", "Branch specifier", put */tags/<TAG_TO_BUILD> (replacing <TAG_TO_BUILD> with your actual tag name).

How do you fetch a tag?

To fetch tags from your remote repository, use “git fetch” with the “–all” and the “–tags” options. Let's say for example that you have a tag named “v1. 0” that you want to check out in a branch named “release”. Using this command, you have successfully checked out the “v1.


3 Answers

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"
    }
}
like image 140
sorin Avatar answered Nov 02 '22 08:11

sorin


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");
like image 38
Amaksoft Avatar answered Nov 02 '22 10:11

Amaksoft


For people that couldn't get the above working I used the sshagent plugin directly, which did the trick:

stage('tag build'){
checkout([
    $class: 'GitSCM', branches: [[name: '*/master']],
    userRemoteConfigs: [[credentialsId: 'git',
    url: 'ssh://<ssh URL>']],
    extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'targeted-dir']]
])

sshagent(credentials: ['<credentials ID.']){
  dir('targeted-dir'){
    sh("git config user.email '<email>")
    sh("git config user.name '<user>.com'")

    // deletes current snapshot tag
    sh ("git tag -d ${PARAM_VERSION_NUMBER} || true")
    // tags current changeset
    sh ("git tag -a ${PARAM_VERSION_NUMBER} -m \"versioning ${PARAM_VERSION_NUMBER}\"")
    // 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")
    }
}

}

like image 29
Nick Brown Avatar answered Nov 02 '22 08:11

Nick Brown