如何在 Jenkins 中使用 Windows 批处理命令使用环境变量? [英] How are environment variables used in Jenkins with Windows Batch Command?
问题描述
我正在尝试在我的 xcopy 脚本中使用 Jenkins(全局)环境变量.
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
推荐答案
我对 Jenkins 一无所知,但看起来您正在尝试使用某种形式的 unix 语法访问环境变量 - 这行不通.
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.
如果变量的名称是 WORKSPACE,则该值在 Windows 批处理中使用%WORKSPACE%
.这种形式的扩展是在解析时执行的.例如,这将打印到屏幕上 WORKSPACE 的值
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%
如果你在执行时需要这个值,那么你需要使用延迟扩展!WORKSPACE!
.默认情况下通常不启用延迟扩展.使用 SETLOCAL EnableDelayedExpansion
来启用它.由于括号内的代码块和/或由 &
、&&
或 ||
连接的多个命令,通常需要延迟扩展一次全部解析,因此除非您使用延迟扩展,否则无法稍后在同一块中读取在块内分配的值.
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!
)
上面的输出是
Normal Expansion = BEFORE
Delayed Expansion = AFTER
在命令行中使用 HELP SET
或 SET/?
来获取有关 Windows 环境变量和各种扩展选项的更多信息.例如,它解释了如何进行搜索/替换和子字符串操作.
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.
这篇关于如何在 Jenkins 中使用 Windows 批处理命令使用环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!