Jenkinsfile 打印参数很好
Jenkinsfile print parameters nicely
我们有一个 Jenkinsfile,其参数声明如下:
def params = [string(name: 'ENVIRONMENT', value: environment),
string(name: 'VERSION', value: version),
string(name: 'REGION', value: region)]
我想回应这些,例如
DEPLOYING: ENVIRONMENT=STAGING, VERSION=1.3.0, REGION=EU
但是调用 echo "$params"
打印:
[@string(name=ENVIRONMENT,value=STAGING), @string(name=VERSION,value=1.3.0), @string(name=REGION,value=EU)]
我尝试迭代数组 - 例如:
params.each { echo it }
抛出 UnsupportedOperationException: no known implementation of class java.lang.String is using symbol ‘string’
params.each { echo it.name }
抛出 RejectedAccessException: No such field found: field org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable name
我怎样才能很好地打印 params
数组?
编辑 - 来自 Matt Schuchard 的回复:
def params = [string(name: 'ENVIRONMENT', value: "STAGING"),
string(name: 'VERSION', value: "1.3.0"),
string(name: 'REGION', value: "EU")]
print "$params"
params.each() { param, value ->
print "Parameter: ${param}, Value: ${value}"
}
returns(即值全为空):
[@string(name=ENVIRONMENT,value=STAGING), @string(name=VERSION,value=1.3.0), @string(name=REGION,value=EU)]
Parameter: @string(name=ENVIRONMENT,value=STAGING), Value: null
Parameter: @string(name=VERSION,value=1.3.0), Value: null
Parameter: @string(name=REGION,value=EU), Value: null
您可以使用 Groovy 地图迭代器 lambda 方法(例如 each
)迭代 params
地图。示例如下:
params.each() { param, value ->
print "Parameter: ${param}, Value: ${value}"
}
如果您决定直接在 pipeline
块中使用它,那么在使用声明性 DSL 时需要将其放在 script
块中。
必须是更好的方法,但下面的方法有效
迭代参数数组:
1. Cast each parameter to a regular String
2. Extracted the value between @string( VALUE )
3. Split the key/value pairs first by comma ','
4. Then split each side of the above by equals '='
5. Concatenate the second argument
def pretty(params) {
def s = ""
params.each { param ->
def a = ("${param}" =~ /^@string\((.+)\)$/)[ 0 ][ 1 ]
def b = a.split(",")
s = s + b[0].split("=")[1] + "=" + b[1].split("=")[1] + "\n"
}
return s
}
Returns:
ENVIRONMENT=STAGING
VERSION=1.3.0
REGION=EU
是params
目标是参数化构建吗?
如果是这种情况,您可以使用 parameters
指令
The parameters
directive provides a list of parameters that a user
should provide when triggering the Pipeline.
The values for these
user-specified parameters are made available to Pipeline steps via the
params
object.
递减管道:
Declarative Pipeline supports parameters out-of-the-box, allowing the
Pipeline to accept user-specified parameters at runtime via the
parameters directive.
parameters {
string(name: 'ENVIRONMENT', defaultValue: 'STAGING', description: 'Environment to test. e.g: production/develop'),
string(name: 'VERSION', defaultValue: '1.3.0', description: 'Version to run'),
string(name: 'REGION', defaultValue: 'EU', description: 'Region')
}
脚本管道:
Configuring parameters with Scripted Pipeline is
done with the properties step, which can be found in the Snippet
Generator
properties([
parameters([
string(name: 'ENVIRONMENT', defaultValue: 'STAGING', description: 'Environment to test. e.g: production/develop'),
string(name: 'VERSION', defaultValue: '1.3.0', description: 'Version to run'),
string(name: 'REGION', defaultValue: 'EU', description: 'Region')
])
])
现在,这些参数可以作为 params
变量的成员访问
所以,那么你可以简单地使用:
params.each() { param, value ->
print "Parameter: ${param}, Value: ${value}"
}
我们有一个 Jenkinsfile,其参数声明如下:
def params = [string(name: 'ENVIRONMENT', value: environment),
string(name: 'VERSION', value: version),
string(name: 'REGION', value: region)]
我想回应这些,例如
DEPLOYING: ENVIRONMENT=STAGING, VERSION=1.3.0, REGION=EU
但是调用 echo "$params"
打印:
[@string(name=ENVIRONMENT,value=STAGING), @string(name=VERSION,value=1.3.0), @string(name=REGION,value=EU)]
我尝试迭代数组 - 例如:
params.each { echo it }
抛出 UnsupportedOperationException: no known implementation of class java.lang.String is using symbol ‘string’
params.each { echo it.name }
抛出 RejectedAccessException: No such field found: field org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable name
我怎样才能很好地打印 params
数组?
编辑 - 来自 Matt Schuchard 的回复:
def params = [string(name: 'ENVIRONMENT', value: "STAGING"),
string(name: 'VERSION', value: "1.3.0"),
string(name: 'REGION', value: "EU")]
print "$params"
params.each() { param, value ->
print "Parameter: ${param}, Value: ${value}"
}
returns(即值全为空):
[@string(name=ENVIRONMENT,value=STAGING), @string(name=VERSION,value=1.3.0), @string(name=REGION,value=EU)]
Parameter: @string(name=ENVIRONMENT,value=STAGING), Value: null
Parameter: @string(name=VERSION,value=1.3.0), Value: null
Parameter: @string(name=REGION,value=EU), Value: null
您可以使用 Groovy 地图迭代器 lambda 方法(例如 each
)迭代 params
地图。示例如下:
params.each() { param, value ->
print "Parameter: ${param}, Value: ${value}"
}
如果您决定直接在 pipeline
块中使用它,那么在使用声明性 DSL 时需要将其放在 script
块中。
必须是更好的方法,但下面的方法有效
迭代参数数组:
1. Cast each parameter to a regular String
2. Extracted the value between @string( VALUE )
3. Split the key/value pairs first by comma ','
4. Then split each side of the above by equals '='
5. Concatenate the second argument
def pretty(params) { def s = "" params.each { param -> def a = ("${param}" =~ /^@string\((.+)\)$/)[ 0 ][ 1 ] def b = a.split(",") s = s + b[0].split("=")[1] + "=" + b[1].split("=")[1] + "\n" } return s }
Returns:
ENVIRONMENT=STAGING
VERSION=1.3.0
REGION=EU
是params
目标是参数化构建吗?
如果是这种情况,您可以使用 parameters
指令
The
parameters
directive provides a list of parameters that a user should provide when triggering the Pipeline.
The values for these user-specified parameters are made available to Pipeline steps via theparams
object.
递减管道:
Declarative Pipeline supports parameters out-of-the-box, allowing the Pipeline to accept user-specified parameters at runtime via the parameters directive.
parameters {
string(name: 'ENVIRONMENT', defaultValue: 'STAGING', description: 'Environment to test. e.g: production/develop'),
string(name: 'VERSION', defaultValue: '1.3.0', description: 'Version to run'),
string(name: 'REGION', defaultValue: 'EU', description: 'Region')
}
脚本管道:
Configuring parameters with Scripted Pipeline is done with the properties step, which can be found in the Snippet Generator
properties([
parameters([
string(name: 'ENVIRONMENT', defaultValue: 'STAGING', description: 'Environment to test. e.g: production/develop'),
string(name: 'VERSION', defaultValue: '1.3.0', description: 'Version to run'),
string(name: 'REGION', defaultValue: 'EU', description: 'Region')
])
])
现在,这些参数可以作为 params
变量的成员访问
所以,那么你可以简单地使用:
params.each() { param, value ->
print "Parameter: ${param}, Value: ${value}"
}