47

In Jenkins scripted pipeline you can set PATH env variable like this :

node {
   git url: 'https://github.com/jglick/simple-maven-project-with-tests.git'
   withEnv(["PATH+MAVEN=${tool 'M3'}/bin"]) {
      sh 'mvn -B verify'
   }
}

Notice the PATH+MAVEN as explained here https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-withenv-code-set-environment-variables :

A list of environment variables to set, each in the form VARIABLE=value or VARIABLE= to unset variables otherwise defined. You may also use the syntax PATH+WHATEVER=/something to prepend /something to $PATH.

But I didn't find how to do it in declarative pipeline using environment syntax (as explained here : https://jenkins.io/doc/pipeline/tour/environment).

environment {
    DISABLE_AUTH = 'true'
    DB_ENGINE    = 'sqlite'
}

Ideally I would like to update the PATH to use custom tools for all my stages.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Vincent
  • 687
  • 2
  • 6
  • 8

4 Answers4

88

It is possible with environment section:

pipeline {
  agent { label 'docker' }
  environment {
    PATH = "/hot/new/bin:${env.PATH}"
  }
  stages {
    stage ('build') {
      steps {
        echo "PATH is: ${env.PATH}"
      }
    }
  }
}

See this answer for info.

jpsecher
  • 4,461
  • 2
  • 33
  • 42
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    That is significantly nicer than the `withEnv(['PATH+WHATEVER=...'])` mess! – jpyams Apr 06 '18 at 22:21
  • 8
    This almost works, but `PATH` is null in the environment block, so when I use this I end up with just `/hot/new/bin` and nothing else. – Max May 29 '18 at 13:28
  • 2
    This works if I set it at the stage level, rather than pipeline. – Max May 30 '18 at 12:50
  • Will this work if you change `$PATH` to `${PATH}` in the environment block? – Jeremy Jan 03 '19 at 00:20
  • 3
    @Jeremy it needs to be ${env.PATH} – Travis Primm May 07 '19 at 23:44
  • 2
    This method does not set the PATH variable inside the shell. Try running sh 'echo $PATH', it will not have the /hot/new/bin part. The echo step picks up the path variable through env interpolation of Jenkins. If you are trying to call commands, e.g. a command installed by python pip, this solution does not work for appending $HOME/.local/bin to the PATH. – olivier.va Sep 16 '20 at 17:58
  • 1
    `environment { PATH = "...:$PATH" }` worked just fine for me. – davidrmcharles Feb 06 '21 at 04:59
7

As a workaround, you can define an environment variable and use it in the sh step:

pipeline {
    environment {
        MAVEN_HOME = tool('M3')
    }

    stages {
        stage(Maven') {
           sh '${MAVEN_HOME}/bin/mvn -B verify'
        }
    }
}
Dan Berindei
  • 7,054
  • 3
  • 41
  • 48
  • Where is the `tool` function defined? – Learner Mar 17 '20 at 07:10
  • It's predefined, but you need the tool to be configured in the global settings: https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#tool-use-a-tool-from-a-predefined-tool-installation – Dan Berindei Mar 18 '20 at 07:09
  • Could you do something like MAVEN_HOME = powershell("doSomething") - where doSomething is a function defined on the system path? I'm trying to call a powershell script that will return the location of python. – Learner Mar 18 '20 at 07:56
  • 1
    I see there is a `powershell` function, so `powershell(script = "doSomething", returnStdout = true)` might work, but I never tried it. – Dan Berindei Mar 21 '20 at 22:16
1

Check the following link, this explains how to configure your tools. Using the declarative pipeline things become a bit different but overall it is easier to understand.

declarative-maven-project

Wim Ederveen
  • 164
  • 3
1

Using the tool section in pipeline is only allowed for pre-installed Global Tools. Some tools are provided by plugins, but if it not exists I'am afraid you cannot use the environment setup via pipeline tool declaration.

I hope to be wrong!

PRF
  • 821
  • 1
  • 9
  • 16