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, I created a slave (it uses JLNP to connect to the Jenkins master) but ENVIRONMENT variables (checkbox is not ticked) and there are no ENVIRONMENT variables created by Swarm client jar (by default). A user can manually add if reqd but I'm looking if there's a way to add / update ENV variables in a slave.
I want to create multiple swarm slaves (where each slave has different tools with different values i.e. slave01 JAVA_HOME=/path/jdk1.7.0.67 and other slave02 JAVA_HOME=/path/jdk1.8.0_45 etc etc).
I tried looking into http://javadoc.jenkins-ci.org/hudson/model/Node.html or http://javadoc.jenkins-ci.org/hudson/model/Slave.html or http://javadoc.jenkins-ci.org/hudson/slaves/DumbSlave.html but it doesn't provide any method / way to set Node's properties / ENV variables. There's no setNodeProperties or something like that (if that's the correct method to set the ENV variables/properties).
What I'm looking for is a way to add the following variables to the slave.
This post (by Villiam) reflects that someone tried groovy piece to do the same but I don't see how he can set ENV variables using the same API to Create/Manage Nodes
Jenkins-CLI has an option to run groovy scripts:
java -jar path/to/jenkins-cli.jar -s http://localhost:8080 groovy path/to/script
script:
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
Jenkins.instance.addNode(new DumbSlave("test-script","test slave description","C:\\Jenkins","1",Node.Mode.NORMAL,"test-slave-label",new JNLPLauncher(),new RetentionStrategy.Always(),new LinkedList()))
(see docs for other options: http://javadoc.jenkins-ci.org/)
You can also run an interactive groovy shell with
java -jar jenkins-cli.jar -s http://localhost:8080 groovysh
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()