如何在 Jenkins Groovy 脚本控制台中获取环境变量? [英] How to get environment variable in Jenkins Groovy script console?

查看:25
本文介绍了如何在 Jenkins Groovy 脚本控制台中获取环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Jenkins 配置 (http://JenkinsURL/configure) 中的全局属性"中,我定义了一些环境变量".

In Jenkins configuration (http://JenkinsURL/configure) within "Global properties" I defined some "Environment variables".

如何在 Groovy 脚本控制台 (http://JenkinsURL/script) 中访问它们?

How can I access them in the Groovy Script console (http://JenkinsURL/script)?

我试图找到合适的解决方案(例如:访问从 Jenkins 构建步骤中的 groovy 脚本构建环境变量 (Windows))但似乎它们都不适合我.

I've tried to find appropriate solution (for example the solutions mentioned in: Access to build environment variables from a groovy script in a Jenkins build step (Windows)) but it seems that none of them work for me.

我试过例如:

System.getenv("myVar")

manager.build.getEnvironment(listener).get('myVar') //no manager error

import jenkins.model.Jenkins
Jenkins.instance.getProperty('myVar') //No signature of method: hudson.model.Hudson.getProperty() is applicable for argument types: (java.lang.String)

import jenkins.model.Jenkins
Jenkins.instance.ParameterValue("DEV_local")

推荐答案

您可以像这样获取全局属性:

You can get global properties like this:

import jenkins.model.Jenkins
def envVars = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars() 
println envVars['myVar']

我参考了下面的链接,了解如何以编程方式设置全局属性.https://groups.google.com/forum/#!topic/jenkinsci-users/KgCGuDmED1Q

I referred to the link below, about how to set global properties programatically. https://groups.google.com/forum/#!topic/jenkinsci-users/KgCGuDmED1Q

这篇关于如何在 Jenkins Groovy 脚本控制台中获取环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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