How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

mr-2
Hi!
I have some environment variables as List of key-value pairs within the "Global properties" section of the global Jenkins configuration.

I wonder if it is possible to change such a environment variables programmatically, for example by means of a Groovy script?

Regards,
mr

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

mr-2
Hi Folks!

Do anybody have an idea how to set/change the variables within a Groovy script?

Regards!

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

Walter Kacynski
I use the following script to inject a username and password:

usernamePassword = build.getEnvironment(null)[secret]
def (username, password) = usernamePassword.split(':')

e = new hudson.EnvVars()
e.put(secret + '_USERNAME', username)
e.put(secret + '_PASSWORD', password)
build.environments.add(hudson.model.Environment.create(e))


On Monday, September 22, 2014 3:23:51 AM UTC-4, mr wrote:
Hi Folks!

Do anybody have an idea how to set/change the variables within a Groovy script?

Regards!

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

mr-2
Thanks for the answer!

Where do you inject the username and password? I think you do it into the current build.

But I want to inject a new variable or change an existing variable (like e.g. username or password) into the global configuration, thus next time when I go into the Global Jenkins Configuration UI, I could see the username or password in the section "Global properties".

Is this possible? Is there a package like "hudson.configuration.global" within the API which would allow me to change the environment variable within the global properties jenkins configuration section?



Am Montag, 22. September 2014 19:48:12 UTC+2 schrieb Walter Kacynski:
I use the following script to inject a username and password:

usernamePassword = build.getEnvironment(null)[secret]
def (username, password) = usernamePassword.split(':')

e = new hudson.EnvVars()
e.put(secret + '_USERNAME', username)
e.put(secret + '_PASSWORD', password)
build.environments.add(hudson.model.Environment.create(e))


On Monday, September 22, 2014 3:23:51 AM UTC-4, mr wrote:
Hi Folks!

Do anybody have an idea how to set/change the variables within a Groovy script?

Regards!

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

mr-2
Does anybody have an idea???


Am Dienstag, 23. September 2014 12:03:51 UTC+2 schrieb mr:
Thanks for the answer!

Where do you inject the username and password? I think you do it into the current build.

But I want to inject a new variable or change an existing variable (like e.g. username or password) into the global configuration, thus next time when I go into the Global Jenkins Configuration UI, I could see the username or password in the section "Global properties".

Is this possible? Is there a package like "hudson.configuration.global" within the API which would allow me to change the environment variable within the global properties jenkins configuration section?

--
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 [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

Jonathann Zenou

I seem to have the same issue.
Does someone know what to do to alter a global variable from a job ? Is it even possible ?

Thanks
On Monday, September 29, 2014 at 10:17:57 AM UTC+3, mr wrote:
Does anybody have an idea???


Am Dienstag, 23. September 2014 12:03:51 UTC+2 schrieb mr:
Thanks for the answer!

Where do you inject the username and password? I think you do it into the current build.

But I want to inject a new variable or change an existing variable (like e.g. username or password) into the global configuration, thus next time when I go into the Global Jenkins Configuration UI, I could see the username or password in the section "Global properties".

Is this possible? Is there a package like "hudson.configuration.global" within the API which would allow me to change the environment variable within the global properties jenkins configuration section?

--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/8eef2f94-7794-40bf-af44-9bf17c8daefd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

Victor Volle

I have a partial solution, when I have already enabled Global Properties:


<a imageanchor="1" href="https://lh4.googleusercontent.com/-EJJeUHpiDSs/VO4yDdXk2vI/AAAAAAAAACY/UDR7x-oUvJo/s1600/Configure%2BSystem%2B[Jenkins]%2B2015-02-25%2B21-35-00.png" style="margin-left: 1em; margin-right: 1em;"><img src="https://lh4.googleusercontent.com/-EJJeUHpiDSs/VO4yDdXk2vI/AAAAAAAAACY/UDR7x-oUvJo/s320/Configure%2BSystem%2B[Jenkins]%2B2015-02-25%2B21-35-00.png" border="0" width="320" height="73" style="">


the following script works for me:


import jenkins.*

import jenkins.model.*

import hudson.*

import hudson.model.*


nodes = Jenkins.getInstance().getGlobalNodeProperties()

nodes.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

if ( nodes.size() != 1 ) {

  println("error: unexpected number of environment variable containers: "

          + nodes.size()

          + " expected: 1")

} else {

  envVars= nodes.get(0).getEnvVars()

  envVars.put("FOO", "foo")

  ...

  Jenkins.getInstance().save()

  println("okay")

}

I send this script to Jenkins with:

java -jar /tmp/jenkins-cli.jar -s http://localhost:8080/jenkins/ groovy /tmp/jenkins_global_vars.groovy

and the variables are set and persisted. What I haven't yet figured out, is how to enable the Global Properties in the first place


--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/c7f7c51a-91c4-4fe8-9847-4cd5169153d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

Victor Volle
Final solution:

instance = Jenkins.getInstance()
globalNodeProperties
= instance.getGlobalNodeProperties()
envVarsNodePropertyList
= globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

newEnvVarsNodeProperty
= null
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("FOO", "foo")

instance
.save()



--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/2fc2f1b5-3cae-4230-a1c7-30b94419f5a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

joshphp
Any idea how to make this delete all existing key pairs to ensure only the ones defined in this script  are the ones in Jenkins?

On Wednesday, February 25, 2015 at 4:53:27 PM UTC-5, Victor Volle wrote:
Final solution:

instance = Jenkins.getInstance()
globalNodeProperties
= instance.getGlobalNodeProperties()
envVarsNodePropertyList
= globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

newEnvVarsNodeProperty
= null
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("FOO", "foo")

instance
.save()



--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/777d76e0-6189-42d8-97e7-6ab38c122dad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How to set "Environment variables" in "Global properties" of global Jenkins configuration programmatically?

joshphp
Solved the problem, just add an envVars.clear() to the else statement 

On Thursday, November 9, 2017 at 3:58:36 PM UTC-5, Josh Branham wrote:
Any idea how to make this delete all existing key pairs to ensure only the ones defined in this script  are the ones in Jenkins?

On Wednesday, February 25, 2015 at 4:53:27 PM UTC-5, Victor Volle wrote:
Final solution:

instance = Jenkins.getInstance()
globalNodeProperties
= instance.getGlobalNodeProperties()
envVarsNodePropertyList
= globalNodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)

newEnvVarsNodeProperty
= null
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("FOO", "foo")

instance
.save()



--
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 [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/90f2b3c2-8f92-4ee0-a69c-ad575d863b2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.