1

I'd like to set Jenkins Global Properties - Environment variables via Groovy script (YAML file) which is executed in Ansible.
I've tried following

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
instance = Jenkins.getInstance()
envVars.put("FOO1", "bar1")
envVars.put("FOO2", "bar2")
instance.save()

Why is this not working?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Tony Montana
  • 357
  • 2
  • 5
  • 16
  • See if this helps - https://stackoverflow.com/questions/10413936/creating-a-jenkins-environment-variable-using-groovy or https://stackoverflow.com/questions/10625259/how-to-set-environment-variables-in-jenkins/10626193#10626193 – Rao Jun 19 '17 at 15:42
  • Thank you for reply, but this I've seen already and this will not help. – Tony Montana Jun 19 '17 at 19:22
  • Where is the YAML? – Anthon Jun 20 '17 at 07:48

1 Answers1

7

Done in this way:

import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
instance = Jenkins.getInstance()
globalNodeProperties = instance.getGlobalNodeProperties()
envVarsNodePropertyList = globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

envVars = null
if ( envVarsNodePropertyList == null || envVarsNodePropertyList.size() == 0 ) {
  newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
  globalNodeProperties.add(newEnvVarsNodeProperty)
  envVars = newEnvVarsNodeProperty.getEnvVars()
} else {
  envVars = envVarsNodePropertyList.get(0).getEnvVars()  
}
envVars.put("name", "value")
instance.save()
badbishop
  • 1,281
  • 2
  • 18
  • 38
Tony Montana
  • 357
  • 2
  • 5
  • 16
  • i dont want to set the variables globally, only for that build that time so instead of globalNodeProperties = instance.getGlobalNodeProperties() what can be used and what other changes do i need to make? – jayant singh Sep 19 '17 at 20:18