Jenkins - load a groovy file into a DSL file - jenkins

I want to load a groovy file in a DSL file. If I process the DSL file I got following error message:
Processing DSL script folderA/job.dsl
ERROR: (job.dsl, line 2) No signature of method: job.load() is applicable for argument types: (org.codehaus.groovy.runtime.GStringImpl) values: [./build.groovy ]
Possible solutions: find(), job(java.lang.String), find(groovy.lang.Closure), wait(), run(), run()
The first two lines of my file job.dsl:
def workspace = '.'
def module = load "${workspace}/build.groovy "
I understand the error message in this way, there is no method load() in object job. The question is, how can I access global/build-in methods like load() in a DSL file?

According to the message there is no load() method with a signature that contains a groovy.lang.GString parameter.
You could use:
a Double-quoted string: workspace + "/build.groovy" or
a Single-quoted string: workspace + '/build.groovy'
without interpolation which are interpreted as java.lang.String.
See also Load script from groovy script.

Related

Updated Jenkins and ReadToString() no longer works

we recently updated Jenkins and when running one of our jobs to upload a wildcard cert to aks it no longer excepts ReadToString(). It offers a possible solution but simply changing it doesn't resolve the issue.
Version: Jenkins 2.361.1
Pipeline Error:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.lang.String.readToString() is applicable for argument types: () values: []
Possible solutions: toString(), toString(), toString()
Part of the the config:
def fb64crt = input message: 'Upload Cert (*.crt) File. There is no validation on this input so be sure what you select!',
parameters: [base64File('crtfile')]
writeFile(file: 'tls.crt', text: fb64crt.readToString())
I thought if I change it to writeFile(file: 'tls.crt', text: fb64crt.toString()) that it would accept the contents of the function but that doesn't seem to work. Any suggestions are greatly appreciated. I'm not at all familiar with groovy.
Thanks.

jenkins job dsl - No signature of method: javaposse.jobdsl.dsl.jobs.WorkflowJob.pipelineTriggers() is applicable for argument types

I have a DSL script that creates a new job. Part of the code looks like this:
pipelineTriggers {
triggers {
// cron('''${SCHEDULE}''')
parameterizedTimerTrigger {
parameterizedCron('''${SCHEDULE} % INSTANCE=testserver''')
}
}
}
When I run it, I got the error:
Processing provided DSL script
ERROR: (script, line 6) No signature of method: javaposse.jobdsl.dsl.jobs.WorkflowJob.pipelineTriggers() is applicable for argument types: (script$_run_closure1$_closure3) values: [script$_run_closure1$_closure3#8160ead]
Finished: FAILURE
Line 6 is the pipelineTriggers {. I can not find any info online for the particular error. The Job DSL plugin is v. 1.81.
When I tried to use cron('''${SCHEDULE}'''), I got the same error.
If I take off the pipelineTriggers { and just use the triggers, I got an warning saying triggers is deprecated.
Any idea how I can fix this issue?
Thanks!

groovy.lang.MissingMethodException: No signature of method: Why is this error coming for Jenkins Shared Pipeline library?

I have a shared pipeline library code. The library is loaded implicitly in my Jenkins and I'm calling one of the methods using the following code in my Jenkinsfile:
node {
CheckOut {}
}
I've also tried using CheckOut.call() & CheckOut.call([:],{}) but to no avail.
Keep getting the following error:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: CheckOut.call() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: [org.jenkinsci.plugins.workflow.cps.CpsClosure2#668faf1f]
Possible solutions: call(), wait(), any(), wait(long), main([Ljava.lang.String;), any(groovy.lang.Closure)
P.S. - The error is not specific to one function and is happening for all the other functions of the library as well.
Found the issue. While configuring the shared library repo in Jenkins Global Configuration, fill in the link without .git in the end.
For example, use https://github.com/arghyadeep-k/jenkins-shared-library and not https://github.com/arghyadeep-k/jenkins-shared-library.git.
And, then invoke the functions in your Jenkinsfile as
node{
checkOut.call()
}

can't get Groovy ConfigSlurper to parse a String and find result as property

I'm having a really simple problem getting ConfigSlurper to process my config
Groovy version 2.5.6
Went back to basics and tried this simple Groovy script:
ConfigSlurper slurper = new ConfigSlurper ()
slurper.parse ("""host='localhost' """)
println slurper.getProperty('host')
/* gives exception :
Caught: groovy.lang.MissingPropertyException: No such property: host for class: groovy.util.ConfigSlurper
groovy.lang.MissingPropertyException: No such property: host for class: groovy.util.ConfigSlurper
at scripts.testSSlurper.run(testSSlurper.groovy:7)
*/
Why doesn't this simple parse fail?
What am I doing wrong here? This is a blocker for the real code I've written parsing a file - which also seems to bind nothing into slurper.
There is one misunderstanding in your code sample. Parsing a config script does not mutate the ConfigSlurper object, but it returns a ConfigObject instead. All you have to do is to capture the result of slurper.parse(script) method and access host key from the returned ConfigObject instance.
ConfigSlurper slurper = new ConfigSlurper()
def config = slurper.parse(""" host = 'localhost' """)
println config.getProperty("host")
The output:
localhost

How should I write an OptaPlanner Planning Entity PlanningVariable Annotation in Grails?

I tried to convert my OptaPlanner code from Java to Grails. Everything else is fine except I'm stuck when I changed the Planning Entity class into a Groovy file. Then Error message with this would show:
startup failed: F:\Users\Administrator\Documents\workspace-ggts-3.2.0.RELEASE\spa\src\groovy\optaplanner\domain\AllocationEntity.groovy: 15: Annotation list attributes must use Groovy notation [el1, el2] in #org.optaplanner.core.api.domain.variable.PlanningVariable # line 15, column 48. able(valueRangeProviderRefs = {"projects ^ 1 error
And my Intellij IDEA also would prompt an error message when I hover over the line #PlanningVariable(valueRangeProviderRefs = {"projectsRange"}) with red warning highlight under {"projectsRange"}, and the error message is this:
Cannot assign 'Class' to 'String[]'
I wish to use Groovy instead of Java for the GORM feature to query database. But how can I fix this error so I can use the Planning Entity as a Groovy class?
Most Java code is valid Groovy code, but there are a few exceptions, mostly when dealing with curly braces. Closures are defined in Groovy as a code block inside of curly braces, e.g.
def foo = {
...
}
so other uses of curly braces will confuse the Groovy parser. In most cases you just use regular braces instead. In this case your annotation list should be
#PlanningVariable(valueRangeProviderRefs = ["projectsRange"])

Resources