Local Build Using/Incrementing Team City Version Number

Jun 18
Posted by

On my current project we have a problem in that one of the assemblies we use requires an expensive license that is per computer so our build server would need it’s own license which we are not doing at this point. Now I need to run our publish deploy script locally which means manually increasing the version number on each build. Something that has been missed before causing a new version to overwrite an old version… which is bad… very bad.

To combat this I started digging and found a way to have my build script use and change the TeamCity build counter remotely using their REST API.

First open up http://[Server]/httpAuth/app/rest/projects/ and find your project you want to work with.

Then open up http://[Server]/httpAuth/app/rest/projects/id:project2 and find the configuration you want to work with.

Then open up http://[Server]/httpAuth/app/rest/buildTypes/bt5/settings/buildNumberCounter to get the current build counter

Here is the C# code I use

    public class TeamCityVersionIncrementer
    {
        private readonly string _buildCounterUri;
        private readonly NetworkCredential _credentials;
        public TeamCityVersionIncrementer()
        {
            _buildCounterUri = “http://[server]/httpAuth/app/rest/buildTypes/bt5/settings/buildNumberCounter”;
            _credentials = new NetworkCredential(“[user]“, “[pass]“);
        }
        public int Increment()
        {
            int originalCounter = GetBuildCounterFromTeamCity();
            UpdateCounter(originalCounter + 1);
            int counterAfterIncrement = GetBuildCounterFromTeamCity();
            if (counterAfterIncrement != originalCounter + 1)
            {
                throw new ApplicationException(“Expected build to be incremented to ” + (originalCounter + 1) + “but was ” + counterAfterIncrement);
            }
            return counterAfterIncrement;
        }
        private int GetBuildCounterFromTeamCity()
        {
            WebRequest webrequest = WebRequest.Create(_buildCounterUri);
            webrequest.Credentials = _credentials;
            webrequest.Method = “GET”;
            WebResponse webResponse = webrequest.GetResponse();
            using (var reader = new StreamReader(webResponse.GetResponseStream()))
            {
                string readToEnd = reader.ReadToEnd();
                return int.Parse(readToEnd);
            }
        }
        private void UpdateCounter(int number)
        {
            byte[] arr = Encoding.UTF8.GetBytes(number.ToString());
            var request = (HttpWebRequest)WebRequest.Create(_buildCounterUri);
            request.Method = “PUT”;
            request.ContentType = “text/xml”;
            request.ContentLength = arr.Length;
            request.Credentials = _credentials;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(arr, 0, arr.Length);
            dataStream.Close();
            var response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode != HttpStatusCode.NoContent)
                throw new ApplicationException(“Unexepected status code: ” + response.StatusCode);
        }
    }

Hope that helps someone in this rare situation!

Filed Under: General

Leave a Reply

Your email address will not be published. Required fields are marked *

*