Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins plugin - environment variables

I am using Jenkins with Testswarm and this plugin (forked sources).

I want to get a "job name" for Testswarm containing the Jenkins job name, build number and svn revision number.

Putting JOB_NAME in the configuration field does not help, the variable is not replaced by its value.

So I modified the plugin source code to get the Jenkins environment variables but all I get are "null"s.

Here is the culprit code. (in src/main/java/com/javaclimber/jenkins/testswarmplugin/TestSwarmBuilder.java from line 205)

I researched a lot concerning this functionnality and I did not find a working example for getting a variable.

public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    ...
EnvVars envVars = build.getEnvironment(listener);
    ...
    envVars.get("JOB_NAME")
}

I am not at ease in Java and I am stuck at this point. Any idea anyone, please ?

Update: java used version
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~10.04.2)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

like image 757
GMO Avatar asked Dec 26 '22 11:12

GMO


2 Answers

Replacing

EnvVars envVars = build.getEnvironment(listener);

By

EnvVars envVars = new EnvVars();
envVars = build.getEnvironment(listener);

did the trick...

like image 50
GMO Avatar answered Dec 29 '22 00:12

GMO


What version of Java are you using? According to this, to get an environment variable, you need to add the following:

String job_name = System.getenv("JOB_NAME");

Have you tried this instead?

Also, I'm not sure what the configuration field looks like, but did you try using $JOB_NAME instead of JOB_NAME ?

like image 44
Sagar Avatar answered Dec 29 '22 01:12

Sagar