| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- #!/usr/bin/env groovy
- /**
- * 在 pipeline 的开始 & 结束各调用一次即可:
- * onBuildEvent('start')
- * onBuildEvent('end')
- */
- def call(String phase) {
- // 从 Jenkins 环境变量里取值
- String jobName = env.JOB_NAME
- String buildNum = env.BUILD_NUMBER
- String branch = env.GIT_BRANCH ?: 'unknown'
- String result = currentBuild.currentResult ?: 'UNKNOWN'
- String buildUrl = env.BUILD_URL
- String duration = phase == 'end' ? "${currentBuild.durationString}" : '-'
- // 颜色 & 图标随结果变化
- String headerColor = phase == 'start' ? 'blue' : (result == 'SUCCESS' ? 'green' : 'red')
- String headerIcon = phase == 'start' ? 'rocket' : (result == 'SUCCESS' ? 'check_circle' : 'error')
- def card = [
- header: [
- title: [
- tag: "plain_text",
- content: "Jenkins ${phase == 'start' ? '开始' : '结束'}通知"
- ],
- template: headerColor
- ],
- elements: [
- [
- tag: "div",
- text: [
- tag: "lark_md",
- content: "**项目:** ${jobName}\n" +
- "**分支:** ${branch}\n" +
- "**构建:** #${buildNum}\n" +
- "**结果:** ${result}\n" +
- "**耗时:** ${duration}\n" +
- "**链接:** [查看详情](${buildUrl})"
- ]
- ]
- ]
- ]
- // 从 Jenkins 凭据里取机器人配置,避免硬编码
- def robot = [
- webhook: credentials('feishu-webhook'),
- secret : credentials('feishu-secret')
- ]
- feishu.send(robot.webhook, robot.secret, card)
- }
|