Jenkins Pipeline : How can I pass a map obtained from readProperties to withEnv ?

2,365 views
Skip to first unread message

JL 6BerYeti

unread,
Nov 30, 2016, 8:26:07 AM11/30/16
to Jenkins Users
Dear All,

I am trying to setup (many) environment variables for a set of stages within a pipeline by reading a property file and then passing the result to withEnv.
As far as my understanding is correct, I thought that withEnv waits for a map, and readProperties returrns a map.
So, I try this :
def localProps=[:]
localProps=["moduleName=Communicator","versionNbr=1.0"]
    def pipeProps=readProperties([
defaults : localProps,
file : "Pipeline/lib/tutoFile.properties"
])
echo "${pipeProps}"
withEnv(pipeProps) {
            ... pipeline stages
        }

I am sure that the file is read within pipePros.
But the withEnv step fails with the following error :
java.lang.UnsupportedOperationException: must specify $class with an implementation of interface java.util.List at org.jenkinsci.plugins.structs.describable.DescribableModel.resolveClass(DescribableModel.java:442) at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:371) at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:313) at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:257) at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:194) at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:181) at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:126) at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
I don't understand what is wrong in the code (as you can imagine, I am not a groovy guru...), and I'd like to know how to get out of there. Thanks for your help. J-L

thomo5000

unread,
Jul 12, 2017, 3:19:56 AM7/12/17
to jenkins...@googlegroups.com
This question is a top search result on Google so I figure it's worth a late
reply.

readProperties returns a map and withEnv expects a list so you'll need to
convert it. Try something like:


> def map = readProperties file:'env.properties'
> def properties = map.collect { key, value -> return key+'='+value }
> withEnv(properties)





--
View this message in context: http://jenkins-ci.361315.n4.nabble.com/Jenkins-Pipeline-How-can-I-pass-a-map-obtained-from-readProperties-to-withEnv-tp4861746p4900401.html
Sent from the Jenkins users mailing list archive at Nabble.com.
Reply all
Reply to author
Forward
0 new messages