An alternative to setting a build quality on a TFS vNext build

TFS vNext builds do not have a concept of build quality unlike the old XAML based builds. This is an issue for us as we used the changing of the build quality as signal to test a build, or to mark it as released to a client (this was all managed with my TFS Alerts DSL to make sure suitable emails and build retention were used).

So how to get around this problem with vNext?

I have used Tag on builds, set using the same REST API style calls as detailed in my post on Release Management vNext templates. I also use the REST API to set the retention on the build, so I actually now don’t need to manage this via the alerts DSL.

The following script, if used to wrapper the calling of integration tests via TCM, should set the tags and retention on a build

  1  
  2function Get-BuildDetailsByNumber  
  3{  
  4    param  
  5    (  
  6        $tfsUri ,  
  7        $buildNumber,  
  8        $username,   
  9        $password 
 10
 11    )
 12
 13 
 14
 15    $uri = "$($tfsUri)/\_apis/build/builds?api-version=2.0&buildnumber=$buildNumber"
 16
 17 
 18
 19    $wc = New-Object System.Net.WebClient  
 20    if ($username -eq $null)  
 21    {  
 22        $wc.UseDefaultCredentials = $true  
 23    } else   
 24    {  
 25        $wc.Credentials = new-object System.Net.NetworkCredential($username, $password)  
 26    }  
 27    write-verbose "Getting ID of $buildNumber from $tfsUri "
 28
 29    $jsondata = $wc.DownloadString($uri) | ConvertFrom-Json   
 30    $jsondata.value\[0\]  
 31    
 32}
 33
 34 
 35
 36function Set-BuildTag  
 37{  
 38    param  
 39    (  
 40        $tfsUri ,  
 41        $buildID,  
 42        $tag,  
 43        $username,   
 44        $password
 45
 46 
 47
 48    )
 49
 50 
 51
 52    
 53    $wc = New-Object System.Net.WebClient  
 54    $wc.Headers\["Content-Type"\] = "application/json"  
 55    if ($username -eq $null)  
 56    {  
 57        $wc.UseDefaultCredentials = $true  
 58    } else   
 59    {  
 60        $wc.Credentials = new-object System.Net.NetworkCredential($username, $password)  
 61    }  
 62      
 63    write-verbose "Setting BuildID $buildID with Tag $tag via $tfsUri "
 64
 65    $uri = "$($tfsUri)/\_apis/build/builds/$($buildID)/tags/$($tag)?api-version=2.0"
 66
 67    $data = @{value = $tag } | ConvertTo-Json
 68
 69    $wc.UploadString($uri,"PUT", $data)   
 70      
 71}
 72
 73 
 74
 75function Set-BuildRetension  
 76{  
 77    param  
 78    (  
 79        $tfsUri ,  
 80        $buildID,  
 81        $keepForever,  
 82        $username,   
 83        $password
 84
 85    )
 86
 87    
 88    $wc = New-Object System.Net.WebClient  
 89    $wc.Headers\["Content-Type"\] = "application/json"  
 90    if ($username -eq $null)  
 91    {  
 92        $wc.UseDefaultCredentials = $true  
 93    } else   
 94    {  
 95        $wc.Credentials = new-object System.Net.NetworkCredential($username, $password)  
 96    }  
 97      
 98    write-verbose "Setting BuildID $buildID with retension set to $keepForever via $tfsUri "
 99
100    $uri = "$($tfsUri)/\_apis/build/builds/$($buildID)?api-version=2.0"  
101    $data = @{keepForever = $keepForever} | ConvertTo-Json  
102    $response = $wc.UploadString($uri,"PATCH", $data)   
103      
104}
105
106 
107
108  
109\# Output execution parameters.  
110$VerbosePreference ='Continue' # equiv to -verbose  
111
112$ErrorActionPreference = 'Continue' # this controls if any test failure cause the script to stop 
113
114$folder = Split-Path -Parent $MyInvocation.MyCommand.Definition  
115
116write-verbose "Running $folderTcmExec.ps1" 
117
118 
119
120& "$folderTcmExec.ps1" -Collection $Collection -Teamproject $Teamproject -PlanId $PlanId  -SuiteId $SuiteId -ConfigId $ConfigId -BuildDirectory $PackageLocation -TestEnvironment $TestEnvironment -SettingsName $SettingsName write-verbose "TCM exited with code '$LASTEXITCODE'"  
121$newquality = "Test Passed"  
122$tag = "Deployed to Lab"  
123$keep = $true  
124if ($LASTEXITCODE -gt 0 )  
125{  
126    $newquality = "Test Failed"  
127    $tag = "Lab Deployed failed"  
128    $keep = $false  
129}  
130write-verbose "Setting build tag to '$tag' for build $BuildNumber"
131
132 
133
134  
135$url = "$Collection/$Teamproject"  
136$jsondata = Get-BuildDetailsByNumber -tfsUri $url -buildNumber $BuildNumber #-username $TestUserUid -password $TestUserPwd  
137$buildId = $jsondata.id  
138write-verbose "The build $BuildNumber has ID of $buildId"  
139   
140write-verbose "The build tag set to '$tag' and retention set to '$key'"  
141Set-BuildTag -tfsUri $url  -buildID $buildId -tag $tag #-username $TestUserUid -password $TestUserPwd  
142Set-BuildRetension -tfsUri $url  -buildID $buildId  -keepForever $keep #-username $TestUserUid -password $TestUserPwd
143
144 
145
146\# now fail the stage after we have sorted the logging  
147if ($LASTEXITCODE -gt 0 )  
148{  
149    Write-error "Test have failed"  
150}

If all the tests pass we see the Tag being added and the retention being set, if they fail just a tag should be set

image

$ErrorActionPreference = 'Continue'