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?
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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With