You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jenkins构建状态页添加Checkmarx扫描结果链接

解决Jenkins构建状态页添加Checkmarx扫描链接的问题

问题描述

Jenkins构建完成后,状态页会展示启动用户、构建耗时、Git提交等详情。我希望在构建结束后,将Checkmarx扫描结果的网页链接添加至该页面,可在Checkmarx扫描阶段结束后或通过构建后操作实现。因组织限制,无法安装自定义插件。尝试了以下Groovy构建后脚本但未生效:

// Groovy Postbuild Script
import hudson.model.*

def build = manager.build
def customMessage = "Custom message: " // my checkmarx link here

def changeSet = build.changeSet
def changes = changeSet.getItems().collect {
    "${it.author.fullName}: ${it.msg}"
}.join('
')

def newContent = """
    <div>
        <h3>Custom Information:</h3>
        <p>${customMessage}</p>
        <h3>Changes:</h3>
        <pre>${changes}</pre>
    </div>
"""

def action = new ParametersAction([
    new StringParameterValue('CustomInformation', newContent)
])

build.addAction(action)

原脚本失效原因

你使用的ParametersAction是用来添加构建参数的,这类参数仅会出现在构建参数列表中,不会直接展示在构建状态页的详情区域,所以页面上看不到预期内容。

正确的Groovy后脚本实现

要直接在构建状态页添加自定义内容,需使用Groovy Postbuild插件提供的manager对象相关方法,以下是两种可行方案:

方案1:直接追加HTML到构建描述

该方式会将内容直接整合到构建状态页的描述区域,直观展示:

import hudson.model.*

def build = manager.build
// 替换为你的Checkmarx扫描结果实际链接
def checkmarxLink = "https://your-checkmarx-scan-result-url"
def customMessage = "Checkmarx扫描结果: <a href='${checkmarxLink}' target='_blank'>点击查看</a>"

// 收集Git提交变更信息
def changeSet = build.changeSet
def changes = changeSet.getItems().collect {
    "${it.author.fullName}: ${it.msg}"
}.join('<br>')

// 组装要添加的HTML内容
def newContent = """
    <hr>
    <h3>自定义信息</h3>
    <p>${customMessage}</p>
    <h3>本次提交变更</h3>
    <p>${changes}</p>
"""

// 获取现有描述并追加新内容,避免覆盖原有信息
def currentDescription = build.getDescription() ?: ""
build.setDescription(currentDescription + newContent)

方案2:添加带图标的摘要区块

如果想在构建状态页顶部添加带图标的快捷入口,可使用createSummary方法:

import hudson.model.*

def build = manager.build
// 替换为你的Checkmarx扫描结果实际链接
def checkmarxLink = "https://your-checkmarx-scan-result-url"

// 创建带内置图标的摘要区块(图标可换为Jenkins内置的其他图标,比如"warning.png")
def summary = manager.createSummary("clipboard.png")
summary.appendText("<h3>Checkmarx扫描结果</h3>", false, false, false, "blue")
summary.appendText("<a href='${checkmarxLink}' target='_blank'>点击查看完整扫描报告</a>", false, false, false, "black")

// 追加Git提交变更信息
def changeSet = build.changeSet
def changes = changeSet.getItems().collect {
    "${it.author.fullName}: ${it.msg}"
}.join('<br>')
summary.appendText("<h4>本次提交变更</h4><p>${changes}</p>", false, false, false, "black")

注意事项

  • 确保已安装Groovy Postbuild插件(这是Jenkins官方维护的插件,不属于自定义插件,一般组织不会限制)
  • 务必将脚本中的https://your-checkmarx-scan-result-url替换为实际的Checkmarx扫描结果链接

内容的提问来源于stack exchange,提问作者lr-pal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 00:52:42