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

前端 未结 4 834
时光取名叫无心
时光取名叫无心 2020-12-30 09:09

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 orde

4条回答
  •  灰色年华
    2020-12-30 09:54

    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 = 'ci@example.com'
        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 git@github.com:..."
    
            // 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"
        }
    }
    

提交回复
热议问题