Conditionally display TeamCity build progress messages

TeamCity has a number of notifications/messages that you can make use of to interact with the build script. One of them is the Build Progress Message which is shown on the project dashboard to provide sort of a UI hint to the user while the build is running, like this example:

image

According to the TeamCity docs, here’s how to construct a build progress message :

##teamcity[progressStart ‘<message>’]
…some build activity…
##teamcity[progressFinish ‘<message>’]

note: (The same <message> should be used for both progressStart and progressFinish)

So if we want to “signal” to the user in what “stage” the build process is in this is what we would use. To take the message above (‘deployment in progress…’) we would echo (or equivalent “signaling’” approach) ##teamcity[progressStart ‘deployment in progress…’] which is then accompanied by the corresponding ##teamcity[progressFinish ‘deployment in progress…’] when that “stage” has completed in the build script.

Background/Problem

However, let’s now say that we would like to have all these messages in our deployment script, which is of course treated as source code, instead of inside TeamCity. That means that we would need a way to output these messages conditionally through our deployment code when it runs in TeamCity…but not otherwise.
That’s what this post is supposed to show you how to accomplish. I’m going to show you how to accomplish this is a console application (.exe) using C# and also with PowerShell.

Let’s begin.

note: just wanna look at the code? Find it @github

Solution

The easy/general approach to finding out if your code executes in a TeamCity environment is to look for certain things that would only exists if TeamCity is installed where the code runs. That usually means trying to read an environment variable. You can use:

  1. A custom (you set it yourself) environment variable.
    image
    Disadvantage – you’d identify only your TeamCity server.
  2. A built-in environment variable (e.g. TEAMCITY_DATA_PATH)
    image
    Advantage – you’d identify any TeamCity server.

I’m going with the latter, generic, approach in this post.

In a Console Application

If you got your own console application to execute the deployment script, this is the approach for you.

1. Try to read the OOB variable

var variable = Environment.GetEnvironmentVariable("TEAMCITY_DATA_PATH", EnvironmentVariableTarget.Machine);

2. Later you can just check if ‘variable’ is null or an empty string…or whatever you’d like to determine if the code executes in a TeamCity context. And if it does…:

Console.WriteLine("##teamcity[progressStart 'deployment in progress...']");
//...etc

note: the complete source code can be found here

OK OK…so now show me how to do that in PowerShell?

	if($Env:TEAMCITY_DATA_PATH) {
		$Script:OnDeploymentStarting = [System.Action] { Write-Host "##teamcity[progressStart 'deployment in progress...']" }
		$Script:OnDeploymentFinished = [System.Action] { Write-Host "##teamcity[progressFinish 'deployment in progress...']" }
	} else {
		$Script:OnDeploymentStarting = [System.Action] { Write-Host "deployment started" }
		$Script:OnDeploymentFinished = [System.Action] { Write-Host "deployment finished successfully" }
	}

# ...further down

	$OnDeploymentStarting.Invoke()

	# do stuff...

	$OnDeploymentFinished.Invoke()

Explain please…

Line 1 – try to read the OOB variable and then I’m setting the delegates to execute later.

Line 11 – Execute the OnDeploymentStarting delegate

Line 15 – Execute the OnDeploymentFinished delegate

 

note: the complete source code can be found here

Easy right…?!

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: