2

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.

2

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.

  • I ended up using this approach. However please note that the global tools is just that - global. To vary by agent you must override in the agent configuration. – Mark W Mar 15 '18 at 11:00
  • @MarkW do you mind selecting this as the approach you used? – Michael J Jan 3 at 2:36
3

This question was already asked over at Stack Overflow and you may want to look at the answers there: How to set environment variables in Jenkins?

Global, static environment variables can be set for any Jenkins installation in Manage Jenkins > Configure System > Global Properties > Environment Variables.

Environment variables can be set per-executor in the executor settings. There's a tickbox for "environment variables" and when it is ticked, the environment variable configuration is exposed.

Environment variables can also be set per-job:

  • Thanks. I am using pipelines, and that will probably work. However paths vary by node environment. I have several, and I'd rather not have several conditional statements in the pipeline. It makes it quite brittle. – Mark W Mar 9 '18 at 15:39
  • 1
    @MarkW environment variables can be set per-executor in the executor settings. There's a tickbox for "environment variables" and when it is ticked, the environment variable configuration is exposed. – jayhendren Mar 9 '18 at 20:42
1

If you're using configuration-as-code to configure the Jenkins master instance, you can use globalNodeProperties as shown at https://github.com/jenkinsci/configuration-as-code-plugin/blob/d9171d086fbbf92cb5807da2dd12d6fc43df7a6b/demos/jenkins/jenkins.yaml#L7-L11:

globalNodeProperties:
    - envVars:
        env:
          - key: FOO
            value: BAR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.