get current customized environment
println currentBuild.getBuildVariables()?.MY_ENV
get downstream build environment
def res = build job: 'downstream-job', propagate: false
println res.getBuildVariables()?.MY_ENV
get previous build environment
println currentBuild.getPreviousBuild().getBuildVariables()?.MY_ENV
System.getenv().collect { k, v -> "$k=$v" }.join('\n>>> ')
sh 'env'
or sh 'printenv'
sh 'env'
// or
sh 'printenv | sort'
env.getEnvironment()
or currentBuild.getRawBuild().getEnvironment()
println currentBuild.getRawBuild().getEnvironment()
// or
println prettyPrint( toJson(env.getEnvironment()) )
import hudson.model.*
def build = Thread.currentThread().executable
def myVar = build.getBuildVariables().get('myVar')
import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.EnvVars
import hudson.model.BuildableItem
import hudson.model.Job
import jenkins.model.*;
jenkins = Jenkins.instance;
EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties()
.get(EnvironmentVariablesNodeProperty.class)
EnvVars env = prop.getEnvVars()
println env['MY_VAR']
import hudson.EnvVars;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
public createGlobalEnvironmentVariables(String key, String value){
Jenkins instance = Jenkins.getInstance();
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance.getGlobalNodeProperties();
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);
EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = null;
EnvVars envVars = null;
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
globalNodeProperties.add(newEnvVarsNodeProperty);
envVars = newEnvVarsNodeProperty.getEnvVars();
} else {
envVars = envVarsNodePropertyList.get(0).getEnvVars();
}
envVars.put(key, value)
instance.save()
}
createGlobalEnvironmentVariables('Var1','Dummy')
### [update Jenkins envvars in Jenkisnfile](https://stackoverflow.com/a/54945018)
```groovy
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
import hudson.slaves.*
def updateEnvVar() {
script {
instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
newEnvVarsNodeProperty = null
envVars = null
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
globalNodeProperties.add(newEnvVarsNodeProperty);
envVars = newEnvVarsNodeProperty.getEnvVars();
envVars.put(NEW_VAR, "toto");
} else {
for (property in envVarsNodePropertyList) {
envVars = property.getEnvVars();
envVars.put("EXISTING_VAR","tata");
}
}
instance.save()
}
}