feature: 新增打tag

This commit is contained in:
leonmin 2025-02-06 19:22:26 +08:00
parent 83c9f7ce05
commit 4b0d8c7a3d
2 changed files with 86 additions and 0 deletions

83
vars/buildTag.groovy Normal file
View File

@ -0,0 +1,83 @@
def call (Map config = [:]) {
def branch = config.branch
def tag = config.tag
def description = config.description
def credentialsId = config.credentialsId
def master = "origin/master"
def targetBranch = "origin/${branch}"
def conflictCheck = sh(script: "git merge-base --is-ancestor ${master} ${targetBranch}", returnStatus: true)
def targetTag = "v${tag}"
def targetDesc = "${description}"
// master
if (conflictCheck != 0) {
def errorMessage = "分支: ${targetBranch}与${master}有冲突, 无法打tag"
currentBuild.description = getErrorBuildDescription(targetTag, errorMessage)
error(errorMessage)
}
// tag是否合法
def tagPattern = /^v\d+\.\d+\.\d+$/
def latestTag = ''
try {
latestTag = sh(script: "git describe --tags `git rev-list --tags --max-count=1`", returnStdout: true).trim()
} catch (Exception e) {
echo "没有找到任何 Git tags使用默认值"
latestTag = 'v0.0.0'
}
//
if (!targetTag.matches(tagPattern)) {
def errorMessage = "输入的tag格式不正确必须是1.0.0这种格式, 例如1.0.1、2.3.4"
currentBuild.description = getErrorBuildDescription(targetTag, errorMessage)
error(errorMessage)
}
//
if (!compareVersions(targetTag, latestTag)) {
def errorMessage = "目标tag: ${targetTag}小于或等于最新Tag: ${latestTag}, 无法打tag"
currentBuild.description = getErrorBuildDescription(targetTag, errorMessage)
error(errorMessage)
}
//
if (targetDesc.trim() == '') {
def errorMessage = "请输入版本改动内容"
currentBuild.description = getErrorBuildDescription(targetTag, errorMessage)
error(errorMessage)
}
withCredentials([gitUsernamePassword(credentialsId: credentialsId, gitToolName: 'git-tool')]) {
sh "git tag -a $targetTag -m '$targetDesc'"
sh "git push origin $targetTag"
sh "git commit --allow-empty -m '构建tag: $targetTag'"
sh "git push origin HEAD:$branch"
}
//
currentBuild.description = """
<p>tag(${targetTag})</p>
<p>: ${targetBranch}</p>
"""
}
def getErrorBuildDescription (String tag, String message) {
return """
<p style="color: red">tag(${tag})</p>
<p style="color: red">${message}</p>
"""
}
def compareVersions(String version1, String version2) {
// "v"
version1 = version1.replaceFirst("^v", "")
version2 = version2.replaceFirst("^v", "")
//
def ver1Parts = version1.tokenize('.')
def ver2Parts = version2.tokenize('.')
//
for (int i = 0; i < Math.max(ver1Parts.size(), ver2Parts.size()); i++) {
int part1 = i < ver1Parts.size() ? ver1Parts[i].toInteger() : 0
int part2 = i < ver2Parts.size() ? ver2Parts[i].toInteger() : 0
if (part1 != part2) {
return part1 > part2
}
}
return false // 12
}

3
vars/doTest.groovy Normal file
View File

@ -0,0 +1,3 @@
def call (Map config = [:]) {
sh "echo test msg: ${config.msg}"
}