我有一项任务是根据用户参数将一些值推送给领事,并且在像shutdown_date和termination_date这样运行管道时生成2个值:
def now, shutdown_date, termination_date
pipeline {
parameters {
string(name: 'env', defaultValue: 'abc')
string(name: 'owr', defaultValue: 'abc')
string(name: 'de', defaultValue: 'abc')
string(name: 'tct', defaultValue: 'abc-123')
}
agent { label 'abc' }
stages {
stage ('Update ENV'){
steps {
script {
now = new Date()
println now.format("yyyy-MM-dd", TimeZone.getTimeZone('UTC'))
shutdown_date = now + 170
shutdown_date = (shutdown_date.format("yyyy-MM-dd", TimeZone.getTimeZone('UTC'))).trim()
println shutdown_date
termination_date = now + 365
termination_date = (termination_date.format("yyyy-MM-dd", TimeZone.getTimeZone('UTC'))).trim()
println termination_date
step([$class: 'ConsulKVBuilder', aclToken: '', apiUri: '', debugMode: 'DISABLED', envVarKey: 'env_status', hostUrl: '', key: 'env_status/${env_name}', keyValue: '{ "owr":"${owr}", "de":"${de}", "tct":"${tct}", "shutdown_date": "${shutdown_date}", "termination_date": "${termination_date}" }', requestMode: 'WRITE'])
}
}
}
}
}预期结果:
{ "owr":"abc","de":"abc","tct":"abc-123","shutdown_date":"2020-02-15","termination_date":"2020-08-15“}
实际结果:
{ "owr":"abc","de":"abc","tct":"abc-123","shutdown_date":"${shutdown_date}","termination_date":"${termination_date}“}
发布于 2019-08-16 07:54:35
正如在这个答案中提到的,单引号字符串不会插入变量。
您需要更改step以使用双引号并转义json中的双引号。
step([$class: 'ConsulKVBuilder', aclToken: '', apiUri: '', debugMode: 'DISABLED', envVarKey: 'env_status', hostUrl: '', key: "env_status/${env_name}", keyValue: "{ \"owr\":\"${owr}\", \"de\":\"${de}\", \"tct\":\"${tct}\", \"shutdown_date\": \"${shutdown_date}\", \"termination_date\": \"${termination_date}\" }", requestMode: 'WRITE'])https://stackoverflow.com/questions/57516462
复制相似问题