Wednesday, November 30, 2011

Continuous Delivery with psake and TeamCity - Visualizing a pipeline

So far we’ve covered:
  1. Creating a Local Build With psake, Part 1: Compiling
  2. Creating a Local Build with psake, Part 2: Testing
  3. Continuous Delivery with psake and TeamCity - Reusing the Local Build to Create a CI Build
  4. Continuous Delivery with psake and TeamCity - Preparing for a pipeline
  5. Continuous Delivery with psake and TeamCity - Creating a pipeline with artifact dependencies
Today I will show you how to update the build number of a deployment build with the build number of the build it is dependent on.

    So if we were to deploy to test and the build number of the dependent build is, we want to visualize that this build number is actually deployed to test by updating the build number from our build script. One strategy for implementing this is to let the CI build output a file with the build number and publish it to the artifact repository in TeamCity.
    The first thing we have to do is to create a task that outputs a file with the build number:
    task create_build_number_file {
        "$env:build_number"  | out-file "$base_dir\build.number" -encoding "ASCII" -force  
    # Add this task as a dependency to ci
    task ci -depends compile, test, create_build_number_file
    $env:build_number is an environment variable set by TeamCity containing the build number of the running build.
    Next we have to publish this file to the artifact repository. Add build.number to the Artifact paths in TeamCity:


    If we now run the CI build we should see something like the following under the Artifacts tab of the build:


    So far, so good! Next up is to read this file when we deploy to test and set the build number.
    task set_build_number {
        $script:build_no = get-content "$build_artifacts_dir\build.number"
        TeamCity-SetBuildNumber $script:build_no
    # Add this task as a dependency to deploy
    task deploy -depends set_build_number{
        Write-Output "deploying to test!"
    # Make sure to add this line after the properties declarations
    include .\..\tools\psake\teamcity.ps1
    TeamCity-SetBuildNumber is a helper function that is defined in teamcity.ps1 which comes bundled with psake.

    We also have to tell TeamCity to download the build.number file when Deploy to Test is run:


    If we run Deploy to Test the TeamCity dashboard should look similar to this:


    We have now seen how we can visualize which build number is deployed to which environment. Happy days!
    As always you can download the source from GitHub.


    1. Nice work Gøran. Another way to get the build number from the CI build is to use the variable variable. That way you don't have to put it in an artifact.

    2. ... as described here:

      1. Thank for your comments Eirik! I have been following that approach for a while now and should have written an update for this post. Your solution works much better!