1

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.

ChoNuff
  • 814
  • 6
  • 12

2 Answers2

2

For Environment variables TeamCity replaces non alpha numeric characters with "_"

I.e vcsroot.branch becomes vcsroot_branch

devlead
  • 4,935
  • 15
  • 36
  • You know, I had seen this listed, but thought (for some reason) it only applied to powershell. Either way, I tried converting the variable names to use the underscores, to no avail. Thanks for the suggestion, though! – ChoNuff Oct 10 '18 at 19:38
  • You could use EnvironmentVariables alias to list all keys available https://cakebuild.net/api/Cake.Common/EnvironmentAliases/97336C76 – devlead Oct 10 '18 at 19:53
  • Yeah, that's a great idea...I'll try that now. Thanks again! – ChoNuff Oct 10 '18 at 20:03
  • Oh man, I'm an idiot...I forgot to mention that most of the environment vars are returning valid values, just not the one that references a configuration parameter. I've edited the orig post to reflect that. Anyway, it returned a bunch of our env vars with valid values; unfortunately, none of them can be used to determine the branch. I'll see if there's a way to grab the parameters, but I'm not holding my breath. – ChoNuff Oct 10 '18 at 20:12
1

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).

ChoNuff
  • 814
  • 6
  • 12