Access to build environment variables from a groovy script in a Jenkins build step (Windows)

WindowsGroovyJenkinsEnvironment Variables

Windows Problem Overview


I'm using Scriptler plugin, so I can run a groovy script as a build step. My Jenkins slaves are running on windows in service mode. With scriptler, I don't need to use windows batch scripts.

But I have trouble to get the environment variables in a build step... This is working:

System.getenv("BASE")

Where BASE is part of the env-vars on jenkins startup. However, I would like to get

%JOB_NAME%

If I'm adding an "Execute Windows batch command" build step:

echo %JOB_NAME%

It works. If I'm adding a scriptler script as a build step with the same settings:

println "JOB_NAME: " + System.getenv("JOB_NAME")

I'm getting:

JOB_NAME: null

So how can I reach the injected environment variables from a groovy script as a build step?

Windows Solutions


Solution 1 - Windows

build and listener objects are presenting during system groovy execution. You can do this:

def myVar = build.getEnvironment(listener).get('myVar')

Solution 2 - Windows

You might be able to get them like this:

def thr = Thread.currentThread()
def build = thr?.executable
def envVarsMap = build.parent.builds[0].properties.get("envVars")

Solution 3 - Windows

On jenkins 2.x, with groovy plugin 2.0, running SystemGroovyScript I managed to get to build variables, as below:

def build = this.getProperty('binding').getVariable('build')
def listener = this.getProperty('binding').getVariable('listener')
def env = build.getEnvironment(listener)
println env.MY_VARIABLE

If you are using goovy from file, simple System.getenv('MY_VARIABLE') is sufficient

Solution 4 - Windows

The Scriptler Groovy script doesn't seem to get all the environment variables of the build. But what you can do is force them in as parameters to the script:

  1. When you add the Scriptler build step into your job, select the option "Define script parameters"

  2. Add a parameter for each environment variable you want to pass in. For example "Name: JOB_NAME", "Value: $JOB_NAME". The value will get expanded from the Jenkins build environment using '$envName' type variables, most fields in the job configuration settings support this sort of expansion from my experience.

  3. In your script, you should have a variable with the same name as the parameter, so you can access the parameters with something like:

    println "JOB_NAME = $JOB_NAME"

I haven't used Sciptler myself apart from some experimentation, but your question posed an interesting problem. I hope this helps!

Solution 5 - Windows

The only way I could get this to work (on Linux) was to follow this advice:

https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script

import hudson.model.*

// get current thread / Executor and current build
def thr = Thread.currentThread()
def build = thr?.executable

// if you want the parameter by name ...
def hardcoded_param = "FOOBAR"
def resolver = build.buildVariableResolver
def hardcoded_param_value = resolver.resolve(hardcoded_param)

println "param ${hardcoded_param} value : ${hardcoded_param_value}"

This is on Jenkins 1.624 running on CentOS 6.7

Solution 6 - Windows

Jenkins 2.x has the global variables. env is one of them from any script...

println env.JOB_NAME

More at https://build.intuit.com/services-config/pipeline-syntax/globals#env

Solution 7 - Windows

One thing to note, if you are using a freestyle job, you won't be able to access build parameters or the Jenkins JVM's environment UNLESS you are using System Groovy Script build steps. I spent hours googling and researching before gathering enough clues to figure that out.

Solution 8 - Windows

In System Groovy Script (Jenkins 2.89), I was able to use the environmental variable to disable another Jenkins job

import jenkins.*
import jenkins.model.*
def env = binding.build.environment
Jenkins.instance.getItemByFullName(env.job_name).setDisabled(false) 

I also added a conditional step so as to either enable or disable another Jenkins job.

enter image description here

Thanks @Allan Lewis, your comment was helpful.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam OcsvariView Question on Stackoverflow
Solution 1 - WindowsKanstantsin ShautsouView Answer on Stackoverflow
Solution 2 - WindowsAlfView Answer on Stackoverflow
Solution 3 - WindowsAugustin GhaurattoView Answer on Stackoverflow
Solution 4 - Windowsmacg33zrView Answer on Stackoverflow
Solution 5 - Windowsdayer4bView Answer on Stackoverflow
Solution 6 - WindowsMarcello de SalesView Answer on Stackoverflow
Solution 7 - WindowsJohn CzukkermannView Answer on Stackoverflow
Solution 8 - WindowsAshwaqView Answer on Stackoverflow