在 Cake (C#) returns 中获取 TeamCity (Docker Linux) 参数或系统属性

Getting TeamCity (Docker Linux) parameters or system properties in Cake (C#) returns nothing

我试图在 Linux Docker 容器内访问 TeamCity 运行 上 Cake 中的分支名称,但每当我尝试获取任何 "Configuration Parameters",这些值 return 什么都没有。

在我的分支中,以下构建参数值在 TeamCity 上可见:

配置参数

环境变量

env.vcsroot.branch变量的值为 %teamcity.build.branch%

我的蛋糕脚本只是试图吐出值,下面的所有值都返回空值:

var branch = EnvironmentVariable("vcsroot.branch");
var tcbranch = EnvironmentVariable("teamcity.build.branch");
var agent = EnvironmentVariable("system.agent.name");
var confName = EnvironmentVariable("system.teamcity.buildConfName");
var buildId = EnvironmentVariable("teamcity.build.id");
var vcsRootBranch = EnvironmentVariable("vcsroot.Root_TemplatedVcsRoot1.branch");
var argOrEnv = ArgumentOrEnvironmentVariable("teamcity.build.branch", "vcsroot.branch", "Unfound");

Information($"vcsroot.branch = {branch}");
Information($"teamcity.build.branch = {tcbranch}");
Information($"system agent name = {agent}");
Information($"system TC build cof name= {confName}");
Information($"param buildId = {buildId}");
Information($"vcsroot template branch = {vcsRootBranch}");
Information($"test argument or env variables = {argOrEnv}");

实际输出:

[12:34:51][Step 1/2] vcsroot.branch = 
[12:34:51][Step 1/2] teamcity.build.branch = 
[12:34:51][Step 1/2] system agent name = 
[12:34:51][Step 1/2] system TC build cof name= 
[12:34:51][Step 1/2] param buildId = 
[12:34:51][Step 1/2] vcsroot template branch = 
[12:34:51][Step 1/2] test argument or env variables = Unfound

奇怪的是,在我们非 docker Windows 的 TeamCity 代理上,这些值似乎 return 很好。我有一种感觉,我在这里遗漏了一些非常简单的东西,但在 Cake、TeamCity 和 Docker 方面,我是一个相对的新手。任何帮助将不胜感激。谢谢!

编辑:澄清一下,大多数环境变量都按预期返回;我注意到唯一没有引用配置参数的参数。

对于环境变量,TeamCity 将非字母数字字符替换为“_”

vcsroot.branch 变为 vcsroot_branch

我想通了...

首先,我错过了配置参数的 TC 项目参数页面上的潜台词;它指出 Configuration parameters are not passed into build, can be used in references only.

其次,我没有意识到 none 的系统属性是可见的(不知道这是否是一个问题),但它的潜台词也指出 System properties will be passed into the build (without system. prefix), they are only supported by the build runners that understand the property notion.

因此,为了获取配置参数值,我需要使用配置参数作为其值创建一个环境变量:

env.TCBranch = %teamcity.build.branch%

有点令人不安的是 teamcity.build.branch 在指定值时没有出现在预先输入中,但它确实有效。

这引出了为什么 env.vcsroot.branch 的环境值不起作用的问题,我认为这是因为变量的名称与不同的配置变量名称相同。考虑到这些参数没有传递到构建中,我不明白为什么这很重要,但我想不出为什么它不起作用。无论如何,感谢@devlead 的建议(以上)。