Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access Jenkins environment variables with DSL?

Tags:

jenkins

groovy

As the question says. I can't access it.

Tries:

#1:

def env = System.getenv()
def BUILD_NUMBER= env["BUILD_NUMBER"]

Result:

Null when I access BUILD_NUMBER

#2:

def BUILD_NUMBER= params["BUILD_NUMBER"]

Result:

Null when I access BUILD_NUMBER This solution however works for environment variables I create

like image 562
Cher Avatar asked Jul 29 '15 17:07

Cher


People also ask

How do I view environment variables in Jenkins pipeline?

Listing Environment Variables Every Jenkins installation has this page called env-vars. html; if you open it in your Jenkins installation, you will see this list of environment variables with some short explanation. You can find variables like BUILD_ID, JOB_NAME, JOB_BASE_NAME, BUILD_URL, JENKINS_URL, and a few others.

How do you pass an environment variable in Jenkins pipeline?

Like if I need to inject a value within Steps/Script section, in Jenkins pipeline, I can define globally in the environment variables or using Jenkins project Configure General Check mark Prepare an environment for the run Check mark Keep Jenkins environment variables I can provide the environment variable in the ...


2 Answers

This will work with recent versions of Jenkins and DSL plugin:

def BUILD_NUMBER = getBinding().getVariables()['BUILD_NUMBER']
like image 169
Alex O Avatar answered Sep 18 '22 10:09

Alex O


Jenkins ver. 2.138.2

def gitUrl = binding.variables.get("GITLAB_URL")

like image 45
radzserg Avatar answered Sep 19 '22 10:09

radzserg