Where should environment variables be set for Jenkins

47,699

Solution 1

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.

Solution 2

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:

Solution 3

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
Share:
47,699

Related videos on Youtube

Mark W
Author by

Mark W

Updated on September 18, 2022

Comments

  • Mark W
    Mark W over 1 year

    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.

  • Mark W
    Mark W about 6 years
    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.
  • jayhendren
    jayhendren about 6 years
    @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.
  • Mark W
    Mark W about 6 years
    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.
  • Michael J
    Michael J over 5 years
    @MarkW do you mind selecting this as the approach you used?