Jenkins Slave - How to Add or Update Environment Variables

Has anyone tried a way to add or update an ENVIRONMENT variable in a Jenkins subordinate configuration using the Jenkins Rest / API or any other way.

Using the Jenkins Swarm plugin, I created a subordinate (it uses JLNP to connect to the Jenkins master), but the ENVIRONMENT variables (the box is not checked) and there are no ENVIRONMENT variables created by the Swarm client bank (by default). User can add manually if reqd, but I am looking for if there is a way to add / update ENV variables in slave.

enter image description here

I want to create several slave roms (where each subordinate 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 at 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 does not provide any method / way to set the Node of ENV properties / variables. There is no setNodeProperties or anything like that (if this is the right method to set ENV variables / properties).

What I'm looking for is a way to add the following variables to the slave.

enter image description here

This post (from Villiam) reflects that someone was trying to do the groovy part to do the same, but I don’t see how he can set ENV variables using the same node creation / management API

Jenkins-CLI has the ability 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 start the groovy interactive shell with

java -jar jenkins-cli.jar -s http://localhost:8080 groovysh

+7
environment-variables jenkins jenkins-plugins master-slave
source share
3 answers

The method that will work if the "Environment Variables" checkbox has not been checked is to use nodeProperties.add(new EnvironmentVariablesNodeProperty)

Completely script that I use to set environment variables on Jenkins when deploying below (assuming it is 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() 
+1
source share

When creating a node, you can pass 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() 
+3
source share

My answer is a bit because of the other answers, but it will include environment variables if it is turned off.

 public class KeyValuePair { String key String value } ... KeyValuePair[] environmentVariables = [...] ... import hudson.slaves.EnvironmentVariablesNodeProperty Jenkins jenkins = Jenkins.instance List<EnvironmentVariablesNodeProperty> nodeProperties = jenkins.globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class) if (nodeProperties.empty) { // Enable 'Environment variables' under 'Global properties' jenkins.globalNodeProperties.add(new EnvironmentVariablesNodeProperty()) nodeProperties = jenkins.globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class) } for (int nodePropertyIndex = 0; nodePropertyIndex < nodeProperties.size(); nodePropertyIndex++) { EnvironmentVariablesNodeProperty nodeProperty = nodeProperties[nodePropertyIndex] for (int environmentVariableIndex = 0; environmentVariableIndex < environmentVariables.size(); environmentVariableIndex++) { KeyValuePair environmentVariable = environmentVariables[environmentVariableIndex] nodeProperty.envVars.put(environmentVariable.key, environmentVariable.value) } } jenkins.save() 
+1
source share

All Articles