在 Jenkins 构建步骤 (Windows) 中从 groovy 脚本访问构建环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21236268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access to build environment variables from a groovy script in a Jenkins build step (Windows)
提问by Adam Ocsvari
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.
我正在使用 Scriptler 插件,因此我可以将 groovy 脚本作为构建步骤运行。我的 Jenkins slaves 以服务模式在 Windows 上运行。使用脚本程序,我不需要使用 Windows 批处理脚本。
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
哪里BASE
是将env-瓦尔对詹金斯启动部分。但是,我想得到
%JOB_NAME%
If I'm adding an "Execute Windows batch command" build step:
如果我要添加“执行 Windows 批处理命令”构建步骤:
echo %JOB_NAME%
It works. If I'm adding a scriptler script as a build step with the same settings:
有用。如果我添加一个 scriptler 脚本作为具有相同设置的构建步骤:
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?
那么作为构建步骤,如何从 groovy 脚本中获取注入的环境变量呢?
采纳答案by macg33zr
The Scriptler Groovy script doesn't seem to get all the environment variables of the build. But what you can do is force them in as parameters to the script:
Scriptler Groovy 脚本似乎没有获得构建的所有环境变量。但是你可以做的是强制它们作为脚本的参数:
When you add the Scriptler build step into your job, select the option "Define script parameters"
Add a parameter for each environment variable you want to pass in. For example "Name: JOB_NAME", "Value: $JOB_NAME". The value will get expanded from the Jenkins build environment using '$envName' type variables, most fields in the job configuration settings support this sort of expansion from my experience.
In your script, you should have a variable with the same name as the parameter, so you can access the parameters with something like:
println "JOB_NAME = $JOB_NAME"
当您将 Scriptler 构建步骤添加到您的作业中时,选择“定义脚本参数”选项
为每个要传入的环境变量添加一个参数。例如“名称:JOB_NAME”、“值:$JOB_NAME”。该值将使用 '$envName' 类型变量从 Jenkins 构建环境中扩展,根据我的经验,作业配置设置中的大多数字段都支持这种扩展。
在您的脚本中,您应该有一个与参数同名的变量,以便您可以使用以下内容访问参数:
println "JOB_NAME = $JOB_NAME"
I haven't used Sciptler myself apart from some experimentation, but your question posed an interesting problem. I hope this helps!
除了一些实验之外,我自己还没有使用过 Sciptler,但是您的问题提出了一个有趣的问题。我希望这有帮助!
回答by Kanstantsin Shautsou
build
and listener
objects are presenting during system groovy execution. You can do this:
build
和listener
对象在系统常规执行期间呈现。你可以这样做:
def myVar = build.getEnvironment(listener).get('myVar')
回答by Alf
You might be able to get them like this:
你也许可以像这样得到它们:
def thr = Thread.currentThread()
def build = thr?.executable
def envVarsMap = build.parent.builds[0].properties.get("envVars")
回答by Augustin Ghauratto
On jenkins 2.x, with groovy plugin 2.0, running SystemGroovyScriptI managed to get to build variables, as below:
在 jenkins 2.x 上,使用 groovy 插件 2.0,运行SystemGroovyScript我设法构建变量,如下所示:
def build = this.getProperty('binding').getVariable('build')
def listener = this.getProperty('binding').getVariable('listener')
def env = build.getEnvironment(listener)
println env.MY_VARIABLE
If you are using goovy from file, simple System.getenv('MY_VARIABLE')
is sufficient
如果您使用文件中的 goovy,简单System.getenv('MY_VARIABLE')
就足够了
回答by dayer4b
The only way I could get this to work (on Linux) was to follow this advice:
我可以让它工作(在 Linux 上)的唯一方法是遵循以下建议:
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script
import hudson.model.*
// get current thread / Executor and current build
def thr = Thread.currentThread()
def build = thr?.executable
// if you want the parameter by name ...
def hardcoded_param = "FOOBAR"
def resolver = build.buildVariableResolver
def hardcoded_param_value = resolver.resolve(hardcoded_param)
println "param ${hardcoded_param} value : ${hardcoded_param_value}"
This is on Jenkins 1.624 running on CentOS 6.7
这是在 CentOS 6.7 上运行的 Jenkins 1.624
回答by John Czukkermann
One thing to note, if you are using a freestyle job, you won't be able to access build parameters or the Jenkins JVM's environment UNLESS you are using System Groovy Script build steps. I spent hours googling and researching before gathering enough clues to figure that out.
需要注意的一件事是,如果您使用的是自由式作业,除非您使用 System Groovy Script 构建步骤,否则您将无法访问构建参数或 Jenkins JVM 环境。在收集足够的线索来解决这个问题之前,我花了几个小时的谷歌搜索和研究。
回答by Marcello de Sales
Jenkins 2.x has the global variables. env
is one of them from any script...
Jenkins 2.x 有全局变量。env
是任何脚本中的其中之一...
println env.JOB_NAME
More at https://build.intuit.com/services-config/pipeline-syntax/globals#env
更多信息请访问https://build.intuit.com/services-config/pipeline-syntax/globals#env