Unable to overwrite JAVA_HOME in Workflow

146 views
Skip to first unread message

Rafael Ribeiro Rezende

unread,
Aug 6, 2015, 8:28:26 AM8/6/15
to Jenkins Developers
Hello all,

I need to get my JAVA_HOME environment variable from within my MyPublisher plugin's perform method. So, I do the following:

@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher,
       
TaskListener listener) throws InterruptedException, IOException {
   
         
PublisherLogger logger = new PublisherLogger(listener.getLogger());
         logger
.info("Starting the RTC Publisher post-build step.");

         
// Get environment variables
         
EnvVars env = run.getEnvironment(listener);
         
...
}

My default JAVA_HOME in the OS is
JAVA_HOME=C:\Apps\openjdk-1.7.0.45

I have a single JDK installation in my Jenkins global configuration
Name: Java Tool
JAVA_HOME
: C:\tool\java_jdk\1.7.0_60_64

Finally, in my workflow job I have attempted to assign my JAVA_HOME in two different ways:
node {

     env
.JAVA_HOME = "C:\\tool\\java_jdk\\1.7.0_60_64"
     echo env
.JAVA_HOME

     env
.JAVA_HOME="${tool 'Java Tool'}"
     echo env
.JAVA_HOME

     step
([$class: 'MyPublisher',  antName: 'Ant 1.9.2', ...])
     
...
}

The echo commands print the right paths:
Running: Print Message
C
:\toolbase\java_jdk\1.7.0_60_64
Running: Install a tool
Running: Print Message
C
:\toolbase\java_jdk\1.7.0_60_64

Still, when I debug the env variable from the code on top, I see
JAVA_HOME=C:\Apps\openjdk-1.7.0.45

I'm pretty sure I'm misusing it somehow. But the requirement is so simple that I don't know what else to try.
Any help?

Jesse Glick

unread,
Aug 6, 2015, 4:43:22 PM8/6/15
to Jenkins Dev
On Thu, Aug 6, 2015 at 8:28 AM, Rafael Ribeiro Rezende
<rafael...@gmail.com> wrote:
> I need to get my JAVA_HOME environment variable from within my MyPublisher
> plugin's perform method. So, I do the following:
>
> EnvVars env = run.getEnvironment(listener);

`Run.getEnvironment` produces the environment variables defined for
the build as a whole. These are generally quite limited: `BUILD_TAG`,
that sort of thing. It will never reflect anything set _within_ the
flow, because that is specific to the `StepContext` (consider that you
could use `withEnv` inside one branch of a `parallel` step).

From a `StepExecution`, you would just `@StepContextParameter private
EnvVars env;` to get access to the contextual environment variables.
This is not available to a `SimpleBuildStep`, however. And that is
tracked as: https://issues.jenkins-ci.org/browse/JENKINS-29144

Rafael Ribeiro Rezende

unread,
Aug 7, 2015, 6:37:12 AM8/7/15
to Jenkins Developers
Thanks for the reply, Jesse.
Basically I'm using an Ant installation (existing Ant plugin: https://github.com/jenkinsci/ant-plugin/blob/master/src/main/java/hudson/tasks/Ant.java) within my plugin. When I run my plugin, the internal Ant plugin is simply using the default JAVA_HOME from my OS.
So, if I understood well, the only way to force this Ant installation to use another JAVA_HOME is by resetting the env variable in console before starting my Jenkins instance. Is that right?

Jesse Glick

unread,
Aug 11, 2015, 10:28:31 AM8/11/15
to Jenkins Dev
On Fri, Aug 7, 2015 at 6:37 AM, Rafael Ribeiro Rezende
<rafael...@gmail.com> wrote:
> if I understood well, the only way to force this Ant installation to use
> another JAVA_HOME is by resetting the env variable in console before
> starting my Jenkins instance. Is that right?

You could

· set $JAVA_HOME as a variable accessible to the whole `Run`, for
example by using node properties
· fix JENKINS-29144 and use a version of Jenkins core with the fix
· implement `Step` from `workflow-step-api` directly (rather than
implementing `SimpleBuildStep` to share the class with the freestyle
publisher) and get the contextual `EnvVars`

The third option seems most practical, though it is somewhat more intrusive.
Reply all
Reply to author
Forward
0 new messages