DSL Jenkins pass parameter to phaseJob - jenkins

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")

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}"

Injecting variable from jenkins into jenkins pipeline

I am using a password parameter option in jenkins pipeline job where i store a password and need to inject that into the environment of jenkins file
Here is the part of the jenkins file where i am trying to inject this PASS
pipeline {
agent any
environment {
USER= 'abcd'
PW= '${params.PASS}'
}
stages {
stage('staring tests') {
When i try echo-ing $PW, it just echoes ${params.PASS}.
Any pointers will be appreciated!!
In Groovy Strings are single quotes. They cannot be interpolated with variables. You would need to concatenate strings and values. i.e. PW='pass' + myVar
GStrings are double quotes. http://docs.groovy-lang.org/latest/html/api/groovy/lang/GString.html
So you would need to change it to PW="${params.PASS}" and this would interpolate the variable into the string.
However since your not doing any interpolation in that example, you don't need to use GStrings either. Whereever you need access to that value just call it directly echo params.PASS
You should not put a 'params' value into an environment variable. It won't work for you. The environment sections initialize only string variables, therefore, it initializes your PW with a pure string of ${params.PASS} since it does no 'calculations' for it.
Instead, in your pipeline, just use the params.PASS directly in your code.

Space separated string parameter in Jenkins String Parameter

I have a string parameter in jenkins called 'Keywords'.
I set the value of Keywords = "Google,Microsoft,Uber Go".
But jenkins string parameter takes only till "Google,Microsoft,Uber" and truncates "Go" automatically.
Due to this my code runs incorrectly.
Can anyone help in handling this auto-truncate issue in jenkins string parameter?
This string parameter is used in my python code where I split the Keywords on comma(,) and use the 3 generated words for operation in my code. But jenkins is truncating "Go" automatically due to which incorrect word is processed by my code.
Found the solution for this.
While calling the string parameter variable "Keywords" put in Double quotes. Shell script command to be put in Jenkins :
python script.py "${Keywords}"

String parameter as lockable resources label in 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)

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