Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to concatenate string with job parameter in pipeline script

I have a Pipeline jenkins job with a checkout step that I want to modify to accept the branch as a parameter.

Currently, this is how we checkout:

stage('Prepare'){
        steps {
               checkout([$class: 'SubversionSCM', "..." remote: 'http://svn.xxx.bbb/svn/yyy/branches/version_2017']]])
        }
}

I would like to change the checkout to something like :

checkout([$class: 'SubversionSCM', "..." remote: 'http://svn.xxx.bbb/svn/yyy/params.BRANCH/params.VERSION']]])

Anyone has done something similar? I can't figure out if it is possible to concatenate a string with job parameters.

like image 213
Ecnerwal Avatar asked Oct 13 '17 15:10

Ecnerwal


People also ask

How do I concatenate strings in a groovy script?

Groovy - Concatenation of Strings The concatenation of strings can be done by the simple '+' operator. Parameters − The parameters will be 2 strings as the left and right operand for the + operator.

How do you concatenate strings and variables?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

How do I combine two string values?

You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs. For string variables, concatenation occurs only at run time.


1 Answers

The thing you're missing is Groovy's String Interpolation: Inside double-quoted strings, ${..} allows you to include groovy code:

def test = 'world'
println "hello ${test}" // prints hello world
println 'hello ${test}' // prints hello ${test}

So in your example, use

remote: "http://svn.xxx.bbb/svn/yyy/${params.BRANCH}/${params.VERSION}"
like image 184
StephenKing Avatar answered Oct 30 '22 17:10

StephenKing