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,
When creating the node, you can pass the variables as the last parameter:
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
entry = new EnvironmentVariablesNodeProperty(new EnvironmentVariablesNodeProperty.Entry("MY_NAME", "my_value"))
list = new LinkedList()
list.add(entry)
Jenkins.instance.addNode(new DumbSlave("test-slave", "test slave description", "C:\\Jenkins", "1", Node.Mode.NORMAL, "test-slave-label", new JNLPLauncher(), new RetentionStrategy.Always(), list))
From DumbSlave here and EnvironmentVariablesNodeProperty here.
To add variables to an existing slave, we can use the following:
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
jenkins = Jenkins.instance
node = jenkins.getNode('test-slave')
props = node.nodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
for (prop in props) {
prop.envVars.put("MY_OTHER_NAME", "my_other_value")
}
jenkins.save()