5

I have to access an environment variable from my Groovy script. I am not using Jenkins. How do I get the variable?

Michael S.
  • 589
  • 8
  • 25
  • 2
    Have you tried https://stackoverflow.com/questions/21236268/access-to-build-environment-variables-from-a-groovy-script-in-a-jenkins-build-st? Have you tried: https://stackoverflow.com/questions/40215394/how-to-get-environment-variable-in-jenkins-groovy-script-console Have you tried: https://stackoverflow.com/questions/38599641/jenkins-pipeline-accessing-environment-variables ... What _have_ you tried? – tim_yates Aug 19 '19 at 13:57

2 Answers2

6

Use java.lang.System#getenv(java.lang.String)

Example:


def envVar = System.getenv('my_env_var')
Peter
  • 4,752
  • 2
  • 20
  • 32
0

A shorter groovy way would be like this:

def envVar = System.env['my_env_var']

Here is an example.

Ibrahim.H
  • 1,062
  • 1
  • 13
  • 22