如何在 Jenkins 中使用 Windows 批处理命令使用环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8606664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How are environment variables used in Jenkins with Windows Batch Command?
提问by DonBecker
I'm trying to use Jenkins (Global) environment variablesin my xcopy script.
我正在尝试在我的 xcopy 脚本中使用Jenkins(全局)环境变量。
${WORKSPACE} doesn't work
"${WORKSPACE}" doesn't work
'${WORKSPACE}' doesn't work
回答by dbenham
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.
我对 Jenkins 一无所知,但看起来您正在尝试使用某种形式的 unix 语法访问环境变量 - 这是行不通的。
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
如果变量的名称是 WORKSPACE,则在 Windows 批处理中使用%WORKSPACE%
. 这种形式的扩展是在解析时执行的。例如,这将打印到屏幕 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.
如果在执行时需要该值,则需要使用延迟扩展!WORKSPACE!
。默认情况下通常不启用延迟扩展。使用SETLOCAL EnableDelayedExpansion
来启用它。通常需要延迟扩展,因为括号内的代码块和/或由&
,&&
或连接的多个命令被||
一次性解析,因此块内分配的值无法在同一块中稍后读取,除非您使用延迟扩展。
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.
使用HELP SET
或SET /?
从命令行获取有关 Windows 环境变量和各种扩展选项的更多信息。例如,它解释了如何进行搜索/替换和子字符串操作。
回答by Volodymyr Bezuglyy
In windows you should use %WORKSPACE%
.
在 Windows 中,您应该使用%WORKSPACE%
.
回答by Aykut ?ALI?KAN
I should this On Windows, environment variable expansion is %BUILD_NUMBER%
我应该在 Windows 上,环境变量扩展为 %BUILD_NUMBER%