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.
Setting Stage Level Environment Variable It is by using the env variable directly in the script block. We can define, let us say, USER_GROUP and display it. You will see that the underlying shell also has access to this environment variable. You can also set an environment variable using withEnv block.
Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on. Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.
The environment directive specifies a sequence of key-value pairs which will be defined as environment variables for the all steps, or stage-specific steps, depending on where the environment directive is located within the Pipeline.
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.
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' } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With