78

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

3 Answers 3

148

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.

2
  • 1
    I totally forgot about that little nit picky detail about windows. Thanks !
    – JoxTraex
    Oct 11, 2012 at 19:17
  • @UtsavGupta I was also stuck on this. I was using $BUILD_ID earlier. Now happy with %BUILD_ID% on windows. Feb 3, 2017 at 8:20
30

In windows you should use %WORKSPACE%.

2
  • 6
    Yes. And in Hudson's "Build/Command/Execute Windows batch command" field. Dec 22, 2011 at 17:03
  • %VARIABLE% syntax can also be used to refer User-defined Jenkins variables. May 31, 2022 at 11:36
4

I should this On Windows, environment variable expansion is %BUILD_NUMBER%

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.