How to set the build quality during your release with Release Management

I was recently asked the following question. 

"I would like the build quality to be set by the RMS stage, for example, once a the Test stage is complete the build quality will be set to "Ready for Initial Test". Ultimately, if the build is released to production, I'd like the build quality set to "Released" the the build set to Retain Indefinitely."

I thought this was a very cleaver idea and set out to help.  I already blogged how to set the Retain Indefinitely flag here. So all we need is a PowerShell script to allow us to set the build quality. If you are using the agent based deployment you can add the AgentBasedSetBuildQualityPowerShell script as a custom tool. If you are using the vNext based deployment you can simply execute the vNextSetBuildQuality PowerShell using the PS/DSC action.  When using a vNext based deployment the $tfsUrl, $teamProject and $buildDefinition values are automatically passed to all PowerShell scripts. When using the agent based solution you will need to add those as parameters when you add the PowerShell as a custom tool.  You can then use the Helper Variables provided by Release Management to pass in the correct values.  You can see an example of adding an agent based custom tool here.

vNextSetBuildQuality.ps1

function set-BuildQuality

{

    [CmdletBinding()]

       param(

    [string] $buildQuality

    )

 

    # The machine you use to execute this PowerShell must have Team Explorer installed

    # so that we can load the needed assemblies.

    # The [void] stops the GAC messages from being output to screen.

    Write-Verbose "Loading assemblies"

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

 

    # Connect to TFS

    Write-Verbose "Connecting to $tfsUrl"

    $teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUrl)

 

    # Get the build service

    $buildServer = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Build.Client.IBuildServer")

 

    # Find the build used for this release

    $buildSpec = $buildServer.CreateBuildDetailSpec($teamProject, $buildDefinition)

 

    # Don't query any additional information i.e. associated work items, associated changesets, etc.

    $buildSpec.InformationTypes  = $null

 

    # Use the build number to find the build

    $buildSpec.BuildNumber = $buildNumber

 

    # Search for the build

    $results = $buildServer.QueryBuilds($buildSpec)

 

    # Make sure we only found a single build

    if($results.Builds.Count -eq 1)

    {

        Write-Verbose "Build Found"

        $build = $results.Builds[0]

        $build.Quality = $buildQuality

        $build.Save()

 

        Write-Verbose "Build Quality set to $buildQuality"

    }

}


AgentBasedSetBuildQuality.ps1 (1.9KB)

Comments (7) -

  • Hi Donovan,
    Thanks for a great blog!
    I have problems getting this to work, I get the folowing error:
    Unable to find type [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]: make sure that the assembly containing this type is loaded.
    Team Explorer is installed on the server. To you have any suggestions?
    /Magnus
    • Make sure you have Team Explorer on the machine that is executing your task.  The TFS dlls are installed with Team Explorer. If is is not on the machine they will not be found.
  • Hi Donovan,
    Thanks for your quick answer. Team Explorer is installed on the machine. I guess it must be something else then. I will continoue to look.
    /Magnus  
  • Your AgentBasedSetBuildQuality.ps1 script has a couple of easily overlooked errors. I corrected them, followed your guide on adding an agent based tool, and it works perfectly. Thank you. This has made my life so much easier because our developers no longer have to ask me what stage what build is in, they can just check the queue.

    I'll be following your "retain QA builds indefinitely guide" next.

    For the curious, this is the corrected script for agent based releases:

        [CmdletBinding()]
      param(
        [string] $buildQuality,
        [string] $tfsUrl = $(Throw 'TFS Url is required'),
        [string] $teamProject = $(Throw 'Team Project is required'),
        [string] $buildDefinition = $(Throw 'Build Definitioin is required'),
        [string] $buildNumber = $(Throw 'Build Number is required')
        )

        Write-Verbose "Loading assemblies" -verbose
        [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Client")
        [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.TeamFoundation.Build.Client")

        Write-Verbose "Connecting to TFS" -verbose
        $teamProjectCollection = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($tfsUrl)

        $buildServer = $teamProjectCollection.GetService([type]"Microsoft.TeamFoundation.Build.Client.IBuildServer")

        $buildSpec = $buildServer.CreateBuildDetailSpec($teamProject, $buildDefinition)

        $buildSpec.InformationTypes  = $null

        $buildSpec.BuildNumber = $buildNumber

        $results = $buildServer.QueryBuilds($buildSpec)

        if($results.Builds.Count -eq 1)
        {
            Write-Verbose "Build Found" -verbose
            $build = $results.Builds[0]
            $build.Quality = $buildQuality
            $build.Save()
            Write-Verbose "Build Quality set to $buildQuality" -verbose
        }
  • I'm trying to run this on a clean "vNext" build server as a "PowerShell step" but the script cannot load the assemblies... Comparing with other build servers I have access to it seems like your script would require VS2013 (assemblies appear to be GAC:ed on those machines).

    I'm running VS2015 Enterprise, PowerShell 4.0 and Build agent version 1.83.2 on Win2012R2.

    Looking at various VS2015 installs there does not seem to be any real consistency as to where the various .dll's are located making it difficult to rewrite your script and load the assemblies using an absolute path... Any suggestions on how to solve this?
    • I am going ping a buddy of mine to help.  
  • Any update on this? I have tried both scripts, and not working for me.

Pingbacks and trackbacks (1)+

Add comment

Loading