Reputation: 13734
I've tried various variations of the below code, none of them seems to work
def runScript(command){
sh '''#!/bin/bash
file="env.txt"
while IFS='=' read -r key value
do
export ${key}="${value}"
done < "$file"
pwd
"${command}"
'''
}
command
is the dynamic shell command that I want to execute after re-creating the environment variables from env.txt
Any idea ?
Upvotes: 2
Views: 6187
Reputation: 13734
Use this
def runScript(command){
sh '''#!/bin/bash
file="env.txt"
while IFS='=' read -r key value
do
export ${key}="${value}"
done < "$file"
pwd
''' + "${command}"
}
The single quotes '''
will create multi-line shell script. The "
quotes will get the value of command variable and concatenate it with shell script.
Note that there are two shell variables key
& value
& hence, '''
can't be replaced with """
References :
Access a Groovy variable from within shell step in Jenkins pipeline
How to pass variables from Jenkinsfile to shell command
Upvotes: 3