Has anyone tried a way to add or update an ENVIRONMENT variable in a Jenkins slave\'s configuration using Jenkins Rest/API or any other way.
Using Jenkins Swarm plugin,
A method that will work if the "Environment Variables" checkbox has not been ticked is to use nodeProperties.add(new EnvironmentVariablesNodeProperty)
The full script I'm using to set Environment Variables on Jenkins when deploying is below (intended to be called with jenkins-cli.jar):
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
String node_name = args[0]
String env_key = args[1]
String env_value = args[2]
instance = Jenkins.getInstance()
if (node_name == "master") {
node = instance
} else {
instance.getNode(node_name)
}
props = node.nodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
if(props.empty) {
def entry = new EnvironmentVariablesNodeProperty.Entry(env_key, env_value)
def evnp = new EnvironmentVariablesNodeProperty(entry)
node.nodeProperties.add(evnp)
} else {
for (prop in props) {
prop.envVars.put(env_key, env_value)
}
}
instance.save()