How to handle parameters of pipeline job in DSL? - jenkins

I have a pipeline job which runs a sequence of jobs (Eg; setup >> run test >> clean). I want this sequence to run for 100 different tool with different parameter for each tool. Parameterising is done. So I can pass different parameter.
I am passing one parameter to pipeline as tool list as a string parameter which will have the tools separated with comma (Eg; tool1, tool2, tool3)
Now I want to change the pipeline to run the same sequence(setup >> run test >> clean) in all the tools with tool name passed to one of the job as parameter.
I was thinking I will take this parameter value and spit it with comma and get each tool name and have a for loop around the sequence.
I could get having forloop part and it is working for a multiple run.
for (int i = 0; i < 2; ++i) {
stage('Setup') {
build job: 'setup', parameters: [string(name: 'tool', value:'<tool>')]
}
stage('Build') {
build 'Build Job'
}
}
But I am not getting how can we split the parameter and give one one to each iteration of forloop in DSL. Can anybody help on this?

Parameters in jenkins are available under the "param" variable. And you can split a string into an array of strings using the split () function. After that, if you want to loop over all strings, you can use a for loop, iterating through the array, something like:
def tools = params.tool.split (',') // default separator is blanks
for (String part : tools)
{
println (part)
}
There are different ways to iterate, this seems very intuitive to me. Notice that using the specific separator makes blanks after the comma part of the strings. If needed, you can use the trim () function to remove it.

Related

Jenkins multi string parameter printing strings in single line

I am trying to create on freestyle project with multi string parameter. This will be built by user he have to input string values. Those strings will be embedded to file and pushed to s3 bucket. But the problem is whenever user trying to input multiple strings all the values are coming in single line.
user input
a
b
c
expected output:
a
b
c
Actual output i am getting
a b c
You can use the below while assigning multi-line value to a string:-
def a = """\
test
test
test""".stripIndent()
println(a);
sample parameters
I guess i figured it out.
printf '%s\n' "$tables_retained" > textfile1

How to reverse the words in the string in automation anywhere

I am trying to reverse the string For Example string s= "Hello Robot Process Automation" will be changed to String s="Automation Process Robot Hello" in automation anywhere.
I tried below steps:-
Reverse the sentence(it reverses the words and alphabets in words as well).
2.Split the sentence and put into list variable
used loop and in same loop i reversed again the alphabets so now the sentence will be like "Automation" "Process" "Robot" "Hello" into one list
4 I am not getting next step after this(joining of these words).
Please help.
For reversing the string as per above requirement, below are the steps:
Create a variable for storing the reversed string called vReversedString
Reverse the given string i.e from above string the output should be noitamotuA ssecorP toboR olleH
Split the reversed string by space deliminator and store in my-list-variable
Loop through my-list-variable
Reverse the each element through the loop. For example, In this case the first element is noitamotuA by reversing this you'll be getting Automation as an output, store it to system variable clipboard or create a new variable to hold each element.
Concatenate and store to vReversedString = $vReversedString$ $clipboard$

Set node label in Jenkins pipeline

Is it possible to set a label instead of a name for nodes Groovy? We want to define labels outside the script to easily access them from the Jenkins Dashboard.
Idea:
Instead of:
Groovy Script
node('Slave_1 || Slave_2'){ echo 'Hello world' }
We want something like this:
Pipeline configuration
Node Label Name: slaveGroup
Node Label Value: Slave_1 || Slave_2
Groovy Script
node(slaveGroup){echo 'Hello world'}
Or is it possible to use the labels you can set in slave configuration directly in the Groovy script?
Just found out that the Pipline Syntax (Generator) gives this option:
Valid Operators
The following operators are supported, in the order of precedence.
(expr)
parenthesis
!expr
negation
expr&&expr
and
expr||expr
or
a -> b
"implies" operator. Equivalent to !a|b. For example, windows->x64
could be thought of as "if run on a Windows slave, that slave must be
64bit." It still allows Jenkins to run this build on linux.
a <-> b
"if and only if" operator. Equivalent to a&&b || !a&&!b. For example,
windows<->sfbay could be thought of as "if run on a Windows slave,
that slave must be in the SF bay area, but if not on Windows, it must
not be in the bay area."
All operators are left-associative (i.e., a->b->c <-> (a->b)->c ) An
expression can contain whitespace for better readability, and it'll be
ignored.
Label names or slave names can be quoted if they contain unsafe
characters. For example, "jenkins-solaris (Solaris)" || "Windows 2008"
More in the documentation.

