Reputation: 1047
I am building a Docker image of Jenkins, and have passed ENV variables to the jenkins.sh
initialization file:
Dockerfile
...
COPY ./jenkins.sh /usr/local/bin/jenkins.sh
jenkins.sh
echo ENV: "$ENV"
echo CLUSTER: "$CLUSTER"
echo REGION: "$REGION"
When I run the image, these values print out perfectly, but I would like to use them in Groovy scripts during the initialization of Jenkins.
The following throws an error during start:
import java.util.Arrays
import java.util.logging.Logger
Logger logger = Logger.getLogger("ecs-cluster")
logger.info("Loading Archeus-Midwayer...")
import jenkins.model.*
instance = Jenkins.getInstance()
def env = System.getenv()
println(env['CLUSTER'])
Error
WARNING: Failed to run script file:/var/jenkins_home/init.groovy.d/init_ecs.groovy groovy.lang.MissingPropertyException: No such property: CLUSTER for class: init_ecs at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53) at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:52) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:307)
How can I capture the environment variables present in jenkins.sh
?
Thanks!
Upvotes: 6
Views: 8324
Reputation: 14762
Check the env vars with:
def env = System.getenv()
env.each {
println it
}
Export the env vars in jenkins.sh
.
See also Access to build environment variables from a groovy script in a Jenkins build step ( Windows).
Upvotes: 6