Jenkins Slave - How to add or update ENVIRONMENT variables

夙愿已清 提交于 2019-12-03 03:09:31

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()

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()

My answer is a bit of a mish-mash of other answers, but it will turn 'Environment variables' on if it's 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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!