Pete Singleton
Pete Singleton

Reputation: 485

How to read properties file from Jenkins 2.0 pipeline script

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 file, but I can't see how to do this from the pipeline Groovy script. I have Googled and found the following, but it doesn't work (FileNotFoundException):

Properties props = new Properties()
File propsFile = new File('./Builder/project.properties')
props.load(propsFile.newDataInputStream())

Thanks!

Upvotes: 35

Views: 106006

Answers (5)

baumato
baumato

Reputation: 378

readProperties fails for me with following error:

java.lang.UnsupportedOperationException: Groovy map constructors may only be invoked using the 'new' keyword in the sandbox (attempted to construct class java.util.Properties via a Groovy cast)
        at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onNewInstance(SandboxInterceptor.java:185)

I just put this helper method at the end of the file:

private Properties readProperties(String filePath) {
  String content = readFile filePath
  Properties props = new Properties()
  props.load(new StringReader(content))
  return props
}

and used it in the pipeline:

Properties props = readProperties("configuration.properties")

Upvotes: 0

Mike Kingsbury
Mike Kingsbury

Reputation: 936

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.

Upvotes: 66

Adir Dayan
Adir Dayan

Reputation: 1617

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

Upvotes: 4

Rang
Rang

Reputation: 29

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   
}

Upvotes: -1

Devi Ojha
Devi Ojha

Reputation: 201

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

Upvotes: 20

Related Questions