-
Improvement
-
Resolution: Unresolved
-
Minor
-
None
-
Jenkins 2.375.3, PostBuildScript Plugin Version 3.2.0-460.va_fda_0fa_26720
It would be useful to be able to access the environment variables in a Groovy script.
Thanks to JENKINS-24308, this is partly possible using a deprecated method:
def envVars = build.getEnvVars() // Deprecated
envVars.each {k,v -> println "$k = $v"}
This works for now, but getEnvVars() is deprecated.
The suggested replacement is to use getEnvironment(TaskListener).
I've tried a couple of ways, but both fail:
- Using a LogTaskListener
{{import java.util.logging.Level}}
{{import java.util.logging.Logger}}
{{import hudson.util.LogTaskListener}}{{def envVars = build.getEnvironment(new LogTaskListener(Logger.getLogger("script"), Level.INFO))}}
{{envVars.each {k,v -> println "$k = $v"}}}
This fails with this exception:
[PostBuildScript] - [INFO] Executing post build scripts.
[PostBuildScript] - [INFO] An error occured during post-build processing.
{{groovy.lang.MissingMethodException: No signature of method: hudson.maven.MavenModuleSetBuild.getEnvironment() is applicable for argument types: (hudson.util.LogTaskListener) values: [hudson.util.LogTaskListener@8a58d4]}}
Possible solutions: getEnvironment(), getEnvironments(), getEnvironment(hudson.model.TaskListener), getEnvironment(hudson.model.TaskListener), getEnvironment(hudson.model.TaskListener), getEnvironment(hudson.model.TaskListener)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:71)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at Script1.run(Script1.groovy:5)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:574)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:612)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:583)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:450)
at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptExecutor.execute(GroovyScriptExecutor.java:49)
at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptPreparer.evaluateScript(GroovyScriptPreparer.java:53)
at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptPreparer.evaluateScript(GroovyScriptPreparer.java:38)
at org.jenkinsci.plugins.postbuildscript.processor.Processor.processGroovyScripts(Processor.java:152)
at org.jenkinsci.plugins.postbuildscript.processor.Processor.processScripts(Processor.java:90)
- Using a StreamBuildListener
import hudson.model.StreamBuildListener
import java.nio.charset.StandardCharsetsdef taskListener = new hudson.model.StreamBuildListener(OutputStream.nullOutputStream(), java.nio.charset.StandardCharsets.UTF_8)
{{def envVars = build.getEnvironment(taskListener)}}
envVars.each {k,v -> println "$k = $v"}
This fails with this exception:
[PostBuildScript] - [INFO] Executing post build scripts.
[PostBuildScript] - [INFO] An error occured during post-build processing.
{{groovy.lang.MissingMethodException: No signature of method: hudson.maven.MavenModuleSetBuild.getEnvironment() is applicable for argument types: (hudson.model.StreamBuildListener) values: [hudson.model.StreamBuildListener@7cb4e1f8]}}
Possible solutions: getEnvironment(), getEnvironments(), getEnvironment(hudson.model.TaskListener), getEnvironment(hudson.model.TaskListener), getEnvironment(hudson.model.TaskListener), getEnvironment(hudson.model.TaskListener)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:71)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:48)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at Script1.run(Script1.groovy:5)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:574)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:612)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:583)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.evaluate(SecureGroovyScript.java:450)
at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptExecutor.execute(GroovyScriptExecutor.java:49)
at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptPreparer.evaluateScript(GroovyScriptPreparer.java:53)
at org.jenkinsci.plugins.postbuildscript.service.GroovyScriptPreparer.evaluateScript(GroovyScriptPreparer.java:38)
at org.jenkinsci.plugins.postbuildscript.processor.Processor.processGroovyScripts(Processor.java:152)
at org.jenkinsci.plugins.postbuildscript.processor.Processor.processScripts(Processor.java:90)
It might be that different class loaders are used and that it would cause a signature mismatch for hudson.model.TaskListener
A possible solution might be to add this to GroovyScriptExecutor.execute():
binding.setVariable("envVars", build.getEnvironment(log.getListener()));