Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 760
Adam Ocsvari Avatar asked Jan 20 '14 14:01

Adam Ocsvari


People also ask

How do I get environment variables in Groovy Jenkins?

In "Manage Jenkins" -> "Configure System" -> "Global Properties" -> "Environment Variables" I added "ALL_NODES_ENVVAR".

How do you set environment variables in Jenkins pipeline script?

Jenkins Environment Variable is a global variable exposed through the env variable and used anywhere in the Jenkins file. Any value stored in the env variable gets stored as a String type. Environment Variables can be set either at the pipeline top level, at the specific stage level, or inside the script block.

How do I set environment variables in Groovy?

Download a binary distribution of Groovy and unpack it into some folder on your local file system. Set your GROOVY_HOME environment variable to the directory where you unpacked the distribution. Add GROOVY_HOME/bin to your PATH environment variable. Set your JAVA_HOME environment variable to point to your JDK.


1 Answers

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

def myVar = build.getEnvironment(listener).get('myVar') 
like image 169
Kanstantsin Shautsou Avatar answered Oct 02 '22 19:10

Kanstantsin Shautsou