如何使用配置文件提供程序插件从Jenkins管道中的配置文件读取属性 [英] How to read property from config file inside Jenkins pipeline using Config File Provider Plugin

查看:691
本文介绍了如何使用配置文件提供程序插件从Jenkins管道中的配置文件读取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个简单的属性配置文件对我的Jenkins管道进行参数化

I want to parametrize my Jenkins pipeline with a simple properties config file

skip_tests=true

在我的管道中,我正在导入此文件,并尝试使用Jenkins Pipeline Config File Plugin读取该文件。

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的 Pipeline实用程序步骤插件

I had to use readProperties from Jenkins' Pipeline Utility Steps Plugin.

这篇关于如何使用配置文件提供程序插件从Jenkins管道中的配置文件读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