Jenkins conditional steps token logic operators

I inject environmental variable from a file myprop.property that has the contents:
var1=y
var2=y
The build steps:
1. Inject environment variables:
Property File Path:${JENKINS_HOME}/myprop.propertie
Execute Windows batch command (to verify variable injected successully)
echo var1 = %var1% echo var2 = %var2%
Condition steps (multiple)
Run?: Boolean condition
Token: ${ENV,var="var1"}||${ENV,var="var2"}
Steps to run if condition is met: echo Yes, works!
Run the build, the condition in step 3 never met while step 2 display the correct values of the variables.
I have tried the conditions and operators:
var1=y, var2=y: ${ENV,var="var1"}||${ENV,var="var2"}
var1=y, var2=y: ${ENV,var="var1"}|${ENV,var="var2"}
var1=y, var2=n: ${ENV,var="var1"}||${ENV,var="var2"}
var1=y, var2=n: ${ENV,var="var1"}|${ENV,var="var2"}
Uppercase or lower case of the values do not make any difference. I am running jenkins 1.641 on windows 7 pro.
If I use only one e.g. ${ENV,var="var1"} in the token field, it works as expected.
Try this instead, It worked for me, we need to mention Boolean Condition with Token mentioned in the image.
This works, change step 3 to:
Run?: Or
Boolean condition
Token: ${ENV,var="var1"}
Or
Boolean condition
Token: ${ENV,var="var2"}
Never
these are actually three conditions. the execution asserts the first condition first, if it's met, stop checking; if not met, asserts the second; and so on until the last condition that is Never, meaning if no condition is met, stop executing the step.
I see my post attracted lots interests. Here are what I eventually worked around.
Use Regular expression match seems to be more readable than using Boolean Condition.
If the logic is an "OR",
? = Regular expression match
expression = ${ENV,var="var1"}|${ENV,var="var2"}
Label = Y
Note that the logic operator is a single '|', not double '||'.
if the logic is an "AND",
? = Regular expression match
expression = ${ENV,var="var1"}${ENV,var="var2"}
Label = YY
The expression is just concatenation of the two variables, and the Label is so as well.

How do we generate all parameter combination to form a URL

Suppose I have a url
xyz.com/param1=abc&param2=123&param3=!##
Each parameter can have many values like:
param1 = abc, xyz, qwe
param2 = 123, 456, 789
param3 = !##, $%^, &*(
All the parameters and values will be read from an excel file. There can be n number of parameters and each may have any number of values.
I want to generate all the combinations which can be formed using all values of each parameters.
Output will be like:
xyz.com/param1=abc&param2=123&param3=!##
xyz.com/param1=xyz&param2=456&param3=!##
xyz.com/param1=qwe&param2=123&param3=!##
xyz.com/param1=qwe&param2=456&param3=!##
xyz.com/param1=xyz&param2=789&param3=$%^
...
..
and so on
besides the previous comment in your post, you need construct some nested loops.
I will assume you have a bash shell available (since you hadn't specified your wanted language).
for I in 'abc' 'xyz' 'qwe'
do
for J in '123' '456' '789'
do
for K in '!##' '$%^' '&*('
do
echo "xyz.com/param1=${I}&param2=${J}&param3=${K}"
done
done
done
Note that:
that '&*(' will bring you problems, since & is the character that you use to delimit each parameter.
double quotes " around echo, will make a double quote character to make this program to fail miserably
the same apply to ', \ and some others

Resources