简体   繁体   English

在 Jenkins 构建步骤 (Windows) 中从 groovy 脚本访问构建环境变量

[英]Access to build environment variables from a groovy script in a Jenkins build step (Windows)

I'm using Scriptler plugin, so I can run a groovy script as a build step.我正在使用 Scriptler 插件,因此我可以将 groovy 脚本作为构建步骤运行。 My Jenkins slaves are running on windows in service mode.我的 Jenkins slaves 以服务模式在 Windows 上运行。 With scriptler, I don't need to use windows batch scripts.使用脚本程序,我不需要使用 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.其中BASE是 jenkins 启动时 env-vars 的一部分。 However, I would like to get但是,我想得到

%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 脚本中获取注入的环境变量呢?

build and listener objects are presenting during system groovy execution . buildlistener对象在系统 groovy 执行期间呈现。 You can do this:你可以这样做:

def myVar = build.getEnvironment(listener).get('myVar')

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")

On jenkins 2.x, with groovy plugin 2.0, running SystemGroovyScript I 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')就足够了

The Scriptler Groovy script doesn't seem to get all the environment variables of the build. Scriptler Groovy 脚本似乎没有获得构建的所有环境变量。 But what you can do is force them in as parameters to the script:但是你可以做的是强制它们作为脚本的参数:

  1. When you add the Scriptler build step into your job, select the option "Define script parameters"当您将 Scriptler 构建步骤添加到您的作业中时,选择“定义脚本参数”选项

  2. Add a parameter for each environment variable you want to pass in. For example "Name: JOB_NAME", "Value: $JOB_NAME".为每个要传入的环境变量添加一个参数。例如“名称:JOB_NAME”、“值:$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.该值将使用 '$envName' 类型变量从 Jenkins 构建环境中扩展,根据我的经验,作业配置设置中的大多数字段都支持这种扩展。

  3. 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" println "JOB_NAME = $JOB_NAME"

I haven't used Sciptler myself apart from some experimentation, but your question posed an interesting problem.除了一些实验之外,我自己还没有使用过 Sciptler,但是您的问题提出了一个有趣的问题。 I hope this helps!我希望这有帮助!

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

Jenkins 2.x has the global variables. Jenkins 2.x 有全局变量。 env is one of them from any script... 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

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.需要注意的一件事是,如果您使用的是自由式作业,除非您使用 System Groovy Script 构建步骤,否则您将无法访问构建参数或 Jenkins JVM 环境。 I spent hours googling and researching before gathering enough clues to figure that out.在收集足够的线索来解决这个问题之前,我花了几个小时的谷歌搜索和研究。

In System Groovy Script (Jenkins 2.89), I was able to use the environmental variable to disable another Jenkins jobSystem Groovy Script (Jenkins 2.89) 中,我能够使用环境变量来禁用另一个 Jenkins 作业

import jenkins.*
import jenkins.model.*
def env = binding.build.environment
Jenkins.instance.getItemByFullName(env.job_name).setDisabled(false) 

I also added a conditional step so as to either enable or disable another Jenkins job.我还添加了一个条件步骤,以便启用或禁用另一个 Jenkins 作业。

在此处输入图片说明

Thanks @Allan Lewis, your comment was helpful.谢谢@Allan Lewis,您的评论很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows上的环境变量中设置Jenkins ant构建中的build.xml文件的路径 - How do I set the path to the build.xml file in a Jenkins ant build from an environment variable on Windows 在预构建事件中设置环境变量并在编译步骤中使用 - Setting environment variables in pre-build event and using in compilation step Jenkins Windows从站有时在第二个“构建”步骤后挂起 - Jenkins Windows slave sometime hangs after second “Build on” step 如何在jenkins构建步骤的Windows10批处理命令中转义%? - How to escape % in windows10 batch command in jenkins build step? 如何将文件中的变量加载到Jenkins管道中的Windows环境变量中? - How to load variables from file into windows environment variables in Jenkins pipeline? 从提升的脚本访问用户环境变量? - Access user environment variables from elevated script? 如何从Windows批处理中保留Jenkins中的环境变量? - How do I persist environment variables in Jenkins from a Windows batch? 为Bamboo构建设置环境变量 - Setting environment variables for a Bamboo build 构建规则中的Bazel环境变量 - Bazel environment variables in build rules 如何在Windows Docker运行状况检查脚本中访问环境变量 - how to access environment variables in Windows Docker healthcheck script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM