Does job - steps - groovyCommand or systemGroovyCommand support a multi-line command string? I can't get the following job to run without error in the groovyCommand, using either groovyCommand or systemGroovyCommand:
job('WorkspaceCleanup') {
description('Jenkins Job from DSL to Cleanup Old Workspaces')
label('controller')
triggers {
cron('@hourly')
}
steps {
systemGroovyCommand("""\
println "Running " + this.class.getSimpleName() + "..."
def deleted = []
def oneDayAgo = new Date() - 1
jenkins.model.Jenkins.instance.nodes.each { hudson.model.Node node ->
node.workspaceRoot.listDirectories().each { hudson.FilePath path ->
def pathName = path.getRemote()
if (path.name.startsWith(".")) {
println "Skipping internal dir $node.displayName:$pathName"
}
else {
def lastModified = new Date(path.lastModified())
if (lastModified < oneDayAgo) {
println "Deleting workspace at $node.displayName:$pathName (last modified $lastModified)"
path.deleteRecursive()
deleted << "$node.displayName:$pathName (last modified $lastModified)"
} else {
println "Skipping workspace at $node.displayName:$pathName (last modified $lastModified)"
}
}
}
println "Summary of Deleted workspaces: \n\t" + deleted.sort().join("\n\t")
}
""") {
}
}
}
Loading the above jobDSL (using CasC) gives me the following exception:
groovy.lang.MissingPropertyException: No such property: pathName for class: javaposse.jobdsl.dsl.helpers.step.StepContext
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
at script$_run_closure1$_closure3.doCall(script:26)
at script$_run_closure1$_closure3.doCall(script)
.
.
.
But it works fine when I place the above string in a file and use groovyScriptFile or systemGroovyScriptFile:
job('WorkspaceCleanup') {
description('Jenkins Job from DSL to Cleanup Old Workspaces')
label('controller')
triggers {
cron('@hourly')
}
steps {
systemGroovyScriptFile("/home/jenkins/.jenkins/cleanup-workspaces.groovy") {
}
}
}
cleanup-workspace.groovy:
println "Running " + this.class.getSimpleName() + "..."
def deleted = []
def oneDayAgo = new Date() - 1
jenkins.model.Jenkins.instance.nodes.each { hudson.model.Node node ->
node.workspaceRoot.listDirectories().each { hudson.FilePath path ->
def pathName = path.getRemote()
if (path.name.startsWith(".")) {
println "Skipping internal dir $node.displayName:$pathName"
}
else {
def lastModified = new Date(path.lastModified())
if (lastModified < oneDayAgo) {
println "Deleting workspace at $node.displayName:$pathName (last modified $lastModified)"
path.deleteRecursive()
deleted << "$node.displayName:$pathName (last modified $lastModified)"
} else {
println "Skipping workspace at $node.displayName:$pathName (last modified $lastModified)"
}
}
}
println "Summary of Deleted workspaces: \n\t" + deleted.sort().join("\n\t")
I'm using Jenkins v2.222.3, job-dsl-plugin v1.77, and groovy plugin v2.3. I have approved all the methods used by this script in my instance.