accessing Jenkins environment

6,041 views
Skip to first unread message

Jeremy Marshall

unread,
May 22, 2013, 10:33:41 PM5/22/13
to job-dsl...@googlegroups.com
Hi

I was wondering if there is a way of accessing the Jenkins environment such as BUILD_NUMBER from with dsl?

Ultimately I want to update the envInject object of the downstream job with the build number from the this job. I could save it to a file in a previous build step and read it back but that seems a bit involved

Thanks
Jeremy

Jeremy Marshall

unread,
May 23, 2013, 12:31:31 AM5/23/13
to job-dsl...@googlegroups.com

Solved my own problem (which is nice)

//binding.variables.each{ println it.key println it.value }


println
(" BUILD_NUMBER = " + binding.variables.BUILD_NUMBER)

I was thinking there would be the variables from the outer scope available but it is a bit more complex than that


with some help once I knew what to google

Justin Ryan

unread,
May 23, 2013, 1:59:50 AM5/23/13
to job-dsl-plugin
Did you try just println " BUILD_NUMBER = ${BUILD_NUMBER}"? That works for me just fine. As you noticed we bind all the build.getEnvironment variables directly into the binding, so that they're available without any other indirection, so you shouldn't need the binding.variables.BUILD_NUMBER. I can see there being a problem deeper down into a method or something. But in that case you can pass it in as a variable. Here's a sample of the variables available:

 BUILD_CAUSE=USERIDCAUSE
 BUILD_CAUSE_USERIDCAUSE=true
 BUILD_ID=2013-05-22_22-53-55
 BUILD_NUMBER=3
 BUILD_TAG=jenkins-job-dsl-env-3
 EXECUTOR_NUMBER=0
 HOME=/Users/jryan
 HUDSON_HOME=/Users/jryan/Projects/Roundup/jenkins-job-dsl/work
 HUDSON_SERVER_COOKIE=e09a20865eb98095e7f486932d021983
 JENKINS_HOME=/Users/jryan/Projects/Roundup/jenkins-job-dsl/work
 JENKINS_SERVER_COOKIE=e09a20865eb98095e7f486932d021983
 JOB_NAME=job-dsl-env
 LANG=en_US.UTF-8
 LOGNAME=jryan
 NODE_LABELS=master
 NODE_NAME=
 OLDPWD=/Users/jryan/Projects/Roundup/jenkins-job-dsl
 PWD=/Users/jryan/Projects/Roundup/jenkins-job-dsl
 SHELL=/bin/bash
 TERM=xterm
 TMPDIR=/var/folders/2q/nkf8g47s4k50hbn2q633q0ww_yp9s1/T/
 USER=jryan

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/00c72446-98a3-4dfd-8d3b-8b40ef11166c%40googlegroups.com?hl=en.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jeremy Marshall

unread,
May 23, 2013, 2:41:32 AM5/23/13
to job-dsl...@googlegroups.com


Hi

yes you're right. I tried several ways but not the right one.

One is in the jenkins environment and the other one I put in with envInject.
The top one works and the two commented out ones fail

Thanks 
Jeremy


println "BUILD_NUMBER = ${BUILD_NUMBER}"

println
"BRANCH_NAME = ${BRANCH_NAME}"


//println "BUILD_NUMBER = " + ${BUILD_NUMBER}
//println "BRANCH_NAME = " + ${BRANCH_NAME}


//println "BUILD_NUMBER = " + $BUILD_NUMBER
//println "BRANCH_NAME = " + $BRANCH_NAME



Justin Ryan

unread,
May 23, 2013, 4:37:42 PM5/23/13
to job-dsl-plugin
BTW, those last two examples aren't valid Groovy. The ${} syntax only exists in gstrings. If you remove the "${" and "}" around them, they would work.


--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.

Andrew Harmel-Law

unread,
May 28, 2013, 7:02:52 AM5/28/13
to job-dsl...@googlegroups.com
Thanks for the question / answer Jeremy,

