Shell – Where should environment variables be set for Jenkins

environment-variablesJenkinsshell

I am using Jenkins to automate application builds using Maven on Linux.

Where should I set environment variables such as $JAVA_HOME and append items to $PATH so that they are available to Jenkins?

I have tried a few different places and had no success. I'm not certain on what sort of shell Jenkins uses, whether its a login/non-login, interactive or non-interactive.

Best Answer

This is the perfect scenario for a Global Tool Configuration. From the Jenkins home page, Click Manage Jenkins -> Global Tool Configuration. If you have a default installation, this page will let you add multiple configurations for installing Maven on your build servers.

Global Tool Configuration for Maven

Once you've configured the tools, you can use them in your jobs by adding "Invoke Top Level Maven Targets" build steps. If you are using specific slaves/nodes for each job, you can pick the Maven that should be installed on each server. Then when the jobs run, Jenkins will manage the installation for you automatically.

"Invoke Top Level Maven Targets" Build Step

Specifically for pipelines, there's the Pipeline Maven Integration plugin. I haven't used it but from the docs it looks like it should be able to do what you're asking:

Provides Maven integration with Pipeline Plugin by using the withMaven step, which configures a maven environment to use within a pipeline job by calling sh mvn or bat mvn.

And this blog post, Declarative Pipeline for Maven Projects, give a good example of configuring Jenkins to run a pipeline with Maven.

Related Question