配置流水线时,配置了参数,如何在jenkinsfile中覆盖参数的值?
如下图,我在流水线中配置了fullTime
参数,默认值为11111111
。
如何在jenkinsfile中覆盖该参数的值呢?
node('openjdk8') {
def date = new Date()
def time = date.format('yyyyMMddHHmmss')
def msec = System.currentTimeMillis()
if (fullTime == "11111111"){
fullTime = "${time}" + "${msec}"
echo "${fullTime}"
}
stage('test') {
echo "${fullTime}"
}
}
输出结果中:
2022-04-08 13:45:11.199 | . [Pipeline] {
2022-04-08 13:45:11.224 | . [Pipeline] echo
2022-04-08 13:45:11.230 | . 202204080545111649396711212
2022-04-08 13:45:11.248 | . [Pipeline] stage
2022-04-08 13:45:11.253 | . [Pipeline] { (test)
2022-04-08 13:45:11.273 | . [Pipeline] echo
2022-04-08 13:45:11.276 | . 202204080545111649396711212
2022-04-08 13:45:11.279 | . [Pipeline] }
2022-04-08 13:45:11.291 | . [Pipeline] // stage
2022-04-08 13:45:11.298 | . [Pipeline] }
2022-04-08 13:45:11.313 | . [Pipeline] // node
2022-04-08 13:45:11.323 | . [Pipeline] End of Pipeline
2022-04-08 13:45:11.356 | . Finished: SUCCESS
但是,如果使用下面的jenkinsfile再次定义fullTime
,此时jenkinsfile中定义的fullTime
作用范围只在if
结构内,所以输出仍为11111111
。
node('openjdk8') {
def date = new Date()
def time = date.format('yyyyMMddHHmmss')
def msec = System.currentTimeMillis()
if (fullTime == "11111111"){
def fullTime = "${time}" + "${msec}"
echo "${fullTime}"
}
stage('test') {
echo "${fullTime}" //输出结果为11111111
}
}
网友评论