Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable path in dir() jenkins pipeline

I have created a jenkinsfile in which I need to go into an already created directory. But the problem is that the name of the directory can be variable, eg: dir ('RELEASE/abc/${abc-version}'). The abc-version is passed as a string argument in the pipeline.

The problem is that, on running the pipeline, jenkins creates a directory named as "${abc-version}" rather than passing it as an argument.

I have failed in doing so till now. Is this even possible? Can we pass a variable in the dir step?

EDIT: I tried with %abc-version% and it still doesn't work. It instead creates a directory with %abc-version% rather than taking it as a variable.

Also, my parameter is like: abc-version. Has it anyting to do with the problem?

like image 341
Swapnil Pandey Avatar asked Dec 03 '22 19:12

Swapnil Pandey


2 Answers

Use double quotes

dir ("RELEASE/abc/${abc_version}")

in groovy Single quotes are a standard Java String while Double quotes are a templatable String

e.g

a = 10
b = "RELEASE/abc/${a}"
c = 'RELEASE/abc/${a}'
print(b)
print('\n')
print(c)

output will be

RELEASE/abc/10
RELEASE/abc/${a}

You can try it here

like image 169
Talha Junaid Avatar answered Dec 26 '22 12:12

Talha Junaid


We need to use double quotes to resolve variable in groovy script.

use the following sample code

def dirpath = "RELEASE/abc/${abc-version}"
dir(dirpath){
  //logic
}
like image 35
Ramakrishna Yelduti Avatar answered Dec 26 '22 11:12

Ramakrishna Yelduti