Reputation: 620
I am having problems accessing environment variable in my shell of the Jenkins pipeline. How can I iterate over the array elements in my sh. See code below
def getArray(){
return ['Item1', 'Item2', 'Item3']
}
pipeline {
agent any
environment {
APP = getArray()
}
stages {
stage('Initialize') {
steps {
sh '''
for var in "${APP[@]}" // does not work
do
echo "value : ${var}" // How to echo this value i.e. Item1,Item2,Item3
done
'''
}
}
}
}
Upvotes: 0
Views: 824
Reputation: 3076
The env variable automatically gets cast to the string type. So you can't store a value of an array type in the environment variable.Whatever is assiged to environmental variable is converted to toString()
You can use different options to solve this purpose by either giving a ,
separated string and use a split
function to extract values.
You can also use tokenize(",")
method to produce a list of elements which are separated by ,
.
pipeline {
agent any
environment {
APP="Item1, Item2, Item3"
}
stages {
stage('Hello') {
steps {
script{
env.APP.tokenize(",").each { item ->
echo "$item"
}
// OR //
APP = env.APP.split(",")
for (item in APP) {
echo "${item}"
}
}
}
}
}
}
Upvotes: 1