Jenkins Slave - How to add or update ENVIRONMENT variables

后端 未结 3 1348

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,

3条回答
  •  囚心锁ツ
    2021-02-06 01:11

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

提交回复
热议问题