I'll put this info on the wiki so others can benefit from it without a trawl through the newsgroup.

Cheers, Andrew

Andrew Harmel-Law

unread,
May 28, 2013, 7:13:44 AM5/28/13
to job-dsl...@googlegroups.com

Parag Doke

unread,
Oct 29, 2014, 9:47:20 AM10/29/14
to job-dsl...@googlegroups.com
Hi Andrew.
The method Jeremy shared (binding.variables.something) works if the DSL job itself has parameters, and someone (like me) wants to access the param values.
Do you think it is a good idea to document that also on the User-Power-Moves Wiki page ? It only saves the user from digging through this forum posts :-).

Please do let me know if this sounds okay. I want to attempt forking this on github and submitting a merge request.
Thanks in advance !
-Parag Doke

Andrew Harmel-Law

unread,
Oct 30, 2014, 1:48:40 AM10/30/14
to job-dsl...@googlegroups.com

Hi Parag. Sounds great. Go for it and submit the PR. Thanks very much for your input.

Kind regards, Andrew

Daniel Spilker

unread,
Nov 5, 2014, 8:47:15 AM11/5/14
to job-dsl...@googlegroups.com
Hi!

It's not necessary to use binding.variables. Build parameters are exposed as environment variables in Jenkins and environment variables are available as variables in DSL scripts, see https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#access-the-jenkins-environment-variables.

I added a hint to the docs in c309d86.

Daniel

Александр Артёмов

unread,
May 15, 2015, 11:31:40 AM5/15/15
to job-dsl...@googlegroups.com
Hi!

It would be also good to have acces to the JOB_URL variable. My script needs to understand on which Jenkins instance it is run and I can't see any way to find it out without this variable.

среда, 5 ноября 2014 г., 14:47:15 UTC+1 пользователь Daniel Spilker написал:

Daniel Spilker

unread,
May 19, 2015, 9:13:43 AM5/19/15
to job-dsl...@googlegroups.com

Mathias De Maré

unread,
Mar 7, 2016, 10:36:12 AM3/7/16
to job-dsl-plugin


On Tuesday, May 19, 2015 at 3:13:43 PM UTC+2, Daniel Spilker wrote:
All build environment variable are available in the script, including JOB_URL. See
https://github.com/jenkinsci/job-dsl-plugin/wiki/User-Power-Moves#access-the-jenkins-environment-variables
I can get this to work in a top-level Groovy file.
However, if I use "${BUILD_URL}" in a static class function in an imported Groovy file, this does not work.

So for example:

top.groovy:
import static Sub.somemethod
println("${BUILD_URL}") //this works

sub.groovy:
class Sub {
    static def somemethod() {
        println("${BUILD_URL}") //this gives an error
    }
}

The error I get:
Apparent variable 'BUILD_URL' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'BUILD_URL' but left out brackets in a place not allowed by the grammar.

Is there any way to  resolve this? I'm using imports in multiple places, so I can't always pass along the variable from the top-level script to fix this problem.

Dan Langer

unread,
Sep 2, 2016, 9:31:17 PM9/2/16
to job-dsl-plugin
Any luck finding a solution for this?

Dan

Daniel Spilker

unread,
Sep 13, 2016, 5:13:46 AM9/13/16
to job-dsl...@googlegroups.com
The environment variables like BUILD_URL are only available in the script, but not in any classes. And you can't use println in classes, see https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands#logging for details.

In the script, the variables are stored in the binding (see http://docs.groovy-lang.org/latest/html/api/groovy/lang/Script.html#getBinding()). You can use the binding to pass the variables to any classes:

script.groovy:
Sub.someMethod(binding.variables, out)

sub.groovy:
class Sub {
    static someMethod(def variables, def out) {
        out.println(variables.BUILD_URL)
    }
}


Daniel

--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugin+unsubscribe@googlegroups.com.
To post to this group, send email to job-dsl-plugin@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/757281ef-6449-40c4-b1e4-99cb11263fd6%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages