Reputation: 103
I am trying to run a pipeline that has several servers. I want to do some actions in several servers at a time when selecting a choice parameter. My idea is to select a choice parameter 'APPLICATION' and execute some actions on 4 different servers sequentially (one server at a time). I am trying to put the environment variables assigning the value os the servers in an array and then ask for the environment variable to execute the actions.
pipeline {
agent {
node {
label 'master'
}
}
environment {
APPLICATION = ['veappprdl001','veappprdl002','veappprdl003','veappprdl004']
ROUTER = ['verouprdl001','verouprdl002']
}
parameters {
choice(name: 'SERVER_NAME', choices: ['APPLICATION','ROUTER'], description: 'Select Server to Test' )
}
stages {
stage ('Application Sync') {
steps {
script {
if (env.SERVER_NAME == 'APPLICATION') {
sh """
curl --location --request GET 'http://${SERVER_NAME}//configuration-api/localMemory/update/ACTION'
"""
}
}
}
}
} }
I want to execute the action on all the servers of the 'APPLICATION' variable if is selected the 'APPLICATION' parameter in 'Build with parameters'.
Any Help would be appreciate it.
Thanks
Upvotes: 10
Views: 13023
Reputation: 42184
You can't store a value of an array type in the environment variable. Whatever you are trying to assign to the env variable gets automatically cast to the string type. (I explained it in more detail in the following blog post or this video.) So when you try to assign an array, what you assign is its toString()
representation.
However, you can solve this problem differently. Instead of trying to assign an array, you can store a string of values with a common delimiter (like ,
for instance.) Then in the part that expects to work with a list of elements, you simply call tokenize(",")
method to produce a list of string elements. Having that you can iterate and do things in sequence.
Consider the following example that illustrates this alternative solution.
pipeline {
agent any
environment {
APPLICATION = "veappprdl001,veappprdl002,veappprdl003,veappprdl004"
}
stages {
stage("Application Sync") {
steps {
script {
env.APPLICATION.tokenize(",").each { server ->
echo "Server is $server"
}
}
}
}
}
}
When you run such a pipeline you will get something like this:
Upvotes: 16