我们可以在jenkins主动选择参数中读取配置文件(托管文件.properties文件)吗?

问题描述 投票:0回答:2

我想在主动选择参数grovy脚本中读取我的属性文件,我的属性文件存储在托管文件中。

属性文件如下所示 [1]:https://i.stack.imgur.com/flvP5.png

我想在 Active Choice Reference Parameter Groovy 脚本中调用这些属性文件并检索 所有值都按照我之前的选择。我一直在使用不同的无法检索值的方法,我们可以通过它们检索值吗?

jenkins groovy jenkins-pipeline jenkins-groovy
2个回答
1
投票

属性文件会像这样'cat /var/jenkins_home/workspace/ss.properties'

100.1.1.1=outside_in,wireless_in
200.x.x.x=mgmt_in,inside_in

创建一个名为“Active Choices Parameter”的参数,将其命名为“主机名”并在其中编写以下 groovy 脚本。在下面的 groovy 脚本中,我们只是从属性文件中读取键,将其转换为列表并将其填充到选择参数中。在我的例子中,选择类型是单选,或者您可以根据您的需要进行设置。

Properties properties = new Properties() 
File propertiesFile = new File('/var/jenkins_home/workspace/ss.properties') 
def stream = propertiesFile.newDataInputStream() 
properties.load(stream) 
Enumeration e = properties.propertyNames(); 
List<String> list = Collections.list(e); 
return list

创建另一个名为“Active Choices Reactive Parameter”的参数,并将其命名为“config_list”,然后将以下 groovy 脚本粘贴到其中

Properties properties = new Properties()
File propertiesFile = new File('/var/jenkins_home/workspace/ss.properties')
def stream = propertiesFile.newDataInputStream()
properties.load(stream)
Enumeration e = properties.propertyNames();
def var = Arrays.asList(properties.getProperty(hostnames).split(','))
return var

上述的参考参数是“主机名”。这意味着,根据主机名选择参数的选择,数据将填充到该参数中。在我的例子中,选择类型是单选,或者您可以根据您的需要进行设置。

保存配置并在 Jenkins 作业中单击使用参数构建,它应该如下所示


0
投票

我尝试过这个解决方案,但似乎不起作用,知道为什么吗?我使用最新版本的 Jenkins 和主动选择参数插件

© www.soinside.com 2019 - 2024. All rights reserved.