Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PATH in Jenkins Declarative Pipeline

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.

like image 321
Vincent Avatar asked Apr 05 '17 16:04

Vincent


People also ask

How do I set environment variable in Jenkins pipeline stage?

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.

How do you pass parameters in Jenkins Declarative pipeline?

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.

Which directive is used to set variables in Declarative pipeline?

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.


2 Answers

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.

like image 66
Vadim Kotov Avatar answered Oct 02 '22 12:10

Vadim Kotov


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'         }     } } 
like image 21
Dan Berindei Avatar answered Oct 02 '22 11:10

Dan Berindei