如何使用Jenkins参数的两个或多个输入定义盐变量?

时间:2019-02-08 08:46:50

标签: jenkins-pipeline salt-stack

使用管道脚本无法按预期方式打印出詹金斯参数。

我在Jenkins管道脚本中将变量定义为: USER =“ xx-yy-$ {Target} -zzz” 这里$ {Target}来自Jenkins逗号分隔的参数(server1,server2)。

properties([
parameters([
    string(defaultValue: '', description: 'Comma-separated list', name: 'Target')
    ])
])
USER = "xx-yy-${Target}-zzz"
node('master') {
stage('pass_the_salt'){

}

当我打印USER时,结果被错误地打印为xx-yy-server1,server2-zzz。预期结果是xx-yy-server1-zzz,xx-yy-server2-zzz。

1 个答案:

答案 0 :(得分:0)

您得到的输出是正确的。您已经将输入参数作为字符串,并且只是对变量进行插值。您必须拆分字符串并在字符串前/后添加字符串才能获得预期的结果。

user_input = "server1,server2"  # equivalent to your Target input parameter
def list = []
def arr = user_input.split(",") # splitting the input with , as delimiter
for( String srv: arr ) {
  list << "xx-yy-${srv}-zz"     # creating a new list with your expected prepend/append string
}

print list.join(",") # Joining the output list with , as delimiter

# result looks as below
xx-yy-server1-zz,xx-yy-server2-zz