在 TeamCity 构建脚本中使用百分号
我正在尝试设置运行自定义命令行脚本的 TeamCity 构建流程。该脚本使用变量,因此需要百分号(例如%x
)。但 TeamCity 使用百分号表示其属性(例如 %build.number%
),因此脚本中的百分号在运行时会被删除。
如果脚本包含以下内容:
for /d %x in ("c:\*") do @echo "%x"
这就是它实际运行的内容:
for /d x in ("\*") do @echo "x"
如何编写脚本以便它可以包含变量?
I am trying to set up a TeamCity build process that runs a custom command line script. The script uses a variable so it needs a percent sign (e.g. %x
). But TeamCity uses percent signs for its properties (e.g. %build.number%
), so the percent sign in the script gets removed when it runs.
If the script contains this:
for /d %x in ("c:\*") do @echo "%x"
This is what it actually runs:
for /d x in ("\*") do @echo "x"
How can I write my script so it can include variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想将
%
传递给 TeamCity,您应该使用另一个%
对其进行转义,即对于%
来说,它必须是%%< /代码>。
但是Windows命令行将
%
视为转义字符,因此您应该再次转义它,在每个%
之前添加另一个%
,即对于%%
你应该通过%%%%
流程是:
tl;dr: 你的问题的答案将是:
If you want to pass
%
to TeamCity, you should escape it with another%
, i.e. for%
it must be%%
.But the Windows command line considers
%
as an escape character, so you should escape it again adding another%
before each%
, i.e. for%%
you should pass%%%%
Flow is:
tl;dr: the answer to your question will be:
尝试 for /d %%x in ("c:\*") do @echo "%%x" (即重复 % 符号)。
但应该有一种方法告诉 TC 不要管该文件。如果 TC 删除源代码中的百分号,那就太可怕了。因此,我非常确定您在配置中做了一些操作来启用
%
的替换。同样,TC 真的会扰乱脚本吗?或者您是否使用构建工具来生成脚本或类似的东西?
Try
for /d %%x in ("c:\*") do @echo "%%x"
(i.e. duplicate the % signs).But there should be a way to tell TC to leave the file alone. It would be horrible if TC would remove the percent signs in the sources. Therefore, I'm pretty sure that you did something in the configuration to enable replacement of
%
.On a similar note, is it really TC that messes with the script? Or are you using a build tool to generate the script or something like that?
看来 TeamCity 只是将您输入的内容粘贴到
a .cmd
文件中在这些情况下,
for
语句需要双百分号。然后,TeamCity 似乎删除了这些
%
符号之一,这就是 tspauld 让它工作的原因(这也是我让它运行的方式)。在日志中,TeamCity 似乎在此处创建了一个文件
Program Files\TeamCity\buildAgent\temp\agentTmp
但 for cmd 执行并很快终止,无法看到它写入的内容,大概是如果第一行是一个长时间执行的任务,你可以检查这个(令人烦恼的是“暂停”不起作用)。It seems that TeamCity simply sticks what you enter into
a .cmd
fileThe
for
statement requires double percents in these cases.It then seems that TeamCity removes one of these
%
signs, hence why tspauld got it to work (this is also how i got it to run).In the logs it seems that TeamCity creates a file here
Program Files\TeamCity\buildAgent\temp\agentTmp
but the for cmd executes and dies too soon to see what it has written, presumably if the first line was a long executing task you would be able to check this (annoyingly 'pause' didn't work).