[GIT]获取git最新的tag

背景

公司前端项目在Jenkins中打包,每次打包需要将新tag回推到仓库中。但是打包失败后如果不删除tag的话下次打包就会失败,需要手动删除,所以在Jenkinsfile中就需要在打包失败时自动删除tag

解决方法

用于查找最近的tag

git describe

把--abbrev设为0, 该命令查找最近的tag名,不需要后缀:

git describe --abbrev=0

获取当前分支的tag

git describe --abbrev=0 --tags 

获取所有分支的tag

git describe --tags `git rev-list --tags --max-count=1`

Jenkinsfile中的配置

cat Jenkinsfile
pipeline{
....
      post {
            unsuccessful {
      		script {
        		def tags="""${sh(
                                         returnStdout: true,
                                         script: "git describe --abbrev=0 --tags"
                                       )}""".replace(' ','\n')
        		sh """
        			git checkout config/history-version.json
        			git tag -d ${tags}
                              """
      	}
      }
      }
}
posted @ 2020-11-17 16:46  大碗油泼  阅读(7851)  评论(0编辑  收藏  举报