Unable to loop the files from Jenkins workspace using groovy - jenkins

I just written the below groovy script to read list of files in the Jenkins workspace an check if there is any file name starts with report. If it's I need to print those values too.
build.getWorkspace().list().each
{
println it.getName()
if(it.name.startsWith('report'))
{
println it
}
}
Exception:
groovy.lang.MissingPropertyException: No such property: it for class:
Script1 at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at
org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at org.kohsuke.groovy.sandbox.impl.Checker$4.call(Checker.java:243)
at
org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:238)
at
org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:221)
at
org.kohsuke.groovy.sandbox.impl.Checker$checkedGetProperty$0.callStatic(Unknown
Source) at
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:230)
at Script1$_run_closure1.doCall(Script1.groovy:12)
How can I find out the list of files in workspace and filter them by using name using groovy?

Related

how to iterrate the tool variable in jenkins groovy

In an example pilepline I saw a variable named tool and is used like this:
echo "${tool 'vs2017'}"
// Outputs C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin
My question is, what kind of variable is this? and how can I see what includes in this variable. I tried to print it like this but it fails:
tool.each { k, v -> echo 'k'}
groovy.lang.MissingPropertyException: No such property: tool for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:271)
at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)

Running groovy script at the end of Jenkins Job

I need to run a groovy Post-Build script to do some clean up so I have arranged the following script:
def sout = new StringBuilder(), serr = new StringBuilder()
def proc = '/usr/bin/docker stop mysql'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
Although the command '/usr/bin/docker stop mysql' works if I log on the machine, when the groovy script is executed, the following error is raised:
java.io.IOException: Cannot run program "/usr/bin/docker": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at org.codehaus.groovy.runtime.ProcessGroovyMethods.execute(ProcessGroovyMethods.java:533)
at org.codehaus.groovy.runtime.dgm$894.invoke(Unknown Source)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:157)
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:104)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedCall(Checker.java:159)
at org.kohsuke.groovy.sandbox.impl.Checker$checkedCall$0.callStatic(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
at Script1.run(Script1.groovy:2)
Do you know why the groovy engine fails to find the 'docker' command?
Thanks!
The Groovy Postbuild plugin actually runs on the master, not on the build node. The documentation states:
This plugin executes a groovy script in the Jenkins JVM
which is presumably meant to convey "and not in an agent JVM". (Fun side note: If you use println in the Groovy script, it prints out to the Jenkins master log, not to the job's console log)
The Post build task plugin does run on the build node (and you don't need to wrap your shell command in a groovy script). The task requires a condition: to "always run" your script just leave the Log text blank.
You might also consider taking a look at the Jenkins Pipeline, which lets you bake in try/catch type logic and maintain your pipeline "as code" using a Jenkinsfile.

Execute SQL*Plus script via Jenkins and bat command

In my Jenkins Pipeline script, I have a map named orderedScripts which contains Integer keys (1-11) and the values for each key are lists. The items within the lists are absolute paths to sql scripts.
I loop through the map and then loop through the list of each key, executing the items in the list via sqlplus. Code snippet below, where <credentials> are the DB credentials used.
orderedScripts.each {
key, value -> for(item in value){
bat "sqlplus <credentials> #'${item}'"
}
}
However, when Jenkins runs the job I get the following Serilizable error:
Caused: java.io.NotSerializableException: hudson.scm.SubversionChangeLogSet
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:569)
at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at java.util.HashMap.internalWriteEntries(Unknown Source)
at java.util.HashMap.writeObject(Unknown Source)
at sun.reflect.GeneratedMethodAccessor95.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
If I print out the ${item} variable with a normal println() then it shows the correct script path. So the loop is working, and identifying the correct scripts to run, Jenkins just won't run them via the bat command. If I print out the path in the script and then copy and paste it into a sqlplus session manually, it executes as expected.
From what I can find, it seems to be how I'm referencing the ${item} variable within the bat command. But I can't get it to execute, with different variations of quotations. Anyone experience this before, or know what I'm doing wrong?
Answering my own question here..
The issue was related to Serialization Problems In Global Function
Which points to the following Jenkinsfile example on Github:
Jenkins File example

How do I solve "Expected named arguments" for freeStyleJob?

I have several different projects that will be compiled in Jenkins and shall be uploaded to my Nexus3 repository. For that I am using the NexusArtifcalUploader. For some reason I get the following error message although the code is essentially copied from the plugin page of the Jenkins wiki.
java.lang.IllegalArgumentException: Expected named arguments but got [clientmoduleNexusArtifactUploaderJob, org.jenkinsci.plugins.workflow.cps.CpsClosure2#63d801fc]
at org.jenkinsci.plugins.workflow.cps.DSL.parseArgs(DSL.java:511)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable(DSL.java:291)
at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:153)
at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
at sun.reflect.GeneratedMethodAccessor463.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
...
My Jenkinsfile calls the uploadToNexus method I created which creates freeStyleJobs:
def uploadToNexus(module) {
def groupId = "com.example"
def moduleVersions = [
"client-module": "1.0.0-SNAPSHOT",
"server-module": "1.0.0-SNAPSHOT",
]
def moduleVersion = moduleVersions.get(module)
def jobName = "${fixModuleName(module)}NexusArtifactUploaderJob"
echo "will run freeStyleJob ${jobName} now..."
freeStyleJob(jobName) {
steps {
nexusArtifactUploader {
nexusVersion('nexus3')
protocol('http')
nexusUrl('nexus:8081')
groupId(groupId)
version(moduleVersion)
repository('maven2_central')
credentialsId('nexus_admin')
artifact {
artifactId('${module}')
type('war')
classifier('debug')
file('${module}.war')
}
}
}
}
}
To my knowledge freeStyleJob expects a string which I pass, don't I? What am I missing and doing wrong?
It seems that I mixed up Job DSL and Pipeline DSL. I wasn't aware there's a difference.
Here's a way to use Job DSL inside Pipeline DSL:
https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#use-job-dsl-in-pipeline-scripts

Assigning a step to a variable in Jenkins Pipeline

I have the following pipeline script:
node {
def myStep = sh
myStep "ls -la"
}
I thought steps were visible as variables and could be assigned to variables so that they can be used later (for example choosing a different step depending on some conditions).
However, this fails with:
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: myStep for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:232)
at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:282)
at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:286)
at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:28)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:3)
at ___cps.transform___(Native Method)
How can I put a step in a variable to use it later without hardcoding its name?
You can write a method in your pipeline that wraps the behavior you want. It will have access to the script variables.
node {
myStep("ls -la")
}
def myStep(String script) {
sh(script)
}
My current workaround:
node {
def myStep = { script ->
sh script
}
myStep("ls -la")
}

Resources