fix Jenkinsfile escape-free

This commit is contained in:
xiangning 2026-07-03 17:30:51 +08:00
parent eb77e0a3f7
commit c25b91325e

19
Jenkinsfile vendored
View File

@ -1,25 +1,24 @@
// BuildFlow buildflow-web CI/CD
// ①拉取代码 -> ②编译打包 -> 推送Harbor -> ③部署
// 飞书卡片通知(成功绿/失败红), 内联闭包用 JsonOutput 无插件依赖
// 飞书卡片通知(成功绿/失败红)
def feishuNotify = { String result, String failedStageName = "" ->
def dur = currentBuild.durationString.replace(" and counting", "")
def commit = ""
try { commit = sh(script: "git log -1 --pretty=format:%s__%an 2>/dev/null | head -1", returnStdout: true).trim() } catch(ignored) {}
try { commit = sh(script: "git log -1 --pretty=format:%s", returnStdout: true).trim() } catch(ignored) {}
def isOk = (result == "SUCCESS")
def emoji = isOk ? "✅" : "❌"
def color = isOk ? "green" : "red"
def title = emoji + " CI/CD " + (isOk ? "成功" : "失败") + " · " + env.JOB_NAME + " #" + env.BUILD_NUMBER
def stMsg = isOk ? "成功" : "失败"
def statusText = isOk ? "成功" : "失败"
def title = "CI/CD " + statusText + " " + env.JOB_NAME + " #" + env.BUILD_NUMBER
def fields = [
[is_short: true, text: [tag: "lark_md", content: "**项目**\n" + env.JOB_NAME]],
[is_short: true, text: [tag: "lark_md", content: "**耗时**\n" + dur]],
[is_short: true, text: [tag: "lark_md", content: "**提交**\n" + commit.take(50)]],
[is_short: true, text: [tag: "lark_md", content: "**状态**\n" + stMsg]]
[is_short: true, text: [tag: "lark_md", content: "**状态**\n" + statusText]]
]
def elements = [[tag: "div", fields: fields], [tag: "hr"]]
if (isOk) {
elements.add([tag: "div", text: [tag: "lark_md", content: "镜像地址\n" + env.IMAGE + ":" + env.TAG]])
elements.add([tag: "div", text: [tag: "lark_md", content: "镜像地址\n`" + env.IMAGE + ":" + env.TAG + "`"]])
} else if (failedStageName) {
elements.add([tag: "div", text: [tag: "lark_md", content: "失败阶段\n" + failedStageName]])
}
@ -29,7 +28,7 @@ def feishuNotify = { String result, String failedStageName = "" ->
def card = [msg_type: "interactive", card: [header: [title: [tag: "plain_text", content: title], template: color], elements: elements]]
def jsonStr = groovy.json.JsonOutput.toJson(card)
writeFile file: "/tmp/feishu_msg.json", text: jsonStr
sh "curl -sS -X POST https://open.feishu.cn/open-apis/bot/v2/hook/d215e8ed-edea-4f07-a77c-2a0e5b52d9b9 -H \x27Content-Type: application/json\x27 -d @/tmp/feishu_msg.json || true"
sh "curl -sS -X POST https://open.feishu.cn/open-apis/bot/v2/hook/d215e8ed-edea-4f07-a77c-2a0e5b52d9b9 -H 'Content-Type: application/json' -d @/tmp/feishu_msg.json || true"
sh "rm -f /tmp/feishu_msg.json"
}
@ -82,7 +81,7 @@ pipeline {
}
echo "部署完成"
} catch (e) {
echo "部署节点(deploy-agent)暂不可用,跳过自动部署。镜像已推送 Harbor。"
echo "部署节点暂不可用,跳过自动部署。镜像已推送 Harbor。"
}
}
}
@ -93,7 +92,7 @@ pipeline {
script { feishuNotify("SUCCESS") }
}
failure {
script { feishuNotify("FAILURE", "构建过程中失败,请查看日志") }
script { feishuNotify("FAILURE", "构建失败,请查看日志") }
}
always {
sh "docker rmi ${IMAGE}:${TAG} ${IMAGE}:latest 2>/dev/null || true"