How to read properties file from Jenkins 2.0 pipeline script

前端 未结 4 1766
终归单人心
终归单人心 2020-12-03 10:44

I am attempting to write a pipeline script to use with Jenkins 2.0 to replicate our existing build. This original build used the envInject plugin to read a Java properties

相关标签:
4条回答
  • 2020-12-03 11:10

    I just fought with this yesterday and today. I wish the availability of this was easier to find.

    Grab the 'Pipeline Utility Steps' plugin.

    Use the readProperties step.

     def props = readProperties  file: 'dir/my.properties'
    

    One word of warning - what I expected to be booleans in the properties files were treated as strings.

    0 讨论(0)
  • 2020-12-03 11:17

    i wasn't able to figure out how to interpolate the plain text from readProperties, so i just made a workaround to expand the variable.

    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   
    }
    
    0 讨论(0)
  • 2020-12-03 11:19

    I tried out and below works perfectly fine:

    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}"
    
    0 讨论(0)
  • 2020-12-03 11:30

    Use:

    def props = readProperties file: 'config/general.properties'
    

    In case your properties file located in Groovy Library, and source code located in different place, you should use the Resources folder from the Groovy Library.

    Hence add below line:

    --> def propFileContent = libraryResource 'config/general.properties'
        def props = readProperties text: propFileContent
    

    Notes:

    • "config" is some folder inside 'resources' folder

    • pay attention, in first places used word "file:", in second used "text:"

    0 讨论(0)
提交回复
热议问题