groovy - 将 Jenkinsfile 中属性文件的值替换为动态设置的变量
问题描述
我有一个 Jenkinsfile,其中包含多个特定于环境的参数。
这些参数的值存储在属性文件中。我正在尝试根据所选环境设置变量,然后从属性文件中替换它们的值。
詹金斯文件
ENVIRONMENT 是具有 2 个值的选择参数:ft、perf 和 pm,如下所示在我的 Jenkinsfile
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"。请帮忙。
解决方案
推荐阅读
- reactjs - 有没有办法在提交时标准化 ant 设计输入的值?
- android - 将自定义视图调整到不同的屏幕
- python - Keras 和 Tensorflow 无法正常工作,显示使用 Anaconda 安装的 Windows 10、Python 3.7.4、Keras 2.3.1、tensorflow 2.1.0 的错误
- python - Pandas DF 按月和日计算的所有年份平均值
- node.js - 如何正确配置和计算 Sequelize 中的 belongsToMany 关联?
- javascript - Javascript为什么返回空数组
- arrays - 保存具有不同 ID Swift 的 JSON 文件
- node.js - 无法在 .spec.ts 文件 NodeJs 中的模块外使用 import 语句
- python - Pandas 到 CSV 和 asyncio
- html - Prelaod 多次获取