属性 文件中基于全局环境变量的 Jenkins 参数

Jenkins parameters from property file based on global env vars

上下文:

jenkins 作业的输入参数在 属性 文件中定义。 属性 文件位置根据 jenkins 所在的环境而变化 运行

例如:对于 dev 环境路径类似于 /app/dev/some/nested/path/propertyfile

for prod 环境路径类似于 /app/prod/some/nested/path/propertyfile

目前使用扩展选择参数插件读取属性文件。如果 属性 文件的路径是绝对路径,这很有效。

问题:

有没有办法在 属性 文件路径中包含全局环境变量? 可以使用 active-choices 插件 来完成吗?

使用主动选择插件

def choices=[]
textFile= new File("/app/${Gloabl_Var}/some/nested/path/propertyfile")
textFile.eachLine{ line ->
if(line.startsWith('<Property-Key-Name>')) {
 line.split('=')[1].split(',').each {
choices.add(it)
}}}
return choices

使用扩展选择参数插件

不能直接使用属性文件,需要使用groovy脚本

import hudson.slaves.EnvironmentVariablesNodeProperty
import jenkins.model.Jenkins
def global_env_var=Jenkins.get().globalNodeProperties.get(EnvironmentVariablesNodeProperty.class).envVars['<Global_Var>']
def props = new Properties()
def stream = new FileInputStream("/app/${global_env_var}/some/nested/path/propertyfile")
try {
props.load(stream)
} finally {
stream.close()
}
return props.getProperty('<Property-Key-Name>').split(',')

参考文献:

extended-choice

active-choices