Issue
{
"TEST_SCRIPTS":["test_1.py","test_2.py"],
"TEST_SCRIPTS1":"test_1.py;test_2.py"
}
This json file, I load in my Jenkins pipeline using :
def load_config(){
def config = readJSON file "./test.json"
return config
}
Now, I need a loop in shell script which can execute each python files defined in TEST_SCRIPTS & TEST_SCRIPTS1.
stage('Test') {
steps {
script{
config = load_config()
sh """
conda env create -n test_env_py37 -f conda.yaml
conda activate test_env_py37
// Below loop is not working. This env is huge, and mendatory for below code to run
for test_script in ${config.TEST_SCRIPTS};
do
python "\$test_script"
done
for test_script in ${config.TEST_SCRIPTS1};
do
python "\$test_script"
done
"""
}
}
}
Solution
You can use a groovy approach instead of a shell one, and do all the parsing logic using groovy functionality.
Something like:
stage('Test') {
steps {
script {
def config = readJSON file "./test.json"
def testScripts = config.TEST_SCRIPTS.collect { "python \"$it\""}.join("\n")
def testScripts1 = config.TEST_SCRIPTS1.split(';').collect { "python \"$it\""}.join("\n")
sh """
conda env create -n test_env_py37 -f conda.yaml
conda activate test_env_py37
${testScripts}
${testScripts1}
"""
}
}
}
Answered By - Noam Helmer
Answer Checked By - Marilyn (JavaFixing Volunteer)