4

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)

4 Answers 4

9

Replacing

EnvVars envVars = build.getEnvironment(listener); 

By

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

did the trick...

Sign up to request clarification or add additional context in comments.

3 Comments

That makes no sense... Both code snippets should produce the same thing, except the second one creates an instance of EnvVars which is never used and gets discarded straight away. Any idea why this solved the problem?
Thank you for answering your own question and making my life easier.
@Illidanek, Jenkins pipelines appear to execute on the server, so the original post's author might be missing a variable defined for the agent (either via the server's agent definition or via the agent's OS).
1

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 ?

Comments

0

Jenkins pipelines appear to execute on the server, so the original post may be missing a variable defined for the agent (either via the server's agent definition or via the agent's OS).

I don't know the Jenkins pipeline way of obtaining the agent's environment.

Comments

0

I stumbled over this and searched a while for a solution, so for the next user the answer: If you need the environment variables, overwrite

public void perform(Run<?, ?> run, FilePath workspace, EnvVars env, Launcher launcher, TaskListener listener)

instead of

public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)

and use the EnvVars from the parameters. (See also https://issues.jenkins.io/browse/JENKINS-29144)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.