且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Jenkins Groovy脚本控制台中获取环境变量?

更新时间:2023-12-05 18:07:28



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

我参考了下面的链接,介绍如何 set 全局属性编程。
https://groups.google.com/forum/# !topic / jenkinsci-users / KgCGuDmED1Q

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

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

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.

I've tried for example:

System.getenv("myVar")

and

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

and

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

and

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

You can get global properties like this:

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

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