groovy 我一直在我的Jenkins作业中遇到MissingPropertyException,尽管定义了变量

eaf3rand  于 7个月前  发布在  Jenkins
关注(0)|答案(1)|浏览(94)
ERROR: Build step failed with exception
groovy.lang.MissingPropertyException: No such property: manager for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:310)
    at Script1.isAnyJobRunningOrQueued(Script1.groovy:90)
    at Script1$isAnyJobRunningOrQueued.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169)
    at Script1.run(Script1.groovy:48)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:574)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:612)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:583)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:442)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:379)
    at hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95)
    at hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:816)
    at hudson.model.Build$BuildExecution.build(Build.java:199)
    at hudson.model.Build$BuildExecution.doRun(Build.java:164)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:524)
    at hudson.model.Run.execute(Run.java:1899)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:44)
    at hudson.model.ResourceController.execute(ResourceController.java:107)
    at hudson.model.Executor.run(Executor.java:449)

我的代码是在没有其他作业正在运行或排队时运行清理作业,除了清理作业-

import hudson.model.*

def runningBuilds = []
def queuedBuilds = []

def manager = Hudson.instance

def queue = manager.queue

if (queue.items.size() > 0) {
    queuedBuilds = queue.items.collect { it.task.url }
}

for (computer in manager.computers) {
    for (executor in computer.executors) {
        if (executor.isBusy()) {
            def currentBuildUrl = executor.currentExecutable.url
            if (!currentBuildUrl.matches('.*/clean_test/\\d+/'))
                runningBuilds << currentBuildUrl
                println "excluding current job from running builds: $currentBuildUrl"
            }
        }
    }
}

def maxRetries = 70
def retryIntervalSec = 10

def retries = 0
while (retries < maxRetries) {
    if (runningBuilds.isEmpty() && queuedBuilds.isEmpty()) {
        break
    }

    if (retries == maxRetries - 1) {
        println "Max retries reached. Exiting..."
        return
    }

    println "Waiting for running and queued builds to complete..."
    sleep(retryIntervalSec * 1000)
    retries++
}

def queueAfterWait = manager.queue

if (!isAnyJobRunningOrQueued(queueAfterWait)) {
    // Your cleanup logic here
    println "Running cleanup job on master node..."
    println "List current files in /tmp folder"
    println runCommand("ls -ltrh /tmp/")
    println "-------------------------------------------------------------------------------------------------------------"

    def diskSpaceBeforeCleanup = runCommand("ls -ltrh /tmp | head -n 1")
    println "TMP folder disk space before cleanup is $diskSpaceBeforeCleanup"

    // runCommand("rm -rf /tmp/<files>")
    println "-------------------------------------------------------------------------------------------------------------"

    def diskSpaceAfterCleanup = runCommand("ls -ltrh /tmp | head -n 1")
    println "TMP folder disk space after cleanup is $diskSpaceAfterCleanup"
    println "-------------------------------------------------------------------------------------------------------------"
} else {
    println "Skipping cleanup as other Jenkins jobs are running or queued."

    println "Running builds:"
    if (runningBuilds.isEmpty()) {
        println "No running builds."
    } else {
        println runningBuilds.join('\n')
    }

    println "Queued builds:"
    if (queuedBuilds.isEmpty()) {
        println "No queued builds."
    } else {
        println queuedBuilds.join('\n')
    }
}

def runCommand(String command) {
    def process = command.execute()
    process.waitFor()
    return process.text.trim()
}

def isAnyJobRunningOrQueued(queue) {
    // Check running builds on all computers
    for (computer in manager.computers) {
        for (executor in computer.executors) {
            if (executor.isBusy()) {
                return true
            }
        }
    }

    // Check queued builds
    if (queue.items.size() > 0) {
        return true
    }

    return false
}

尝试使用Jenkins.instance而不是哈德逊,并删除了管理器以及def jenkins = Jenkins.instance

def queue = jenkins.queue

and this-
def queue = Jenkins.instance.queue

但我还是看到了错误

groovy.lang.MissingPropertyException: No such property: Jenkins for class: Script1
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66)
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:310)
    at Script1.run(Script1.groovy:6)
x4shl7ld

x4shl7ld1#

这部分代码看起来很可疑:

def isAnyJobRunningOrQueued(queue) {
    // Check running builds on all computers
    for (computer in manager.computers) {
----------

您在方法中使用脚本变量manager,而没有将其作为参数传递。你可以试着把它改成:

def isAnyJobRunningOrQueued(queue, manager) {
    // Check running builds on all computers
    for (computer in manager.computers) {
----------

然后更改isAnyJobRunningOrQueued方法的调用:

if (!isAnyJobRunningOrQueued(queueAfterWait, manager)) {
-------

我希望这会有所帮助。
更新:另一种选择是将manager声明为全局变量manager = bla-bla而不是def manager = bla-bla。下面是一个小的测试脚本,让你了解Groovy中局部和全局变量是如何工作的:

def scriptVar = 'I am local'
globalVar = "I'm global!"

someMethod()

def someMethod() {
//    println "scriptVar = $scriptVar" -- it fails!!!
    println "globalVar = $globalVar"
}

相关问题