问题描述
我想用一个简单的属性配置文件来参数化我的 Jenkins 管道
I want to parametrize my Jenkins pipeline with a simple properties config file
skip_tests=true
我已添加到 Jenkins 配置文件管理中:
that I've added to Jenkins Config File Managment:
在我的管道中,我正在导入此文件并尝试使用 Jenkins 管道配置文件插件从中读取.
In my pipeline I'm importing this file and try to read from it using the Jenkins Pipeline Config File Plugin.
node('my-swarm') {
MY_CONFIG = '27206b95-d69b-4494-a430-0a23483a6408'
try {
stage('prepare') {
configFileProvider([configFile(fileId: "$MY_CONFIG", variable: 'skip_tests')]) {
echo $skip_tests
assert $skip_tests == 'true'
}
}
} catch (Exception e) {
currentBuild.result = 'FAILURE'
print e
}
}
这会导致错误:
provisioning config files...
copy managed file [my.properties] to file:/home/jenkins/build/workspace/my-workspace@tmp/config7043792000148664559tmp
[Pipeline] {
[Pipeline] }
Deleting 1 temporary files
[Pipeline] // configFileProvider
[Pipeline] }
[Pipeline] // stage
[Pipeline] echo
groovy.lang.MissingPropertyException: No such property: $skip_tests for
class: groovy.lang.Binding
有什么想法我在这里做错了吗?
Any ideas what I'm doing wrong here?
推荐答案
借助其他答案和如何从 Jenkins 2.0 管道脚本中读取属性文件 我发现以下代码可以工作:
With the help of the other answers and How to read properties file from Jenkins 2.0 pipeline script I found the following code to work:
configFileProvider([configFile(fileId: "$PBD1_CONFIG", variable: 'configFile')]) {
def props = readProperties file: "$configFile"
def skip_tests = props['skip_tests']
if (skip_tests == 'true') {
print 'skipping tests'
} else {
print 'running tests'
}
}
我不得不使用来自 Jenkins 的 readProperties 管道实用程序步骤插件.
I had to use readProperties from Jenkins' Pipeline Utility Steps Plugin.
这篇关于如何使用配置文件提供程序插件从 Jenkins 管道内的配置文件中读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!