Skip to content

Instantly share code, notes, and snippets.

@kevinhillinger
Created August 3, 2018 17:54
Show Gist options
  • Save kevinhillinger/73a15c2a485c1d4af06b322a583451c5 to your computer and use it in GitHub Desktop.
Save kevinhillinger/73a15c2a485c1d4af06b322a583451c5 to your computer and use it in GitHub Desktop.
VSTS REST API Authentication & Trigger Release
Param(
[string]$vstsAccount = "<VSTS-ACCOUNT-NAME>",
[string]$projectName = "<PROJECT-NAME>",
[string]$releaseDefinitionId = "<RELEASE-ID>",
[string]$keepForever = "true",
[string]$user = "",
[string]$token = "<PERSONAL-ACCESS-TOKEN>"
)
Write-Verbose "Parameter Values"
foreach($key in $PSBoundParameters.Keys)
{
Write-Verbose (" $($key)" + ' = ' + $PSBoundParameters[$key])
}
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
# Construct the REST URL to trigger the release
# https://docs.microsoft.com/en-us/rest/api/vsts/release/releases/create?view=vsts-rest-4.1
$uri = "https://$($vstsAccount).visualstudio.com/DefaultCollection/$($projectName)/_apis/release/releases?api-version=4.1-preview.6"
$body = @{
"definitionId" = $releaseDefinitionId;
"description" = "Creating Sample release";
"artifacts" = @(
@{
"alias": "Fabrikam.CI",
"instanceReference": {
"id" = "2";
"name" = $null;
}
}
);
"isDraft" = $false;
"reason" = "none";
"manualEnvironments" = $null;
}
# Invoke the REST call and capture the results
$result = Invoke-RestMethod -Uri $uri -Method POST -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
# This call should only provide a single result
if ($result.count -eq 0)
{
throw "Unable to locate Release Definition for the Id $($releaseDefinitionId)"
}
$releaseId = $result.value[0].id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment