Jenkins Environment Variables in Groovy Init

I am building a Jenkins Docker image and passed the ENV variables to the init file jenkins.sh

:

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 ​​are displayed fine, but I would like to use them in Groovy scripts during Jenkins initialization.

Below is the error during startup:

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'])

      

Mistake

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 on 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.AbstractProperty7.

How can I grab the environment variables present in jenkins.sh

?

Thank!

+3


source to share


1 answer


Check env vars with:

def env = System.getenv()
env.each { 
  println it
}

      



Export env vars to jenkins.sh

.

See also Accessing build environment variables from a groovy script during Jenkins build phase (Windows) .

+2


source







All Articles