如何获取Jenkins构建监视器插件以显示徽章?

问题描述 投票:1回答:1

我正在尝试将badges插件与jenkins build监控器插件集成在一起,根据最新发行说明,现在应该可以使用。 (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 jenkins-plugins jenkins-pipeline
1个回答
0
投票

我没有使用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-

© www.soinside.com 2019 - 2024. All rights reserved.