如何从Jenkins 2.0管道脚本读取属性文件
我正在尝试编写与Jenkins 2.0一起使用的管道脚本来复制我们现有的构建。这个原始版本使用envInject插件来读取一个Java属性文件,但我看不到如何从流水线Groovy脚本执行此操作。我已经谷歌搜索,发现以下,但它不起作用(FileNotFoundException):如何从Jenkins 2.0管道脚本读取属性文件
Properties props = new Properties()
File propsFile = new File('./Builder/project.properties')
props.load(propsFile.newDataInputStream())
谢谢!
我只是昨天和今天一起战斗。我希望这个更容易找到。
抓住'Pipeline Utility Steps'插件。
使用readProperties步骤。
def props = readProperties file: 'dir/my.properties'
一个字的警告 - 我期望在属性文件中的布尔值被视为字符串。
谢谢,我已经这么做了,但它的效果差不多和旧的Env插件一样。我有两个特性: SOFTWARE.VERSION = 3.6 RELEASE = $ {} SOFTWARE.VERSION 这在旧版本詹金斯工作正常,但詹金斯2.0无法识别RELEASE参数为3.6 –
@PeteSingleton这些属性应该是分开行。 –
嗨,他们分开行(复制/粘贴问题!): –
我尝试了以下工作完全没有:
test.properties
Monday=abcdef
Tuesday=kfgh
def props = readProperties file:'/var/lib/jenkins/jobs/abc/test.properties'
def Var1= props['Monday']
def Var2= props['Tuesday']
echo "Var1=${Var1}"
echo "Var2=${Var2}"
我无法弄清楚如何从插值的readProperties纯文本,所以我刚刚作出了一个解决方法,以扩展该变量。
def props = readProperties file: 'dir/my.properties'
def release = expand_property(props['RELEASE'])
def expand_property(property) {
def info
node("anyUnixNode") {
info = sh(script: "echo $property", returnStdout: true)
}
info = info.trim()
return info
}
显示的代码,如果属性文件在** classpath **上,那么就使用'new File('project.properties')'。如果不在类路径上,则转到**绝对**路径,例如'新文件('C:\\ Users \\ John \\ Documents \\ project.properties')' – smoggers
谢谢,绝对路径是Jenkins工作区签出的一部分,所以不愿意对它进行硬编码。不知道在Jenkins构建中如何设置类路径? –