String parameter as lockable resources label in jenkins - jenkins

I am trying to setup a job with the use of lockable resources.
My job is parameterized and one of the String parameters is parameter 'ENVIRONMENT'.
I would like to use this parameter as groovy expression label for lockable resources plugin, i.e.:
groovy:resourceLabels.contains(${ENVIRONMENT})
but the expression is not being evaluated.
Is there a way to make it evaluate String parameter like this?

I'm not sure it's the only problem, but it looks like double-quotes are missing
groovy:resourceLabels.contains("${ENVIRONMENT}")
cf groovy gstring

Job Parameters are more correctly referenced from the params object:
With this the groovy string interpolation is not required,
groovy:resourceLabels.contains(params.ENVIRONMENT)

Related

Parameterized pipeline with choice has trailing comma

I have a jenkins pipeline which uses the defualt choice parameter.
parameters {
choice(choices: ['optA', 'optB'], description: 'Some options', name: 'CHOSEN')
}
Whenever it run the chosen options will always have a comma appended to it. Resulting in the step it's used in taking it as part of the argument.
someprogram.sh optA,
This breaks everything. How do I disable the comma? Also why is this default behavior?
I tried removing it with a combination of shell substition and jenkins groovy string manipulation.
I searched for the option 'omit field value' which 'fixes' this for a choices plugin (I am not using the reactive choices plugin but this is the only response I could find) When I try to find the 'omit field value' box in pipeline settings in parameters it doesn't exists.
Try replacing it like below.
sh "someprogram.sh ${params.CHOSEN}"

How can I pass a list of parameters to Jenkins from a file

I need to pass a list of parameters to Jenkins from a file written in this way
param1=test1
param2=test2
paramBool=true
....
How can I pass these parameters?
I have to use the parameters in a Jenkins pipeline
I mentioned before a dynamic declarative pipeline parameters.
You might combine it with:
either readFile
or load, which can evaluate a Groovy source file into the Pipeline script, and modify params.

How can I specify a release branch regular expression in Jenkins job configuration

In the Branches to build field in Jenkins I would like it to use a branch name with the prefix:
Production-release-
A number would follow the second dash as supplied by our groovy script.
What is the proper syntax to show a wildcard at the end of this prefix?
Assuming you want to use it in a branch statement in declarative syntax, you can use an asterisk (*). Production-release-* should do the job.

DSL Jenkins pass parameter to phaseJob

On Groovy DSL how can I use jenkins parameters in a phaseJob name.
When trying to do
stringParam('jobName', 'bla', 'blabla')
...
phaseJob('$jobName')
jobName is not replaced with the value I entered in Jenkins
You are using single quotes. As you can read here
Any Groovy expression can be interpolated in all string literals, apart from single and triple single quoted strings.
You need to use double quotes in order to make "string ${interpolation}" work:
phaseJob("$jobName")

Using "$" in Groovy

I see { } are used for closures, and then I believe when a $ is put in front of braces, it is simply doing a variable substitution within a string. I can't find the documentation on how the $ works in the reference ... hard to search on it unfortunately, and the Groovy String documentation is lacking in introducing this. Can you please point me to the documentation and/or explain the "$" operator in Groovy -- how all it can be used? Does Grails extend it at all beyond Groovy?
In a GString (groovy string), any valid Groovy expression can be enclosed in the ${...} including method calls etc.
This is detailed in the following page.
Grails does not extend the usage of $ beyond Groovy. Here are two practical usages of $
String Interpolation
Within a GString you can use $ without {} to evaluate a property path, e.g.
def date = new Date()
println "The time is $date.time"
If you want to evaluate an expression which is more complex than a property path, you must use ${}, e.g.
println "The time is ${new Date().getTime()}"
Dynamic Code Execution
Dynamically accessing a property
def prop = "time"
new Date()."$prop"
Dynamically invoking a method
def prop = "toString"
new Date()."$prop"()
As pointed out in the comments this is really just a special case of string interpolation, because the following is also valid
new Date().'toString'()
$ is not an operator in Groovy. In string substitution it identifies variables within the string - there's no magic there. It's a common format used for inline variables in many template and programming languages.
All special Groovy operators are listed here: http://groovy-lang.org/operators.html
Work in side Jenkins File in pipeline
#!/usr/bin/env groovy
node{
stage ('print'){
def DestPath="D\$\\"
println("DestPath:${DestPath}")
}
}

Resources