Accessing Build Environment Variables from Groovy Scripts in Jenkins Build Steps

Nov 24, 2025 · Programming · 8 views · 7.8

Keywords: Jenkins | Groovy Script | Environment Variables | Scriptler Plugin | Windows Build

Abstract: This technical article provides a comprehensive analysis of methods to access build environment variables from Groovy scripts within Jenkins build steps, specifically focusing on Windows environments using the Scriptler plugin. The article examines the limitations of System.getenv() approach and presents a detailed implementation of the parameter passing solution, including complete code examples and configuration steps. Alternative approaches are also discussed to provide readers with a complete understanding of Jenkins environment variable access mechanisms.

Problem Background and Challenges

In Jenkins continuous integration environments, particularly when using the Scriptler plugin to run Groovy scripts on Windows platforms, developers frequently encounter challenges in directly accessing build environment variables. While System.getenv("BASE") successfully retrieves system environment variables, the same approach returns null for Jenkins-injected build environment variables such as JOB_NAME.

Core Solution: Parameter Passing Mechanism

Scriptler Groovy scripts cannot access the complete set of build environment variables by default, but this can be effectively achieved through a parameter passing mechanism. The implementation involves the following steps:

  1. When adding a Scriptler build step in Jenkins job configuration, select the "Define script parameters" option

  2. Add parameters for each required environment variable using the format "Name: variable_name" and "Value: $variable_name"

  3. In the Groovy script, directly use variables with the same names as the parameters to access environment variable values

Code Implementation Example

The following code demonstrates how to access environment variables passed through parameters in a Groovy script:

// Direct access to environment variables passed via parameters
println "JOB_NAME = $JOB_NAME"
println "BUILD_NUMBER = $BUILD_NUMBER"

// Further processing of these variables
def fullJobName = JOB_NAME + "_build_" + BUILD_NUMBER
println "Full job name: $fullJobName"

Parameter Configuration Details

In the parameter configuration interface, proper setup of parameter names and values is essential:

Alternative Approaches Comparison

Besides the parameter passing method, other approaches exist for accessing environment variables:

System Groovy Script Method

For system Groovy scripts, direct access to build and listener objects is available:

def build = this.getProperty('binding').getVariable('build')
def listener = this.getProperty('binding').getVariable('listener')
def env = build.getEnvironment(listener)
println env.get('MY_VARIABLE')

Thread Access Method

Accessing build environment through current thread:

def thr = Thread.currentThread()
def build = thr?.executable
def envVarsMap = build.parent.builds[0].properties.get("envVars")

Best Practices Recommendations

Based on practical application experience, the following best practices are recommended:

Performance and Compatibility Considerations

The parameter passing method demonstrates excellent performance since variable values are determined before build step execution. This approach is compatible with various Jenkins versions and Windows environments, requiring no additional plugin dependencies. In contrast, alternative methods may be limited by Jenkins version and plugin compatibility.

Conclusion

Through the parameter passing mechanism of the Scriptler plugin, effective access to Jenkins build environment variables from Groovy scripts can be achieved. This method is simple, reliable, and suitable for most usage scenarios, making it the preferred solution for environment variable access challenges in Windows environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.