Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a Jenkins/Hudson plugin to set a environment variable pair

I am extending an existing Jenkins/Hudson plugin. I would like it to set a environment variable pair for the running project. What is the easiest way to do that?

like image 523
datka Avatar asked Aug 15 '11 15:08

datka


People also ask

How do you transfer workspace and environment variable in a pipeline to the next job?

Under Build Environment check Set environment variables through a file. give the path of that file here. If the environment variable is created in the first job then again you can save all the environment variable in a file and browse it using the above method. Install this plugin and go to job configuration paeg.

How do I add a global variable in Jenkins?

We can set global properties by navigating to “Manage Jenkins -> Configure System -> Global properties option”.


1 Answers

During build, for example in a Builder's perform() method, you can do at least this:

@Override
public boolean perform(Build<?, ?> build, Launcher launcher, BuildListener listener)
                       throws InterruptedException, IOException {
    //...
    List<ParameterValue> params = new ArrayList<ParameterValue>();
    params.add(new StringParameterValue(name1, value1));
    params.add(new StringParameterValue(name2, value2));
    build.addAction(new ParametersAction(params));
    //...
}

It will add the key-value pairs as build parameters, which will be visible as environment variables too, in the usual manner. Note: I have not tested that extensively, there may be some "gotcha" which presents itself in some situation... But it has worked for me so far.

like image 83
hyde Avatar answered Sep 21 '22 14:09

hyde