Files
jenkins-shared-library/vars/notifyLark.groovy

105 lines
2.8 KiB
Groovy
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
def buildCommitDiffMarkdown() {
def changeSets = currentBuild.changeSets
if (!changeSets || changeSets.isEmpty()) {
return '_本次构建无代码变更_'
}
def commits = []
changeSets.each { cs ->
cs.items.each { item ->
def author = item.author?.fullName ?: 'unknown'
def msg = item.msg?.trim()?.replaceAll('[\\r\\n]+', ' ')
commits << "- **${msg}**${author}"
}
}
if (commits.isEmpty()) {
return '_本次构建无代码变更_'
}
// 最大提交消息条数
def maxCommits = 15
if (commits.size() > maxCommits) {
commits = commits.take(maxCommits)
commits << "\n_……以及更多提交_"
}
return commits.join('\n')
}
def call(Map args = [:]) {
// ===== 参数 =====
def publishEnvs = args.publishEnvs ?: 'production'
def branchName = args.branchName ?: (env.BRANCH_NAME ?: 'unknown')
def webhookUrl = args.webhookUrl ?: env.XSH_FEISHU_WEBHOOK
// ===== Jenkins 内建上下文 =====
def buildStatus = currentBuild.currentResult ?: 'UNKNOWN'
def buildNumber = env.BUILD_NUMBER
def buildUrl = env.BUILD_URL
def jobName = env.JOB_NAME
echo "${env.EXECUTOR_NAME}"
echo "${env.EXECUTOR_OPENID}"
echo "${env.BUILD_USER}"
def triggerBy
if (env.EXECUTOR_OPENID) {
triggerBy = "<at user_id=\"${env.EXECUTOR_OPENID}\"></at>"
} else if (env.BUILD_USER) {
triggerBy = env.BUILD_USER
} else {
triggerBy = 'unknown'
}
// ===== 状态标签 =====
def statusTag = [
SUCCESS : "<text_tag color='turquoise'>通过</text_tag>",
FAILURE : "<text_tag color='red'>失败</text_tag>",
UNSTABLE: "<text_tag color='orange'>不稳定</text_tag>",
ABORTED : "<text_tag color='neutral'>已中止</text_tag>"
][buildStatus] ?: "<text_tag color='neutral'>未知状态</text_tag>"
// def commitDiff = "[查看变更记录](${buildUrl}changes)"
def commitDiff = buildCommitDiffMarkdown()
// ===== 凭证 =====
withCredentials([
string(credentialsId: 'FEISHU_WEBHOOK', variable: 'FEISHU_WEBHOOK')
]) {
def payload = [
msg_type: 'interactive',
card: [
type: 'template',
data: [
template_id: 'AAq2iv39m3lv5',
template_version_name: '1.0.6',
template_variable: [
build_title : jobName,
status_tag : statusTag,
build_number: buildNumber,
publish_envs: publishEnvs,
commit_diff : commitDiff,
build_branch: branchName,
trigger_by : triggerBy,
build_url : [ url: buildUrl ]
]
]
]
]
httpRequest(
httpMode: 'POST',
contentType: 'APPLICATION_JSON',
url: webhookUrl,
requestBody: groovy.json.JsonOutput.toJson(payload),
validResponseCodes: '200:299'
)
}
}