Atom Feed

Running TeamCity in Azure

In my opinion, TeamCity is the best build server out there. It is user-friendly, easy to set up, although maybe a little resource-heavy.

For a long time we have run TeamCity on-premise, but the costs related to keeping hardware up and running and up to date is taking its toll. Azure provides a very interesting solution with its virtual machines and the option of connecting to our other on-premise resources through a VPN tunnel. Using VMs with SSD disks is also very interesting and it

The setup was less problematic than I expected, just follow the guide below to get set up.

Setting up the server

  1. Create a new Virtual Network in Azure
  2. Create a new SQL Database and call it TeamCity and a new Server called TeamCity-SQL.
    Make sure you set the Collation to be SQL_LATIN1_GENERAL_CP1_CS_AS.
    Also make sure you add it to the same Resource Group and Virtual Network as in step 1).
  3. Create a new Virtual Machine with based on the Windows Server 2012 R2 Datacenter template with D1 orD2 specifications.
    Make sure you add two endpoints - one called HTTP and port 80, and one called HTTPS and port 443.
    Also make sure you add it to the same Resource Group and Virtual Network as in step 1).
  4. When the machine is created, go ahead and add a new disk to it and set the size to a minimum of 20GB. You don't pay for more than you use, so there is no need to be squeemish :-)
  5. When the disk has been created, log onto it using RDP and make a partition from the new disk using Computer Management -> Disk Management and give it the disk letter E:
  6. Open Windows Firewall with Advanced Security and add a new rule called TeamCity and the set the ports to 80,443 and allow for all networks.
  7. Download TeamCity from https://www.jetbrains.com/teamcity/download/ and install only the server component. (You must deselect the Build Agent component) When asked about the port number, use port80
    When you install TeamCity make sure you set it up to install in E:\TeamCity and use E:\TeamCity_Data as the data directory
    When asked about the account to run the service under, use a User account with the credentials you entered in step 3).
  8. A configuration window will open, and you will be able to set the database configuration.
    Download the JDBC driver and extract the sqljdbc4.jar to E:\TeamCity_Data\lib\jdbc
    Go back to the browser window and press refresh. Input the connection info to the server you created in step 2) and press continue and wait until the process is finished.
  9. Download the latest azure cloud plugin from https://teamcity.jetbrains.com/viewType.html?buildTypeId=TeamcityAzurePlugin_Build and place the zip in E:\TeamCity_Data\plugins.
  10. Restart the TeamCity service, and log onto the server.
  11. Open the Administration page on the TeamCity web interface and click on Agent Cloud in the left menu
  12. Create a new profile called Azure. Set Cloud Type to Azure.
    An Idle Time of 30 is in my experience too low. I have set mine to 60 but your environment might require a different value. For the management certificate and subscription id go tohttps://manage.windowsazure.com/publishsettings and download the file for your subscription.
    Open the file with notepad and copy the ManagementCertificate and Id values to their corresponding fields in the browser dialog.
  13. The server is now mostly configured, to continue you need to create a build agent image.

(optional) Running TeamCity on SSL

  1. Get a password-protected certificate for the URL the server will be running on in .pfx format and store it atE:\TeamCity\conf\ssl\cert.pfx
  2. Convert the certificate to a keystore by using the following command:
    E:\TeamCity\jre\bin\keytool.exe -importkeystore -srckeystore E:\TeamCity\conf\ssl\cert.pfx -srcstoretype pkcs12 -destkeystore E:\TeamCity\conf\.keystore2
  3. Edit C:\TeamCity\conf\server.xml and replace the element with the following:

  4. Edit C:\TeamCity\conf\web.xml and add the following just before the closing tag:

  5. Restart the TeamCity service

  6. Update the Server URL under Global Settings on the Administration page to https://{your-server-url}
  7. Your server will now respond on both HTTP and HTTPS but will automatically redirect to HTTPS.

Setting up the build agent

  1. Create a new Virtual Machine based on the Visual Studio Community 2013 in Windows Server 2012 R2template with D1 or D2 specifications.
    Make sure you add it to the same Resource Group and Virtual Network as in previous steps.
  2. When the machine is created, log onto it using RDP and open the firewall settings. Add a new rule calledTeamCity and the set the ports to 9090-9099 (or higher if you want a lot of build agents) and allow for all networks.
  3. Open Local Security Policy and navigate to Local Policies -> User Rights Management -> Log on as a service. Add the user you created in step 1).
  4. Open a browser window and navigate to the url for the TeamCity server. Log in and open the Administration page. Click on the Install Build Agents link.
  5. When the file is downloaded start it and install it to c:\BuildAgent when asked for the root directory.
  6. When asked to specifiy server urls and other directories fill it out like this:
ownPort 9090
systemDir D:\BuildAgent\system
workDir D:\BuildAgent\work
tempDir D:\BuildAgent\temp
env.TEAMCITY_JRE C:\BuildAgent\jre
  1. When asked about the account to run the service under, use a User account with the credentials you entered in step 1).
  2. Make sure the build agent successfully connects to the build server by rebooting the server and see if it automatically connects to the server when its back up.
  3. Install all SDKs, libraries, etc. necessary for running your builds
  4. Stop the TCBuildAgent service.
  5. Delete the content under C:\BuildAgent\logs and C:\BuildAgent\temp
  6. Open the C:\BuildAgent\conf\buildAgent.properties file and make sure the serverUrl, name andauthorizationToken properties are blank.
  7. Follow these instructions to create an image from the build agent.
    When asked about image name, call it something like buildagent_windows_v1 so you can create new ones and still know which one is current :-) (The tag field can be anything)

Making changes to the build agent

When you create an image the original VM is deleted. To make changes (install more SDKs, etc) you need to create a new VM in Azure based on the image.
You can then do your changes and repeat step 9-12 in Setting up the build agent

Final configuration

  1. Open the Administration page on the TeamCity web interface, click on Agent Cloud in the left menu and edit the Azure profile.
  2. Click on Add Image and as source select the image you created earlier.
    For username and password you must use the same as when you created the VM.
    The VM size must be D1 or D2 (or whatever you based your VM on, the important part here is that there must be a disk at D: for the above config to work)
    The number of instances and prefix is entirely up to you.

Final Notes

Starting up a new VM from an image in Azure can take up to 10 minutes, which is a long time if you need to build something in a hurry.
I have solved it by having a few VMs almost always running (They are stopped by TeamCity after the idle time, but not deleted like with build agents created from images. These VMs uses just a few seconds to boot.). To accomplish this you create a new VM based on the image and add them in the AzureTeamCity profile. (They will come up under Machines instead of Images when adding the image).