使用有效的詹金斯脚本失败的管道“找不到这样的静态方法:staticMethod”

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

我正在尝试实现詹金斯脚本来监控奴隶代理 关注本文:“https://www.jenkins.io/doc/book/managing/nodes/” 我在詹金斯脚本控制台中成功运行了这个脚本,但当我在管道内运行它时,我一直失败

import hudson.model.*
import hudson.node_monitors.*
import hudson.slaves.*
import java.*
import javax.activation.*
import thread.*
import java.lang.*


def getEnviron(computer) {
   def env
   def thread = Thread.start("Getting env from ${computer.name}", { env = computer.environment })
   thread.join(2000)
   if (thread.isAlive()) thread.interrupt()
   env
}

def slaveAccessible(computer) {
    getEnviron(computer)?.get('PATH') != null
}


def numberOfflineNodes = 0
def numberNodes = 0

pipeline {
    parameters {
         string(name: 'Slack_channel',
           description: 'OTT DevOps channel',
           defaultValue: '#ott-devops-team')
    }
    environment {
   // Slack configuration
   SLACK_COLOR_DANGER  = '#E01563'
   SLACK_COLOR_INFO    = '#6ECADC'
   SLACK_COLOR_UNSTABLE    = '#E01563'
   SLACK_COLOR_WARNING = '#FFC300'
   SLACK_COLOR_GOOD    = '#3EB991'
   }

    agent {label "master"}
    stages {
        stage('monitor and restart slave agents') {
            steps {
                script {


                    def jenkins = Hudson.instance

                    for (slave in jenkins.slaves) {
                       def computer = slave.computer
                       numberNodes ++
                       println ""
                       println "Checking computer ${computer.name}:"
                       def isOK = (slaveAccessible(computer) && !computer.offline)
                       if (isOK) {
                         println "\t\tOK, got PATH back from slave ${computer.name}."
                         println('\tcomputer.isOffline: ' + slave.getComputer().isOffline());
                         println('\tcomputer.isTemporarilyOffline: ' + slave.getComputer().isTemporarilyOffline());
                         println('\tcomputer.getOfflineCause: ' + slave.getComputer().getOfflineCause());
                         println('\tcomputer.offline: ' + computer.offline);


                       } else {
                         numberOfflineNodes ++
                         println "  ERROR: can't get PATH from slave ${computer.name}."
                         println('\tcomputer.isOffline: ' + slave.getComputer().isOffline());
                         println('\tcomputer.isTemporarilyOffline: ' + slave.getComputer().isTemporarilyOffline());
                         println('\tcomputer.getOfflineCause: ' + slave.getComputer().getOfflineCause());
                         println('\tcomputer.offline: ' + computer.offline);
                         if (slave.getComputer().isTemporarilyOffline()) {
                          if (!slave.getComputer().getOfflineCause().toString().contains("Disconnected by")) {
                             computer.setTemporarilyOffline(false, slave.getComputer().getOfflineCause())
                          }
                         } else {
                             computer.connect(true)
                         }
                       }
                     }
                    println ("Number of Offline Nodes: " + numberOfflineNodes)
                    println ("Number of Nodes: " + numberNodes)
                }
            }
        }
    }
   post {
   aborted {
     echo "Sending message to Slack"
     slackSend (color: "${env.SLACK_COLOR_WARNING}",
                channel: "${params.Slack_channel}",
                message: "*ABORTED:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL}")
   } // aborted
   unstable {
     echo "Sending message to Slack"
     slackSend (color: "${env.SLACK_COLOR_UNSTABLE}",
                channel: "${params.Slack_channel}",
                message: "*UNSTABLE:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL} in ${params.ENV}")
   } // unstable
   failure {
     echo "Sending message to Slack"
     slackSend (color: "${env.SLACK_COLOR_DANGER}",
                channel: "${params.Slack_channel}",
                message: "*FAILED:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL} ")
   } // failure
   success {
     echo "Sending message to Slack"
     slackSend (color: "${env.SLACK_COLOR_GOOD}",
                channel: "${params.Slack_channel}",
                message: "*SUCCESS:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} \n More info at: ${env.BUILD_URL} ")
   } // success
  } // post
}

错误代码是:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such static method found: staticMethod java.lang.Thread start org.codehaus.groovy.runtime.GStringImpl org.jenkinsci.plugins.workflow.cps.CpsClosure2
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onStaticCall(SandboxInterceptor.java:182)
    at org.kohsuke.groovy.sandbox.impl.Checker$2.call(Checker.java:187)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedStaticCall(Checker.java:191)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:98)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.methodCall(SandboxInvoker.java:17)
    at WorkflowScript.getEnviron(WorkflowScript:12)
    at WorkflowScript.slaveAccessible(WorkflowScript:19)
    at WorkflowScript.run(WorkflowScript:56)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:84)
    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.GeneratedMethodAccessor307.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.ClosureBlock.eval(ClosureBlock.java:46)
    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:186)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:370)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:93)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:282)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:270)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:66)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    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)
Finished: FAILURE 

在 http://jenkins_home/script 中使用相同的脚本工作正常 /scriptApproval/

中没有等待批准的请求
jenkins jenkins-pipeline jenkins-groovy
1个回答
0
投票

异常来自脚本安全插件。

您将在管理 Jenkins -> 进程内脚本批准(或浏览至

<your-jenkins-host>/scriptApproval
)下看到您需要批准才能运行脚本的行。

如果有多条违规线路,您将需要重复此过程,直到满足所有安全要求。

另一种方法是完全禁用管道中的常规沙箱(配置管道时位于脚本框下方),但除非您正在测试,否则最好将其选中。

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