Okay, mere moments after I posted this, user @OltzU gave me a solution in 
IRC.

I needed to be using instance.nodeProperties.add(). So this code works

import jenkins.model.Jenkins
import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.slaves.NodeProperty


def instance = Jenkins.instance


def evnp = new EnvironmentVariablesNodeProperty();
for (prop in evnp) {
  prop.envVars.put("MY_OTHER_NAME", "my_other_value")
}


instance.nodeProperties.add(evnp)


instance.save()

Perfect!

On Tuesday, 3 May 2016 16:02:15 UTC+1, Barry Laffoy wrote:
>
> Hi
>
> As in this SO question, http://stackoverflow.com/q/34186806/1506521, I am 
> trying to set environment variables on my Jenkins nodes (master + slaves) 
> automatically. i.e. via a script, without having to go through the web 
> front end.
>
> The code given in the answer only works if the Node Properties Environment 
> Variables checkbox in the web interface has already been ticked.
>
> import jenkins.model.*
> import hudson.model.*
> import hudson.slaves.*
>
> jenkins = Jenkins.instance
> node = jenkins.getNode('master')
> props = 
> node.nodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
>
> for (prop in props) {
>   prop.envVars.put("MY_OTHER_NAME", "my_other_value")
> }
>
> jenkins.save()
>
>
> I can't seem to find a way to enable the Node Properties Environment 
> Variables checkbox without doing so manually through the web interface.
>
> I can create a new EnvironmentVariablesNodeProperty class and insert my 
> env vars into this, but then I cannot apply this to the jenkins instance:
>
> import jenkins.model.Jenkins
> import hudson.slaves.EnvironmentVariablesNodeProperty
> import hudson.slaves.NodeProperty
>
> def instance = Jenkins.instance
> def evnp = new EnvironmentVariablesNodeProperty();
>
> for (prop in evnp) {
>   prop.envVars.put("MY_OTHER_NAME", "my_other_value")
> }
>
> instance.nodeProperties = evnp
>
> instance.save()
>
> This results in the error message 
>
> groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: 
>> nodeProperties for class: hudson.model.Hudson
>
>
> In jenkins.model.Jenkins, nodeProperties is set to private, so I believe 
> that's why I can't overwrite it. However, I don't see any interface to 
> update it.
>
> Is there some clever groovy metaprogramming trick that might allow me to 
> force-write a private field?
>
> Alternatively, it seems possible to create a new DumbSlave via groovy with 
> this code
>
> 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))
>
> However, I don't see how this could be used to set an environment variable 
> on the existing master node. It doesn't seem possible to overwrite that 
> node with a new DumbSlave. Is there some way of specifying a list of 
> environment variables for the master node when first deploying Jenkins?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/50528d2e-e7e3-405a-81c4-5626ae09eae4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to