如何使用 Jenkins 参数的两个或多个输入定义盐变量?
How to define a salt variable with two or more inputs from Jenkins parameter?
使用管道脚本未按预期打印 Jenkins 参数。
我在 Jenkins 管道脚本中定义了一个变量:
用户 = "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.
您得到的输出是正确的。您已将输入参数作为字符串,您只是在变量中进行插值。您必须拆分字符串和 prepend/append 字符串才能获得预期结果。
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
使用管道脚本未按预期打印 Jenkins 参数。
我在 Jenkins 管道脚本中定义了一个变量: 用户 = "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.
您得到的输出是正确的。您已将输入参数作为字符串,您只是在变量中进行插值。您必须拆分字符串和 prepend/append 字符串才能获得预期结果。
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