如何让 Jenkins build monitor plugin 显示徽章?

How to get Jenkins build monitor plugin to display badges?

我正在尝试将徽章插件与 jenkins build monitor 插件集成,根据最新的发行说明,现在应该可以了。 (https://github.com/jan-molak/jenkins-build-monitor-plugin/releases/tag/v1.11%2Bbuild.201701152243)

我已经下载并安装了所有必需的插件和依赖项。 并在 Post always 块中将以下代码添加到我的 Jenkins 管道中。

addBadge(icon:"text.gif", text:"cucumber-report", id:"cukerep", link:"http://www.google.com")

但是在构建无误地完成后,我的构建监视器插件视图中没有显示徽章。

我还在构建监视器视图中勾选了显示徽章框。

有人知道我错过了什么吗?

我没有使用 jenkins 管道,但我正在使用带有 groovy postbuild 插件徽章的构建监视器插件。在插件中勾选显示徽章框后,我可以在构建监视器显示中看到徽章。

作为一个完整的猜测 manager.addBadge 可以在管道代码中工作吗?

我建议安装 Groovy PostBuild 插件并尝试类似的操作。 您可以在管道代码中使用它而不是 addBadge。 或者它可能会提供有助于使管道 addBadge 正常工作的线索。

Groovy 添加徽章的 PostBuild 代码:

manager.addShortText("VERSION Black on Lime Green", "black", "limegreen", "0px", "white")
manager.addShortText("OBSOLETE YellowGrey5pxGrey", "yellow", "grey", "5px", "grey")

manager.addBadge("warning.gif", "Warning test")
manager.addWarningBadge("other warning test")

例如Groovy 用于将徽章添加到另一个工作的代码: 当作业的版本与最新版本不匹配时,将作业标记为过时。 我给出了这个示例代码,因为这是我目前使用的并且它有效。

import hudson.model.*
import groovy.json.JsonSlurper
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildAction
//import org.jvnet.hudson.plugins.groovypostbuild.*
import org.jenkinsci.plugins.buildtriggerbadge.BuildTriggerBadgeAction;

def jobList = [ "job1", "job2" ]

def latestFile = downloadDir + "/latest_version.txt"
try {
  println "read file:" + latestFile
  vLATEST = new File(latestFile).text.trim().replaceAll('-','.')
  println vLATEST
} catch (MissingPropertyException|FileNotFoundException e) { 
  vLATEST = "LATEST version file not found"
  vLATEST = "unknown"
}

for(item in Hudson.instance.items) {

  v = "unknown"

  println "ITEM NAME: " + item.getDisplayName()
  println "ITEM DESC: " + item.getDescription()
  if (item.lastBuild) {
    try {
      println "item.WORKSPACE:" + item.WORKSPACE
      println "item.lastBuild.workspace:" + item.lastBuild.workspace;
      vfilename = item.lastBuild.workspace.toString()+"/VERSION.txt"
      v = new File(vfilename).text.trim()
      println v
    } catch (MissingPropertyException|FileNotFoundException e) { 
      // workspace or file not found
      v = "version file not found"
    }
    //currentBuild.setDescription(desc)
  }
  //println item.getDescriptorByName()

  if (item.name in jobList && item.lastBuild) {
    // delete old/other badges
    for(action in item.lastBuild.badgeActions) {
      //println "action:" + action
      //https://javadoc.jenkins.io/hudson/model/Action.html
      if(action instanceof GroovyPostbuildAction) {  // this is safer anyway
        println "action is instanceof GroovyPostbuildAction, remove action:" + action +
            " ts:" + action.toString() +
            " dn:" + action.getDisplayName()
        item.lastBuild.actions.remove(action)
        item.lastBuild.save()
      }
    }

    //item.lastBuild.setDescription(item.lastBuild.description + "\n" + "test set description VERSION:" + v)

    println "check version. vLATEST:" + vLATEST + ", v:" + v
    if (!(vLATEST == "unknown") && !(v.contains(vLATEST))) {
      println "version is not unknown OR does not match so BADGE IT mark build as OLD. vLATEST:" + vLATEST + ", v:" + v
      // badge it badge it badge it badge it badge it badge it badge it badge it badge it badge it badge it badge it
      text = "OLD VERSION: " + v
      item.lastBuild.getActions().add(GroovyPostbuildAction.createShortText(text, "yellow", "grey", "1px", "darkgrey"));
    } else {
      println "vLATEST:" + vLATEST + " is in v:" + v + " so not marking the build as old."

    }
    // https://javadoc.jenkins.io/plugin/groovy-postbuild/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder.BadgeManager.html#addShortText-java.lang.String-      
  }

}

参考:

https://javadoc.jenkins.io/hudson/model/Action.html

https://javadoc.jenkins.io/plugin/groovy-postbuild/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder.BadgeManager.html#addShortText-java.lang.String-

我认为这是 jenkins 监视器视图中的错误,没有显示徽章。

但是,如果你使用方法

addShortText (text: "ExampleText", background: "red", border: "1", borderColor: "black", color: "black")

将显示文本。