46-Jenkins-Groovy Postbuild插件使用

Groovy Postbuild插件使用

      • 前言
      • 安装插件
      • 使用插件
        • 添加额外信息
        • 添加图标链接
        • 提取日志信息
        • 添加链接文本
          • 添加链接
          • 添加文本
        • 设置构建结果


前言

  • 本篇来学习下Groovy Postbuild插件的使用

安装插件

  • Manage Jenkins --> Mangage Plugins --> 可选插件 --> 输出框输入 groovy postbuild

46-Jenkins-Groovy Postbuild插件使用_第1张图片

使用插件

添加额外信息

pipeline {
    agent any

    stages {
        stage('add Summary') {
            steps {
               script{
                manager.createSummary("warning.gif").appendText("

You have been warned!

"
, false, false, false, "red") } } } } }
  • 报错及解决

    • 报错
    org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder$BadgeManager createSummary java.lang.String
    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectMethod(StaticWhitelist.java:265)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:162)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:165)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.run(WorkflowScript:11)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:83)
    at sun.reflect.GeneratedMethodAccessor568.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:403)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:97)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:315)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:279)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
    
    • 解决:取消使用Groovy沙盒的勾选状态
      46-Jenkins-Groovy Postbuild插件使用_第2张图片
  • 效果(构建状态页)
    46-Jenkins-Groovy Postbuild插件使用_第3张图片

添加图标链接

pipeline {
    agent any

    stages {
        stage('add Badge') {
            steps {
               script{
               	   // 两种写法实现
                   addBadge icon: 'info.gif', id: '', link: 'https://blog.csdn.net/IT_heima', text: '我的博客'
                   
                   manager.addBadge("success.gif","test link","https://blog.csdn.net/IT_heima")
               }
            }
        }
       
    }
}
  • 支持如下图标

    • completed.gif
    • db_in.gif
    • db_out.gif
    • delete.gif
    • error.gif
    • folder.gif
    • green.gif
    • info.gif
    • red.gif
    • save.gif
    • success.gif
    • text.gif
    • warning.gif
    • yellow.gif
  • 效果
    46-Jenkins-Groovy Postbuild插件使用_第4张图片

提取日志信息

pipeline {
    agent any

    stages {
        stage('println') {
            steps {
                
                script{
                
                    myBlog = 'https://blog.csdn.net/IT_heima'
            
                    println myBlog
                }
                    
            }
        }
        
        stage('getLogMatcher') {
            steps {
               script{
               def res = manager.getLogMatcher("https://.*")
               
               println "res:${res}"
               
                if(res != null && res.matches()) {
                    
                  url = res.group(0)
                  println url
                  currentBuild.setDescription(url)
                  
                  manager.addBadge("info.gif","myBlog",url)
                }
               }
            }
        }
       
    }
}


  • 效果:日志中提取信息添加到构建历史
    46-Jenkins-Groovy Postbuild插件使用_第5张图片

添加链接文本

添加链接
pipeline {
    agent any

    stages {
        stage('addShortText 1') {
            steps {
              script{
                 addShortText(
                text: "我的博客",   
                border: "0",     
                background: "transparent", 
                color: "blue", 
                link: "https://blog.csdn.net/IT_heima"
            )
              }
            }
        }         
    }
}

  • 效果

46-Jenkins-Groovy Postbuild插件使用_第6张图片·

添加文本
  • addShortText(text, color, background, border, borderColor)
pipeline {
    agent any

    stages {
       
        
        stage('addShortText 2') {
            steps {
               script{
               manager.addShortText("测试","blue", "white", "1px", "blue") 
               }
            }
        }
       
    }
}

  • 效果
    46-Jenkins-Groovy Postbuild插件使用_第7张图片

设置构建结果

  1. buildUnstable()
  2. buildFailure()
  3. buildSuccess()
  4. buildAborted()
  5. buildNotBuilt()
pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
    post{
        always{
            script{
             manager.buildUnstable()
        } 
        }
       
    }
}
  • 效果
    46-Jenkins-Groovy Postbuild插件使用_第8张图片

你可能感兴趣的:(#,Jenkins,jenkins,java,运维)