背景
公司前端項目在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}
"""
}
}
}
}