如何让Jenkins构建监视器插件来显示徽章?

时间:2018-05-30 07:29:16

标签: jenkins jenkins-plugins jenkins-pipeline

我正在尝试将徽章插件与jenkins构建监视器插件集成,根据最新发行说明现在应该可以。 (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")

但是在构建完成且没有错误之后,我的构建监视器插件视图中没有出现任何标记。

我还在构建监视器视图中勾选了show badge。

有谁知道我错过了什么?

2 个答案:

答案 0 :(得分:1)

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

完全可以猜到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-

答案 1 :(得分:1)

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

但是,如果您使用方法

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

将显示文本。

enter image description here