Search code examples
jenkins-pipelinesalt-project

How to define a salt variable with two or more inputs from Jenkins parameter?


Jenkins parameter is not printed as expected using a pipeline script.

I am defining a variable in Jenkins pipeline script as: USER = "xx-yy-${Target}-zzz" Here ${Target} is from a Jenkins comma separated parameters (server1,server2).

properties([
parameters([
    string(defaultValue: '', description: 'Comma-separated list', name: 'Target')
    ])
])
USER = "xx-yy-${Target}-zzz"
node('master') {
stage('pass_the_salt'){

}

When I print the USER, the result is wrongly printed as xx-yy-server1,server2-zzz. The expected result is xx-yy-server1-zzz,xx-yy-server2-zzz.


Solution

  • Output you are getting is correct. You have taken the input parameter as string and you are just interpolating in a variable. You have to split the string and prepend/append the string to get your expected result.

    user_input = "server1,server2"  # equivalent to your Target input parameter
    def list = []
    def arr = user_input.split(",") # splitting the input with , as delimiter
    for( String srv: arr ) {
      list << "xx-yy-${srv}-zz"     # creating a new list with your expected prepend/append string
    }
    
    print list.join(",") # Joining the output list with , as delimiter
    
    # result looks as below
    xx-yy-server1-zz,xx-yy-server2-zz