如何使用Config File Provider Plugin从Jenkins管道中的配置文件中读取属性

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

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

skip_tests=true

我已经添加到Jenkins配置文件管理:

config file management screenshot

在我的管道中,我正在导入此文件并尝试使用Jenkins Pipeline配置文件插件从中读取。

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

我在这里做错了什么想法?

jenkins jenkins-pipeline jenkins-plugins configuration-files
3个回答
1
投票

由于文件是属性格式,您可以在shell步骤中使用它:

sh """
  source ${MY_CONFIG}
  .
  .
  .
"""

您需要导出需要在shell调用的程序上可用的属性(例如Maven)


1
投票

你错误地使用了Groovy GString,你应该在$skip_tests中包装"或直接使用skip_tests

configFileProvider([configFile(fileId: "$MY_CONFIG", variable: 'skip_tests')]) {
  echo skip_tests
  assert skip_tests == 'true'

  echo "$skip_tests"
  assert "$skip_tests" == 'true'
}

注意:skip_tests的值是配置文件的文件路径,该文件路径从master复制到作业的工作空间。这不是配置文件的内容。


0
投票

在其他答案和How to read properties file from Jenkins 2.0 pipeline script的帮助下,我找到了以下代码:

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 Utility Steps Plugin的readProperties。

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