简体   繁体   中英

Jenkins Environment Variables in Groovy Init

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!

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) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM