job - steps - *groovyCommand not working with multiline comomand string

22 views
Skip to first unread message

Tim Black

unread,
Nov 19, 2020, 1:11:20 PM11/19/20
to job-dsl-plugin
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.

Matt Sheehan

unread,
Nov 19, 2020, 1:19:48 PM11/19/20
to job-dsl-plugin
Have you tried triple single quotes instead of triple double quotes?

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/53002aeb-407d-4ea8-8156-c5083d447fe0n%40googlegroups.com.

Tim Black

unread,
Nov 19, 2020, 2:02:59 PM11/19/20
to job-dsl-plugin
Thanks. Yes, I had tried earlier with single quotes and had gotten a different syntax error. Switching back to triple single quotes, I get further, hit script approval restriction, but then after approving it, I get this:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Script1.groovy: 21: expecting anything but ''\n''; got it anyway @ line 21, column 45. 
 ummary of Deleted workspaces: ^

This is what was making me think it didn't support multiline commands. But I see now that it looks like this is complaining about the explicit newline inside the string. 

If I replace that last line with this:

    println "Summary of Deleted workspaces:"
    println deleted.sort().join("\n\t")

it gives a different error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
Script1.groovy: 22: expecting anything but ''\n''; got it anyway @ line 22, column 34. 
 println deleted.sort().join(" 
                              ^ 

So how do I escape newlines in this context?

BTW, going back to using systemGroovyScript, I now have to approve the full script string each time I run. How to I set up approval for this script command? I'm using CasC's scriptApproval:approvedSignatures for this.

Tim Black

unread,
Nov 19, 2020, 2:44:52 PM11/19/20
to job-dsl-plugin
OK, got it working. I needed to escape the \n and \t inside the strings, and also use `sandbox(true)` in the systemGroovyCommand param. So the end of my command looks like this:

    .
    .
    println "Summary of Deleted workspaces:\\n\\t" + deleted.sort().join("\\n\\t")
}
''') {
            sandbox(true)
    }

Reply all
Reply to author
Forward
0 new messages