Category Archives: CI

Automatically pin and tag builds in TeamCity

I’m using TeamCity for both building and deploying my artifacts to the different environments (DEV, CI, PROD, etc…).
I’m also using the daily Build History Clean-up feature to conserve disk space.
This means I might end up deploying to QA for manual testing, by the time the tests are done (in some cases this could take over a couple of days), the cleanup may have already removed the release candidate build (and its artifacts) from the disk, having to compromise between uploading a release that’s not fully tested to delaying the release on account of unplanned testing.

The solution is build pinning, this is equivalent to Jenkins’ “keep this build forever” button.

But you can’t ask a build configuration to pin you build automatically on success , and in my case, I want to pin the build I’m depending on (I have a build config that builds artifacts and other build configurations to deploy those artifacts to the various environments).

It’s pretty simple to use the TeamCity REST API (on a linux box with curl).

buildmanager is a user I’ve created in order to show that the pinning or tagging were done automatically, otherwise it may have used the user of whoever ran the build

Pin this build:

curl -v --basic --user buildmanager:buildmanager --request PUT "http://localhost/TeamCity/httpAuth/app/rest/builds/

Pin the BuildArtifacts build, on which I’m depending:

curl -v --basic --user buildmanager:buildmanager --request PUT "http://localhost/TeamCity/httpAuth/app/rest/builds/

Also tag it ( is a configuration parameter I set on my build configurations that deploy to use the environment name in scripts):

curl -v --basic --user buildmanager:buildmanager --request POST --header "Content-Type: application/xml" --data '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><tags><tag></tag></tags>' "http://localhost/TeamCity/httpAuth/app/rest/builds/"

I’ve simply used these one-liners as custom scripts in a command line type build step at the end of my deployment build configurations.


Formatted Date Parameter – A Plugin for TeamCity

I’ve been using TeamCity for a short while now at my new job. it seemed like there’s nothing it can’t do for me.

I decided I want to get a timestamp generated and incorporated into the build number. However, that seemed a bit more difficult than i thought.

A quick search shows a couple of options:

  • Date Build Number Plugin
    • It sets the entire build number to the timestamp, so you can’t put anything else there, like the VCS revision, build counter or Maven version number.
    • Installation is kinda funky, having to dilddle with TC configuration files.
  • Groovy Plug
    • This plugin provides the build start timestamp as a parameter, which is great
    • The timestamp format isn’t easily configurable
    • Installation is again non-standard
    • The plugin notes say it’s a bit of a memory hog, and it was meant as a demo anyway

So I just wrote my own.

Formatted Date Parameter provides a confguration parameter (named build.formatted.timestamp), which during build will contain the build start timestamp. The timestamp format is ISO-1806 by default (“yyyy-MM-dd’T’HH:mmZ”). The timestamp format can be configured using another configuration parameter (named build.timestamp.format), which uses standard SimpleDateFormat syntax.


Just like the guide says

  • Copy the zip (see link at bottom of page) to the .BuildServer/plugins dir (for me it was /home/username/.BuildServer/plugins)
  • Restart the Tomcat instance your TeamCity WAR is deployed to.



In my case, I wanted the week-in-year (01-52) to appear in the build number, so I

  1. Added build.formatted.timestamp to the build number format field (build configuration -> general settings page)
    using the %build.formatted.timestamp% parameter in the build number field
    using the %build.formatted.timestamp% parameter in the build number field


  2. Then I set the value of build.timestamp.format to show only the week number in 2 digits “ww” (build configuration -> build parameters page -> Add configuration parameter button)
    Setting the timestamp format
    Setting the timestamp format


  3. Now my build number contains the current week number. Hurray.



FormattedDateParameter v1.1 plugin for TeamCity (download) (source on github)