Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are environment variables used in Jenkins with Windows Batch Command?

I'm trying to use Jenkins (Global) environment variables in my xcopy script.

${WORKSPACE} doesn't work "${WORKSPACE}" doesn't work '${WORKSPACE}' doesn't work 
like image 742
DonBecker Avatar asked Dec 22 '11 16:12

DonBecker


People also ask

How use Jenkins environment variable in Windows batch command?

In Jenkins, go to the Build section of your project's configuration. Depending on the platform where your project executes, add either a Windows batch command (Windows) or a shell (UNIX) build step. This sets the following environment variables - COBDIR, PATH, CLASSPATH and LD_LIBRARY_PATH (or LIBPATH on AIX).

How do I set an environment variable in a batch file?

When creating batch files, you can use set to create variables, and then use them in the same way that you would use the numbered variables %0 through %9. You can also use the variables %0 through %9 as input for set. If you call a variable value from a batch file, enclose the value with percent signs (%).

What is Windows batch command in Jenkins?

Now that we have created our own Jenkins Job, we will move forward towards running batch commands in the Jenkins. A batch command is a command that is run in the shell of your system like command prompt in Windows. This name comes from the batch file that were created for early operating systems like DOS and Windows.


1 Answers

I know nothing about Jenkins, but it looks like you are trying to access environment variables using some form of unix syntax - that won't work.

If the name of the variable is WORKSPACE, then the value is expanded in Windows batch using
%WORKSPACE%. That form of expansion is performed at parse time. For example, this will print to screen the value of WORKSPACE

echo %WORKSPACE% 

If you need the value at execution time, then you need to use delayed expansion !WORKSPACE!. Delayed expansion is not normally enabled by default. Use SETLOCAL EnableDelayedExpansion to enable it. Delayed expansion is often needed because blocks of code within parentheses and/or multiple commands concatenated by &, &&, or || are parsed all at once, so a value assigned within the block cannot be read later within the same block unless you use delayed expansion.

setlocal enableDelayedExpansion set WORKSPACE=BEFORE (   set WORKSPACE=AFTER   echo Normal Expansion = %WORKSPACE%   echo Delayed Expansion = !WORKSPACE! ) 

The output of the above is

Normal Expansion = BEFORE Delayed Expansion = AFTER 

Use HELP SET or SET /? from the command line to get more information about Windows environment variables and the various expansion options. For example, it explains how to do search/replace and substring operations.

like image 182
dbenham Avatar answered Oct 21 '22 16:10

dbenham