user8453102
user8453102

Reputation:

Passing parameter from for loop to multiline sh in jenkins pipeline

I have groovy jenkins pipeline step and I want to pass for loop value as parameter to multiline sh script in loop. But parameter is not getting passed.

Or if theres a better way to add step in jenkins stage?

for (int i = 0; i < elements.size(); i++) {

    sh '''
       cd terraform/
       terraform init
       terraform workspace select ${elements[i]}-${envtype}
       terraform plan -var-file="./configs/${elements[i]}/var.tf"
    '''
}

Upvotes: 5

Views: 5107

Answers (2)

Michael
Michael

Reputation: 2683

You need a triple double quoted string. You are using a triple single quoted string. Any single quoted string in Groovy does not feature String interpolation, so '''${i}''' prints ${i}, while """${i}""" prints 3 (if i = 3).

Upvotes: 1

Opal
Opal

Reputation: 84854

It seems that you should use """ instead of '''. ''' is triple single quoted String and doesn't support interpolation.

Upvotes: 7

Related Questions