Accessing Environment variables when processing DSL from a pipeline job

4,994 views
Skip to first unread message

derrick....@imsar.com

unread,
Mar 22, 2017, 1:05:18 PM3/22/17
to job-dsl-plugin


Up until now we have been using a freestyle job as our seed job, using a combination of Job Parameters and EnvInject to setup variables to customize the created jobs.

Now we are trying to setup a multi-branch pipeline job to generate tests for all our branches.  However, we are running into a problem accessing the environment variables from our DSL scripts.  Here is an example of a trivial failing script:

node('master') {
 stage
('DSL') {
    env
.FOO = 'BAZ'
    withEnv
(['FOO=BAR']) {
      jobDsl scriptText
:'println "${FOO}"'
   
}
 
}
}

This fails with:

[Pipeline] node
Running on master in /local_home/jenkins/jobs/QuickTests/jobs/dsl_pipe_test/workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (DSL)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] step
Processing provided DSL script
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: (script, line 1) No such property: FOO for class: script
Possible solutions: jm
Finished: FAILURE

How can I get access to environment variables inside the DSL scripts?

Sai Kishore

unread,
Mar 22, 2017, 1:28:41 PM3/22/17
to job-dsl...@googlegroups.com
Hi,

You can access environment varaible as shown below.

System
.getenv("VariableName")


Thanks
Sai Kishore

--
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/cf3931d3-e601-41fb-a9b6-ade42a80822e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

derrick....@imsar.com

unread,
Mar 22, 2017, 2:33:10 PM3/22/17
to job-dsl-plugin

Unfortunately, that only seems to pick up variables that were defined before the Jenkins server started:
New script:
node('master') {
 stage
('DSL') {
    env
.FOO = 'BAZ'
    withEnv
(['FOO=BAR']) {

      sh
'env'
      jobDsl scriptText
:'System.getenv().collect{println it.key println it.value}'
   
}
 
}
}

As you can see sh('env') prints many more variables that System.getenv() has access to.  Specifically, these variables are not visible:
BUILD_DISPLAY_NAME
BUILD_ID
BUILD_NUMBER
BUILD_TAG
BUILD_URL
EXECUTOR_NUMBER
FOO
HUDSON_COOKIE
HUDSON_HOME
HUDSON_SERVER_COOKIE
HUDSON_URL
JENKINS_HOME
JENKINS_URL
JOB_BASE_NAME
JOB_DISPLAY_URL
JOB_NAME
JOB_URL
NODE_LABELS
NODE_NAME
RUN_CHANGES_DISPLAY_URL
RUN_DISPLAY_URL
WORKSPACE


New output:

[Pipeline] node
Running on master in /local_home/jenkins/jobs/QuickTests/jobs/dsl_pipe_test/workspace
[Pipeline] {
[Pipeline] stage
[Pipeline] { (DSL)
[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh
[workspace] Running shell script
+ env
BUILD_URL=http://jenkins/job/QuickTests/job/dsl_pipe_test/35/
HOSTNAME=optimus
SHELL=/bin/bash
TERM=xterm-color
HUDSON_SERVER_COOKIE=8e4b39efddb19ad4
HISTSIZE=1000
FOO=BAR
BUILD_TAG=jenkins-QuickTests-dsl_pipe_test-35
QTDIR=/usr/lib64/qt-3.3
QTINC=/usr/lib64/qt-3.3/include
JOB_URL=http://jenkins/job/QuickTests/job/dsl_pipe_test/
ANT_HOME=/opt/ant/latest
WORKSPACE=/local_home/jenkins/jobs/QuickTests/jobs/dsl_pipe_test/workspace
RUN_CHANGES_DISPLAY_URL=http://jenkins/job/QuickTests/job/dsl_pipe_test/35/display/redirect?page=changes
USER=jenkins
JENKINS_HOME=/local_home/jenkins
NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat
PATH=/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/ant/latest/bin:/local_home/jenkins/bin
MAIL=/var/spool/mail/jenkins
RUN_DISPLAY_URL=http://jenkins/job/QuickTests/job/dsl_pipe_test/35/display/redirect
_=/bin/env
PWD=/local_home/jenkins/jobs/QuickTests/jobs/dsl_pipe_test/workspace
HUDSON_URL=http://jenkins/
KDE_IS_PRELINKED=1
LANG=en_US.UTF-8
JOB_NAME=QuickTests/dsl_pipe_test
KDEDIRS=/usr
BUILD_DISPLAY_NAME=#35
XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt
JENKINS_URL=http://jenkins/
CATALINA_OPTS=-DJENKINS_HOME=/local_home/jenkins -Xmx512m
BUILD_ID=35
HISTCONTROL=ignoredups
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
JOB_BASE_NAME=dsl_pipe_test
SHLVL=4
HOME=/local_home/jenkins
EXECUTOR_NUMBER=0
NODE_LABELS=fpga linux master
LOGNAME=jenkins
CVS_RSH=ssh
QTLIB=/usr/lib64/qt-3.3/lib
HUDSON_HOME=/local_home/jenkins
NODE_NAME=master
LESSOPEN=|/usr/bin/lesspipe.sh %s
JOB_DISPLAY_URL=http://jenkins/job/QuickTests/job/dsl_pipe_test/display/redirect
BUILD_NUMBER=35
HUDSON_COOKIE=4d56c603-79a3-44b5-a423-7108103ca503
G_BROKEN_FILENAMES=1

[Pipeline] step
Processing provided DSL script
PATH
HISTCONTROL
HISTSIZE
TERM
XFILESEARCHPATH
LANG
G_BROKEN_FILENAMES
ANT_HOME
MAIL
LOGNAME
PWD
_
LESSOPEN
SHELL
QTINC
CVS_RSH
CATALINA_OPTS
USER
SSH_ASKPASS
HOSTNAME
QTDIR
NLSPATH
KDEDIRS
QTLIB
HOME
SHLVL
KDE_IS_PRELINKED

[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Completed QuickTests » dsl_pipe_test #35 : SUCCESS
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.

Daniel Spilker

unread,
Mar 29, 2017, 12:26:03 PM3/29/17
to job-dsl...@googlegroups.com
Pipeline environment variables currently do not work with Job DSL, see https://issues.jenkins-ci.org/browse/JENKINS-40102

Daniel

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/09bdea6a-a655-43b7-b297-256b065c0012%40googlegroups.com.

Derrick Gibelyou

unread,
Mar 30, 2017, 6:28:53 PM3/30/17
to job-dsl...@googlegroups.com
Thanks for the link.  I was searching for bugs related to this, and was not able to find this one.

Are there any workarounds?  In one case all of my jobs are generated by a single script, so I as able to wrap it in a function and do this:
      jobDsl additionalClasspath: 'firmware/common/sim/jenkins/',
             scriptText: "import SimulationTests\n SimulationTests.createAllTests(this,${pwd()})"

But in another case I have about 20 dsl files that I use <targets: "**/hw_tests/*.dsl"> to parse.  Ideally I wouldn't have to refactor all of those files.


--Derrick Gibelyou

I work a 4-10s schedule and my off day is Friday.


--
You received this message because you are subscribed to a topic in the Google Groups "job-dsl-plugin" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/job-dsl-plugin/l19V5sC3Bp4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to job-dsl-plugin+unsubscribe@googlegroups.com.

To post to this group, send email to job-dsl-plugin@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages