【问题标题】:How to get Jenkins build monitor plugin to display badges?如何让 Jenkins 构建监视器插件来显示徽章?
【发布时间】:2018-05-30 07:29:16
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-plugins badge


    【解决方案1】:

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

    但是,如果你使用方法

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

    将显示文本。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-11-12
        • 1970-01-01
        • 1970-01-01
        • 2014-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多