Search code examples
c#dockerteamcity-9.0cakebuild

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


I'm trying to access the branch name in Cake on TeamCity running inside a Linux Docker container, but whenever I try to fetch any of the "Configuration Parameters", the values are returning nothing.

In my branch, the following build parameter values are visible on TeamCity:

Configuration parameters

  • vcsroot.branch: refs/heads/master
  • teamcity.build.branch: 5/merge

Environment variables

  • env.vcsroot.branch: 5/merge

The env.vcsroot.branchvariable has a value of %teamcity.build.branch%.

My cake script simply tries to spit out the values, and all of the ones below are coming back empty:

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}");

The actual output:

[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

Oddly enough, on our non-docker Windows-based TeamCity agents, the values seem to return fine. I have a feeling I'm missing something here that's painfully simple, but I'm a relative novice when it comes to Cake, TeamCity, and Docker. Any help would be greatly appreciated. Thanks!

Edit: to claify, most of the environment variables are coming back as expected; the only one that I've noticed that doesn't is the one that references a configuration parameter.


Solution

  • I figured it out...

    First off, I missed the subtext on the TC project parameters page for Configuration Parameters; it states Configuration parameters are not passed into build, can be used in references only.

    Secondly, I didn't realize that none of the System Properties were visible (don't know if that's an issue), but its subtext also states System properties will be passed into the build (without system. prefix), they are only supported by the build runners that understand the property notion.

    Therefore, in order to get the Configuration Parameter value, I needed to create an Environment Variable using the Configuration Parameter as it's value:

    env.TCBranch = %teamcity.build.branch%
    

    It was a little unsettling that teamcity.build.branch didn't show up in the type-ahead when specifying the value, but it works.

    This begs the question about why the Environment Value of env.vcsroot.branch didn't work, and I presume it's because the name of the variable is identical to a different Configuration Variable name. Considering these parameters aren't passed into the build, I don't see why that should matter, but I can't think of why else it wouldn't work. Anyway, thanks to @devlead for the suggestions (above).