|
<# |
|
|
|
.SYNOPSIS |
|
|
|
Adds a tag or tags to a TFS/VSTS build. |
|
|
|
.DESCRIPTION |
|
|
|
This script adds a tag or tags to a specific build in TFS/VSTS. Authentication can be performed using the current users credentials, a personal access token, or system OAUTH. |
|
|
|
.INPUTS |
|
|
|
No objects can be piped to this script. |
|
|
|
.OUTPUTS |
|
|
|
No objects are output by this script. |
|
|
|
.PARAMETER TeamFoundationCollectionUri |
|
|
|
The URI of the team foundation collection. For example: https://fabrikamfiber.visualstudio.com/. This defaults to the current team foundation collection. |
|
|
|
.PARAMETER TeamProject |
|
|
|
The name of the team project that contains the build to add tags to. This defaults to the current project. |
|
|
|
.PARAMETER BuildId |
|
|
|
The ID of the build to add tags to, this will default to primary artifact build id. |
|
|
|
.PARAMETER Tags |
|
|
|
A list of tags, separated by a comma (,). |
|
|
|
.PARAMETER PersonalAccessToken |
|
|
|
The personal access token used to authenticate with TFS/VSTS. If omitted, the script will attempt to use the system OAUTH token or default credentials. |
|
|
|
.PARAMETER UseDefaultCredentials |
|
|
|
Uses the credentials of the current user to make the request. |
|
|
|
.EXAMPLE |
|
|
|
# Set the 'Test Complete' and 'Ready' tags for build 988. |
|
|
|
.\Add-TfsBuildTags.ps1 -BuildId 988 -Tags "Test Complete,Ready" |
|
|
|
.NOTES |
|
|
|
Author: Jeff Block <jeff.block@nosnitor.net> |
|
About: http://www.rcsit.com/2017/07/15/adding-tags-to-a-vsts-tfs-build/ |
|
Location: https://gist.github.com/jsblock78/23bfaf48ad5cf0f35a28022f416aa14c |
|
|
|
#> |
|
|
|
[CmdletBinding()] |
|
Param ( |
|
[string] $TeamFoundationCollectionUri = $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI, |
|
[string] $TeamProject = $env:SYSTEM_TEAMPROJECT, |
|
[int] $BuildId = $env:BUILD_BUILDID, |
|
[string] $Tags = "", |
|
[string] $PersonalAccessToken = "", |
|
[switch] $UseDefaultCredentials |
|
) |
|
|
|
# Validate parameters |
|
If ([string]::IsNullOrEmpty($TeamFoundationCollectionUri)) { |
|
Throw "TeamFoundationCollectionUri parameter cannot be null or empty." |
|
} |
|
|
|
If ([string]::IsNullOrEmpty($TeamProject)) { |
|
Throw "TeamProject parameter cannot be null or empty." |
|
} |
|
|
|
If ($BuildId -eq 0) { |
|
Throw "BuildId parameter must be supplied." |
|
} |
|
|
|
If (!([string]::IsNullOrEmpty($PersonalAccessToken)) -and ($UseDefaultCredentials -eq $true)) { |
|
Throw "PersonalAccessToken and UseDefaultCredentials parameters can not both be supplied." |
|
} |
|
|
|
Write-Verbose "TeamFoundationCollectionUri : $TeamFoundationCollectionUri" |
|
Write-Verbose "TeamProject : $TeamProject" |
|
Write-Verbose "BuildId : $BuildId" |
|
$baseUrl = "$TeamFoundationCollectionUri/$TeamProject/_apis" |
|
|
|
|
|
If ([string]::IsNullOrEmpty($Tags)) { |
|
Throw "Tags parameter cannot be null or empty." |
|
} |
|
$tagsArray = $Tags.Split(","); |
|
|
|
# Auth |
|
$token = "" |
|
$headers = $null |
|
If (!($UseDefaultCredentials)) { |
|
If (!([string]::IsNullOrEmpty($PersonalAccessToken))) { |
|
Write-Verbose "Using personal access token." |
|
$creds = ":$($PersonalAccessToken)" |
|
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($creds)) |
|
$token = "Basic $encodedCreds" |
|
$headers = @{Authorization = $token} |
|
} |
|
ElseIf (!([string]::IsNullOrEmpty($env:System_AccessToken))) { |
|
Write-Verbose "Using system access token." |
|
$token = "Bearer $($env:System_AccessToken)" |
|
$headers = @{Authorization = $token} |
|
} |
|
Else { |
|
$UseDefaultCredentials = $true |
|
} |
|
} |
|
Write-Verbose "BaseUrl: [$baseUrl]" |
|
Write-Verbose "Token: [$token]" |
|
|
|
If ($tagsArray.Count -gt 0) { |
|
ForEach($tag in $tagsArray) |
|
{ |
|
$tagUrl = "$baseUrl/build/builds/$BuildId/tags/$tag`?api-version=2.0" |
|
$response = Invoke-RestMethod -Uri $tagUrl -Method Put -Headers:$headers -UseDefaultCredentials:$useDefaultCredentials |
|
Write-Verbose "Response:`r`n$response" |
|
If ($response.GetType().Name -eq "String") { |
|
If ($response.Contains("Visual Studio Team Services | Sign In")) { |
|
Throw "Unable to authenticate using supplied authorization." |
|
} |
|
} |
|
} |
|
} |
Trackback: http://www.rcsit.com/2017/07/15/adding-tags-to-a-vsts-tfs-build/