错误:构建步骤失败并出现异常 groovy.lang.MissingPropertyException:没有这样的属性:类的管理器:Script1 在 org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66) 在 org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:310) 在 Script1.isAnyJobRunningOrQueued(Script1.groovy:90) 在 Script1$isAnyJobRunningOrQueued.callCurrent(来源未知) 在 org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:157) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:169) 在 Script1.run(Script1.groovy:48) 在 groovy.lang.GroovyShell.evaluate(GroovyShell.java:574) 在 groovy.lang.GroovyShell.evaluate(GroovyShell.java:612) 在 groovy.lang.GroovyShell.evaluate(GroovyShell.java:583) 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:442) 在 org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:379) 在 hudson.plugins.groovy.SystemGroovy.run(SystemGroovy.java:95) 在 hudson.plugins.groovy.SystemGroovy.perform(SystemGroovy.java:59) 在 hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20) 在 hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:816) 在 hudson.model.Build$BuildExecution.build(Build.java:199) 在 hudson.model.Build$BuildExecution.doRun(Build.java:164) 在 hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:524) 在 hudson.model.Run.execute(Run.java:1899) 在 hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:44) 在 hudson.model.ResourceController.execute(ResourceController.java:107) 在 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 而不是 hudson 并删除了管理器 def jenkins = Jenkins.instance
def 队列=jenkins.queue
还有这个- def 队列 = Jenkins.instance.queue
但是我仍然看到错误 groovy.lang.MissingPropertyException:没有这样的属性:Jenkins 类:Script1 在 org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:66) 在 org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51) 在 org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:310) 在 Script1.run(Script1.groovy:6)
对我来说这部分代码看起来很可疑:
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)) {
-------
希望对你有帮助。