Hudson Modify Environment Variables Properties

Myself and another have been trying to figure out how to modify the environment variables programmaticly using groovy in Hudson. After a little of tinkering we were able to do it.

1
2
3
4
5
6
7
def hudson = hudson.model.Hudson.instance
def globalProps = hudson.globalNodeProperties
def props = globalProps.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
for (prop in props) {
    prop.envVars.put("TEST_ENV", "WORKS")
}
hudson.save() //This is needed in order to persist the change

Then if you want to expand this for the slaves:

1
2
3
4
5
6
7
8
9
10
11
12
def slaves = hudson.model.Hudson.instance.getNodes()
for (slave in slaves) {
    println "-----------------------"
    println slave.name
    println "-----------------------"

    def props = slave.nodeProperties.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class)
    for (prop in props) {
        prop.envVars.put("TEST_ENV", "WORKS")
    }
    //SAVE
}

8 thoughts on “Hudson Modify Environment Variables Properties

  1. Is there any way to set those environment variables for the current build only?

    The method you defined above set a global environment variable for Hudson, so if 2 jobs use the same variable name, it can result in a conflict if they run simultanously

  2. I actually found a way to set parameters for the current build only (after hours of searching) :

    {code}
    import hudson.model.*;
    import hudson.util.*;

    AbstractBuild currentBuild = (AbstractBuild) Thread.currentThread().executable;

    ParametersAction newParamAction = new hudson.model.ParametersAction(new hudson.model.StringParameterValue(“TEST_TEMP_VAR”,”value28″));

    currentBuild.addAction(newParamAction);
    {code}

  3. New to Hudson, is there a way in KSH to save variable in one build step and reference them in the other KSH Build Step? I understand conceptually what you are doing , but how do I translate that into KSH?

  4. KSH – I assume this is KornShell scripting. If so, then I am not sure if KSH could save variables. In my case above, I wanted to be able to change the environment variables on Hudson / Jenkins itself since it has the ability to store environment variables and then have them passed into the jobs, scripts, etc. You can also change the environment variables on the slave instances through the UI. My goal with the code above was to change this all through groovy script. Hopefully that helps in clarifying. Otherwise, let me know.

  5. Hello, Ryan!

    I try to change several jobs props by script.
    But changes became only for instance.
    I tried to use hudson.model.Hudson.instance.save()
    It runs, shows empty result with no errors.
    But there is no changes in .xml files in $ENV_HOME/tools/hudson/home/jobs//config.xml
    File changing, only after clicking on the button “Save” in job prefernce.
    Do you know how to solve this issue?

  6. That’s what I mean:
    {code}
    import hudson.model.*
    import hudson.tasks.*

    def TheGreatNameOfJob
    //looking for JOBZZ
    for(item in Hudson.instance.items) {
    TheGreatNameOfJob=(item.name as String)
    if(TheGreatNameOfJob.indexOf(“Name of job”)>=0){

    //Filteringg…
    if(item instanceof FreeStyleProject) {
    for(publisher in item.publishersList){
    // searching for mailzz
    if(publisher instanceof Mailer) {
    publisher.recipients=”example@server.com”;

    }
    }
    }
    }
    }
    Hudson.instance.save();
    {code}

Comments are closed.