用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 (将#修改为@)