Loading properties from a file in Jenkins 2.0 pipeline

12,002 views
Skip to first unread message

Ian Cotton

unread,
Sep 7, 2016, 11:35:56 AM9/7/16
to Jenkins Users
Hi all,

I would like to load properties from a Java properties file as environment variables. The snippet generator appears to be able to do just this using the properties step. If I check the box 'Prepare an environment for the run' it offers me a text box to enter a 'Properties File Path'. When I put the path in for my properties file, however, it doesn't generate this as part of the code:
Properties File Path = newbuild/src/gateways/build/scripts/bldcfg.properties

Generated code:
properties [[$class: 'BuildBlockerProperty', blockLevel: <object of type hudson.plugins.buildblocker.BuildBlockerProperty.BlockLevel>, blockingJobs: '', scanQueueFor: <object of type hudson.plugins.buildblocker.BuildBlockerProperty.QueueScanScope>, useBuildBlocker: false], pipelineTriggers([]), [$class: 'ThrottleJobProperty', categories: [], limitOneJobWithMatchingParams: false, maxConcurrentPerNode: 0, maxConcurrentTotal: 0, paramsToUseForLimit: '', throttleEnabled: false, throttleOption: 'project'], <object of type org.jenkinsci.plugins.envinject.EnvInjectJobProperty>]

The last parameter (<object of type org.jenkinsci.plugins.envinject.EnvInjectJobProperty>) appears to be where the file name should appear but the code generator is putting nothing in there. Does anyone have any idea what the correct syntax should be?

I'm using Jenkins 2.20.

Thanks,

Ian.

jer...@bodycad.com

unread,
Sep 7, 2016, 2:07:50 PM9/7/16
to Jenkins Users
Not sure about the Java properties file, but I succeed by doing a .groovy file if that's interest you (probably not, but here it is if someone need it, it's unsafe but in controlled env it doesn't matter):

Jenkinsfile
def my_var = evaluate(new File("myEnv.groovy"));
bat
"\"${my_var.python_2_exe}\" myscript.py"
...

myEnv.groovy
def rv= [:]

rv
['mercurial_url'] = 'http://mercurial.buildserver/'
rv
['python_2_exe'] = "c:\\Python27\\python.exe"
...
return rv;

I put the .groovy file on the build machine jenkins folder, so the Jenkinsfile from the repos can work with vars from any of our build machines or network emplacement. 

Ian Cotton

unread,
Sep 8, 2016, 4:50:01 PM9/8/16
to Jenkins Users
Thanks Jerome. Unfortunately this doesn't help me much. The properties file I need to load already exists and is in use by various build scripts.
I assume I need to create an EnvInjectJobProperty object and set the appropriate property on it. I'll give that a try.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/1a03bb2f-5022-4005-ab10-5e3146aebcd1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jer...@bodycad.com

unread,
Sep 9, 2016, 1:16:12 PM9/9/16
to Jenkins Users
I haven't test it but saw it while checking for something else (zip):

Maybe that's what you're looking for with the readProperties function

Pete Singleton

unread,
Sep 21, 2016, 11:22:30 AM9/21/16
to Jenkins Users
Hi, I'm having exactly the same issue (having only started with Jenkins 2.0 in the last couple of days).  I've tried the Pipeline Utility, tried the EnvInject plugin, also tried opening the file using Groovy file operations, no success!  Did you manage to solve this??

Phillip Campbell

unread,
Sep 21, 2016, 12:12:16 PM9/21/16
to Jenkins
Here's a snippet from a Jenkinsfile in a pre-2.0  pipeline job that I use to read an existing build.properties file in the workspace so that the Jenkinsfile can use the properties.  I haven't tried this in 2.0 pipeline yet.

        if (!fileExists('build.properties')){
            echo 'No build properties.'
	    exit 
        }
        def props = readProperties file: 'build.properties'
	/*
	 * From build.properties file
	 */
        def Var1= props["SOME_PROPERTY"]
        def Var2= props["ANOTHER_PROPERTY"]

To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fcf3de22-08c3-47e2-b996-1d0bd85f28ed%40googlegroups.com.

Pete Singleton

unread,
Sep 22, 2016, 4:11:21 AM9/22/16
to jenkins...@googlegroups.com
ok thanks, that seems to work as far as reading the properties file, but then fails when I try to echo the variable:

 def props = readProperties file: 'project.properties'
 def Var1= props['RELEASE']
 def Var2= props['SOFTWARE.VERSION']
        
echo "Var1=" Var1
echo "Var2=" Var2

java.lang.NullPointerException: Cannot get property 'Var1' on null object

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/OV9h0q_FGfQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAAYHPVij13azLVN%2BwkQfeGG_ExE6eUqGE1A4cL9Dg-zvwtXK0A%40mail.gmail.com.

Phillip Campbell

unread,
Sep 22, 2016, 8:12:56 AM9/22/16
to Jenkins
Syntax error.  Var1 and Var2 are variables.  Try:

echo "Var1=${Var1}"
echo "Var2=${Var2}"


Ian Cotton

unread,
Sep 22, 2016, 9:21:23 AM9/22/16
to Jenkins
The original idea of using the properties build step was just not workable since I couldn't directly instantiate the required objects and couldn't find any kind of factory. I went with loading from file straight into a Properties object as some have suggested and it works fine. I'm using much more of the Groovy language now and not working in the sandbox. Security risk, I know, but it's much more powerful.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/OV9h0q_FGfQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

naresh thota

unread,
Apr 30, 2018, 3:39:24 PM4/30/18
to Jenkins Users
hi, can someone help to resolve the below issue.

i have used the readProperties to read the properties from the file as below.

def props = readProperties file: 'my_dir/env_props.properties'   
    
   def var1 = props["${env}.install"]

here env is the parameter passed to the pipeline job, the value of the "env" is not populated in the above statement.

can someone advise on this, thanks in advance.
Reply all
Reply to author
Forward
0 new messages