How to read properties file from Jenkins 2.0 pipeline script

…衆ロ難τιáo~ 提交于 2019-11-28 12:01:58
Mike Kingsbury

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.

Devi Ojha

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}"

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   
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!