Setting up an Azure TeamCity buildagent with Chocolatey
Adding some Chemical X to the mix
Yay! TeamCity now supports Azure! The ticket I submitted two years ago finally got the Fixed status. At eFocus, we worked with a virtual machine in the cloud which we configured manually for a few years. A major drawback is that the buildagent is inactive for long periods of time which is costing us money. One A2 VM monthly costs are $110. With an average use of 15%, we’re losing $1122 per year.

So yeah, we’re pretty excited. But after reading the documentation of the TeamCity Azure plugin I thought it could be automated a bit more.
Chocolatey
Have you heard about Chocolatey? If you love NuGet, you’ll love Chocolatey. Their website describes it as:
“Chocolatey NuGet is a Machine Package Manager, somewhat like apt-get, but built with Windows in mind.”
Let me tell you what it can do for you without reciting the documentation on their website: After installing Chocolatey (which takes about 1 minute) you can, for instance, install skype on your machine with just one line of code:
choco install skype
Check out the website to see which software and commands are available.
Chocolatey is therefore a very handy tool for situation where you have to install a lot of software. This is mostly done by hand until now, but this will not be the case for long. PowerShell OneGet is on its way.
Because it is still in development, I am going to focus on just Chocolatey for now but eventually it should be possible to switch to OneGet all together.
So how can Chocolatey help us with using TeamCity with Azure build agents?

The blog post announcing this long awaited plugin states that everything should be installed on the Azure VM before you can use it. This is something you could automate! And fortunately among the available Chocolatey packages there is a TeamCity buildagent.
How does it work?
I’ve created a simple PowerShell script which installs Chocolatey first and after that all the software you need to make it possible to run your builds.
The script
#install Chocolatey
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
TeamCity is built on Java, so the buildagent software won’t run without it
#without Java, the TeamCity agent can not be started
choco install javaruntime -force
After Java, the build agent application can be installed. The installer will be fetched from the TeamCity server so it is necessary that the build agent can access it via the serverUrl variable you specified.
#building parameters
$params = "serverUrl=$serverUrl" #serverUrl is mandatory
if($agentDir){
$params = [string]::Concat($params, " agentDir=", $agentDir)
}
if($agentName){
$params = [string]::Concat($params, " agentName=", $agentName)
}
if($ownPort -ne $null -and $ownPort -gt 0){
$params = [string]::Concat($params, " ownPort=", $ownPort)
}
#install TeamCity build agent
choco install TeamCityAgent -force -params "$params"
Now you can install all the other software that is required for your buildagent:
#install .NET framework
choco install DotNet4.5
#install … and so on
You can download the PowerShell script here.
Running the script
Windows offers the ability to run PowerShell scripts on startup.
- Open mmc.exe
- Add/Remove Snap-In: Group Policy Object - Local Computer
- Go to Computer Configuration/Windows Settings/Scripts
- Open Startup by double clicking
- Place the ps1 file in C:\Windows\System32\GroupPolicy\Machine\Scripts\Startup (just use the Show Files… button)
- Add the script in the Startup Properties window with the following parameters: -NoProfile -ExecutionPolicy Unrestricted -serverUrl “url” -agentDir “folder” -agentName “name” -ownPort 9090
- Choose to run the Windows PowerShell scripts first
Everytime the machine starts, the PowerShell script will run. When software is already installed, Chocolatey will not install it again by default. However I would recommend to only use this solution when you want to create/destroy VM instances in Azure.
Future steps
Adding a startup PowerShell script to a Azure VM would be awesome. After some research I did not find a solution for that. At this moment you still need to execute a few steps manually and create a VM template. I think the following two options could be a solution for that ‘problem’:

- Ability to add a PowerShell startup script when creating a new VM in Azure
- Worker Role support in the TeamCity Azure plugin. You could add the PowerShell script as a startup task. There a few more perks with this one, such as auto scaling and costs.
Nonetheless I think this is a nice solution. Be sure to read the Chocolatey documentation for all features such as installing by packages.config file. Oh, and don’t forget to fund Chocolatey’s kickstarter project if you like it.
If you have any comments or suggestions, please feel free to leave a comment!