Issue
I have a text file :
export URL = "useful url"
export NAME = "some name"
What I do is executing this file with command source var_file.txt
But when I do echo $URL
or env.URL
it returns nothing.
Please I don't have the ability to change the file var_file.txt : it means it will still be export var= value var
I know that it is possible to use load file.groovy
step in pipeline to load variables but the file must be a list of : env.URL = 'url'
, I can't use this because I can't change file.
And we may also work with withEnv([URL = 'url'])
but I must first get the values from an other script. This will really be a complicated solution.
So is there a way to use the file with list of export var = var_value
in Jenkins Pipeline ?
Solution
What I have done is :
def varsFile = "var_file.txt"
def content = readFile varsFile
Get content line by line and split change the each line of content to env.variable = value
:
def lines = content.split("\n")
for(l in lines){
String variable = "${l.split(" ")[1].split("=")[0]}"
String value = l.split(" ")[1].split("=")[1]
sh ("echo env.$variable = \\\"$value\\\" >> var_to_exp.groovy")
}
And then load file groovy with step load in the pipeline:
load var_to_exp.groovy
Answered By - sirineBEJI
Answer Checked By - David Marino (JavaFixing Volunteer)