how to access Jenkins environment variables with DSL?
Asked Answered
F

6

13

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

Felspar answered 29/7, 2015 at 17:51 Comment(0)
F
13

Found out the answer: build.environment.get("BUILD_NUMBER")

Felspar answered 29/7, 2015 at 18:20 Comment(3)
No such property: build for class: groovy.lang.BindingAlbinaalbinism
sorry can't help with that, I'm not at the same place and to now work with Jenkins anymoreFelspar
@Albinaalbinism Add the following line before it build = Thread.currentThread().executableLenes
M
8

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

def BUILD_NUMBER = getBinding().getVariables().BUILD_NUMBER
Mediocre answered 15/11, 2018 at 10:56 Comment(2)
Yields "Scripts not permitted to use method groovy.lang.Binding getVariables" on the corporate Jenkins I use (version unknown)Apograph
That's a permission issue. Please check with your Jenkins administrator.Mediocre
E
4

Just to let you all know I tried with

def foo = build.buildVariableResolver.resolve("FOO")
println "FOO=$foo" 

It worked. Refer the example here https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin

Emmenagogue answered 8/6, 2016 at 12:55 Comment(3)
In recent versions you need to do def previousBuild = currentBuild.getPreviousBuild(); def foo = previousBuild.buildVariables["YOUR_VARIABLE_NAME"]Fremantle
@Fremantle ERROR: (script, line 1) No such property: currentBuild for class: scriptWells
Probably an old version of Jenkins / DSL / PipelineFremantle
A
2
echo 'Build No: ' + env.BUILD_NUMBER + '. Build URL: ' + env.BUILD_URL

List of environment variables accessible via: http://HOST/PATH_TO_JENKINS/env-vars.html/

e.g., http://localhost:8888/jenkins/env-vars.html/

Albinaalbinism answered 30/5, 2016 at 15:23 Comment(1)
Only way posted here working for me in pipeline stageApograph
M
2

Jenkins ver. 2.138.2

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

Masticate answered 20/12, 2018 at 11:7 Comment(0)
R
1

Not sure what is referenced as DSL here, but if you refer to the Job DSL plugin, you can just use "${BUILD_NUMBER}"

See https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#access-the-jenkins-environment-variables

Rally answered 13/5, 2024 at 14:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.