Setting global environment variables in Jenkinsfile on Windows

jenkins pipeline set environment variable from sh
jenkins pipeline set global variables
jenkins set environment variable from script
jenkins environment variables shell script
jenkins scripted pipeline set environment variable
groovy set environment variable
jenkins inject environment variables
jenkins groovy get environment variable

I a have Jenkins CI server running on top of a Windows system, and now I would like to set global environment variables for my pipeline.

I tried two different ways both without success:

node {
    environment {
    stage ('Checkout and Build') {
        bat 'echo %MY_PYTHON%'
        bat 'echo %PATH%'
        bat 'python --version'

After starting the build it throws the following error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

WorkflowScript: 3: unexpected char: '\' @ line 3, column 22.



Kinda straightforward so I tried doubling the backslashes like this:

environment {

So after restarting the build I got error only for the command python --version as python still cannot be found on path, and also I get back nothing for MY_PYTHON and for PATH I get all the predifined paths except those I wanted to add.

Got stuck, so any help would be appreciated!

Using environment variables, How to list the environment variables available to Jenkins Pipeline We invoke it using the sh command of the Jenkins Pipeline. Before we do that On Windows you could run: bat('set') How to set environment variables in Jenkins? As also  Environment variables may also be set by Jenkins plugins. Refer to the documentation of the specific plugins for environment variable names and descriptions for those plugins. Another common use for environment variables is to set or override "dummy" credentials in build or test scripts.

Try that : MY_PYTHON='D:\\ProgramData\\Anaconda3'

Jenkins Pipeline - set and use environment variables, You will learn how to define env variables, how to updated them, and how to use them in boolean expressions  How to use environment variables in the environment section of Jenkins? environment { PATH = "/path/to/dir:${env.PATH}" JNK_PATH = "${env.WORKSPACE}\\subdir" } The above only works when the environment section is inside a "stage" but would yield "null" for WORKSPACE outside the stages. Set environment executing code

To expand a variable you have to use the double quotes:


Regardless of that, looks like there is a problem in particular to set the PATH in windows, as "PATH" is also a command.

What you have to do is something like this:

node {
    environment {
    stage ('Checkout and Build') {
        bat '''
            set PATH = "%MY_PYTHON%;%MY_PYTHON%\Scripts;%PATH%"
            echo %MY_PYTHON%
            echo %PATH%
            python --version

Jenkins Pipeline Environment Variables, Creating Global environment variables; Access Global environment variables. In jenkins pipeline there are lot of other useful environment variables which can  The latest version is selected by use of the Jenkins Environment Variables. Build a new free-style software project Add a new "Execute windows batch command" build step with the following command: dir > sample.txt echo "This is just an example" > sample.txt **This will create a file on the Jenkins machine

Jenkins environment variables - Mukesh Singal, To add to @Omkar's answer, I would like to explain how to add the environment variable. Manage Jenkins - Configure System - Global  In the Control Panel, open the System option (alternately, you can right-click on My Computer and select Properties). Select the “Advanced system settings” link. In the System Properties dialog, click “Environment Variables”. In the Environment Variables dialog, click the New button underneath the “System variables” section.

Set environment variables in Jenkins, I want to set environment variables in my pipelines from a shared global function. jenkins pipelining Setting an environment variable within a Jenkins Pipeline is Add a new "Execute windows batch command" build step with the following  Setting Environment Variables in Windows 10 Follow the process above to get into the environment variables dialog box. After highlighting the PATH variable from “System Variables” click the Edit button.

Jenkins environment variables pipeline, My example Jenkins Pipeline setup for Android app project - Jenkinsfile For a quick Setting global environment variables. see also Jenkins Handbook: Working If your pipeline will run on MS Windows you'll need to use the bat command. Setting environment variables The environment variables can be set declaratively using environment { } block, imperatively using env.VARIABLE_NAME , or using withEnv(["VARIABLE_NAME=value"]) {} block.

  • Correct! Here, bat "echo var: $VARIABLE_NAME" is interpreted as a Groovy GString and hence interpolated by Groovy and not the OS. Nevertheless, on Windows, %VARIABLE_NAME% should work with either single or double quotes.