Where should environment variables be set for Jenkins

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:

  • If you are using Pipelines, use the withEnv step.

  • If you are using old-style Freestyle jobs, use the EnvInject plugin.


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.


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