如何解决用Jenkinsfile中的属性文件替换值以动态设置变量
我有一个Jenkinsfile,其中包含多个特定于环境的参数。
这些参数的值存储在属性文件中。我试图根据所选环境设置变量,然后从属性文件中替换它们的值。
Jenkinsfile
ENVIRONMENT是一个选择参数,在我的Jenkinsfile中具有以下两个值:ft,perf和pm
choice (
name: 'ENVIRONMENT',choices: ['ft','perf','pm'],description: 'please select the environment'
)
PROPERTY_FILE是下面的另一个选择参数
choice (
name: 'PROPERTY_FILE',choices: ['jenkins/app.groovy'],description: 'please select the property file'
)
app.groovy看起来像:
Test_perf="Hello"
Test_ft="World"
Test_pm="Welcome"
阶段
stage('Load Environment Property File') {
steps {
script {
//sourcing user selected property file
load "${PROPERTY_FILE}"
def envName = "${PROPERTY_FILE}".tokenize(".")[0]
//it will set build description as description in Jenkins build history
env.envName=envName
currentBuild.description = "Env:${envName}"
}
}
}
//需要帮助的阶段
stage('Set Variables Based upon environment name') {
steps {
script{
if ("${ENVIRONMENT}" == "perf" ){
var="\${Test_"+"${DEPLOYMENT_ENVIRONMENT}"+"}"
echo "${var}" //output is ${Test_pm} and is correct as the substitution happens.
//NOW,I AM TRYING TO echo "HELLO",i.e.; replacing the variable "${Test_perf}" stuffed inside "var" from the property file. However,since the property file is already loaded,it will not replace the key by its value from the property file.
} else if ("${ENVIRONMENT}" == "ft" ){
var="\${Test_"+"${DEPLOYMENT_ENVIRONMENT}"+"}"
echo "${var}" //output is ${Test_ft} and is correct as the substitution happens.
//NOW,I AM TRYING TO echo "World"
} else {
var="\${Test_"+"${DEPLOYMENT_ENVIRONMENT}"+"}"
echo "${var}" //output is ${Test_pm} and is correct as the substitution happens.
//NOW,I AM TRYING TO echo "Welcome"
}
}
}
}
是否有一种方法可以进一步替换属性文件中的值。例如“ $ {var}”扩展为“ $ {Test_pm}”,然后“ $ {Test_pm}”从属性文件中获取值“ Welcome”。 请帮忙。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。