如何从Groovy脚本中访问jenkins构建环境变量 [英] How can I access jenkins build environment variables from within Groovy scripts

查看:646
本文介绍了如何从Groovy脚本中访问jenkins构建环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个可以在Jenkins 1.596.2上成功运行的Jenkins构建,我们正在将其迁移到运行Jenkins 2.60.1的新构建机器上

We have a Jenkins build which runs successfully on Jenkins 1.596.2 which we are migrating to a new build machine running Jenkins 2.60.1

作为构建配置的一部分,我们有一个可扩展选择"参数,并使用系统Groovy选择参数"作为选择提供程序".然后,我们使用Groovy脚本获取选项列表.

As part of the build configuration we have an Extensible Choice parameter and use the System Groovy Choice Parameter as the Choice Provider. We then use a Groovy script to get the list of choices.

此Groovy脚本需要访问在构建配置的全局属性"部分(在环境变量"下)中设置的Jenkins构建环境变量之一.

This Groovy script needs access to one of the Jenkins build environment variables, set in the Global Properties section of the build configuration (under Environment Variables).

在Jenkins的早期版本中,我们能够使用以下代码获取此环境变量值:

Under the earlier version of Jenkins, we were able to get this environment variable value using the following code:

import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.EnvVars

EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class)
EnvVars env = prop.getEnvVars()

def myVariable = env['MY_VAR']

但是,当我们在新的Jenkins版本下运行此代码时,会出现以下错误:

However, when we run this under the new Jenkins build, we get the following error:

groovy.lang.MissingPropertyException: No such property: jenkins for class: Script1

任何人都可以解释一下从Groovy脚本中访问Jenkins构建环境变量所需要做的事情吗?

Can anyone please explain what we need to do to get access to the Jenkins build environment variables from within the Groovy scripts?

我们是否缺少提供对詹金斯"对象/变量的访问的插件或导入?

Are we missing a plugin or import which provides access to the "jenkins" object/variable?

我已经在网上找到了一些答案,但是没有一种解决方案有效.我猜我的Groovy脚本无法访问实现这些解决方案所需的内容.

I've found a few answers to this online, but none of the solutions work. I'm guessing that my Groovy script doesn't have access to something it needs to facilitate these solutions.

顺便说一句,当问题是某些东西受到限制时,我确实转到服务器的进程内脚本批准页面并允许该项目.

By the way, when the problem is that something is restricted, I do go to the in-process script approval page of the server and allow the item.

推荐答案

我有以下代码.

import hudson.model.BuildableItem
import hudson.model.Job
import jenkins.model.*;


jenkins = Jenkins.instance;

此功能在Jenkins的某些较新版本(2.60.3)中使用

This was used in some newer (2.60.3) version of Jenkins

这篇关于如何从Groovy脚本中访问jenkins构建环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