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

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?

How to access the “marked” build status from within a Jenkins build Groovy script

I have seen solutions that show you how to access the current result via manager.build.result. However, I have an NUnit publisher configured that marks the build as unstable or failed. In this case, w

PowerShell script called from within the Jenkins PowerShell build step hangs indefinitely

I have a Jenkins (1.493) project that uses the Jenkins PowerShell build step to execute a PowerShell script. Inside that script I want to invoke another script that is stored inside a file. I have now

Preserve environment variables in Jenkins between Windows build commands

I am trying to set up Jenkins to continually check out and build code and verify that the code is compilable. Our build system works like this: we have several different .bat files that set up environ

How do you access Xcode environment (and build) variables from an external script?

I am writing a script to automate my iOS building. It will be run outside of Xcode, either via Terminal or from build automating software. Is there any way to have access to Xcode environment variable

Jenkins groovy build step trigger another job in groovy script removes original job’s parameters

I’m using a groovy script to trigger other jobs, which is based on the example from the Groovy plugin page. I get a list of jobs as a parameter, validate they exist and trigger them with a few paramet

Jenkins Groovy post-build script to evaluate file with function

I’m using the following code in post-build step of my Jenkins job: evaluate(new File(Set-BuildBadge.groovy)); So it runs a script successfully if it does not contain functions. If inside the script

Incorporate jenkins environment variables into groovy template for email notification

I have several environment variables defined for a project on jenkins, amongst which i want to incorporate some onto the email notification sent before and after build success. But groovy.text.Templat

Accessing Variables Specified for Jenkins Groovy Plugin Script

When writing a script that is run by Jenkins Groovy Plugin as a build step (Execute System Groovy Script) one can specify ‘variable bindings’. The helpline says: Define varibale bingings (in the prope

How can I access Jenkins build artifacts from the groovy Postbuild plugin?

Is there a way to read a build artifact file using the Groovy postbuild plugin? Some context: I have written some testng tests that are run by maven in Jenkins. Jenkins runs the tests, gives me the te

Pass parameter from build step to post-build step in Jenkins

How can I pass a parameter in Jenkins from a build step to post-build step? I don’t want to do it through a file, as it’s just a simple string (version number) that I want to pass. In post-build I use

Answers

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!

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

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

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

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