Introduction
I recently stumbled on a problem that required the ability to trigger a release outside the project and even outside our on-premises Azure DevOps Server. To tackle this, I created a custom build/release task that enables creating a release in another Azure DevOps Server farm, project collection, Azure DevOps.
Solution
The solution is based on the REST API that comes with Azure DevOps Server or is available in Azure DevOps. This API is described here. We will use two methods:
- get release definitions
- create a release.
We use a personal access token to authenticate the calls to the API. I explain how to create a personal access token in my blog post: Custom build/release management task to execute a SQL script.
Encode the personal access token:
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $personalAccessToken)))
The URI definition for Azure DevOps:
$uri = "https://$($vstsAccount).vsrm.visualstudio.com/DefaultCollection/$($projectName)/_apis/release/definitions?api-version=3.0-preview.2"
And for Azure DevOps Server Farm:
$uri = "$($TFSUri)/$($projectName)/_apis/release/definitions?api-version=3.0-preview.2"
Invoke the REST call and capture result:
$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
From the list of definitions, we will select the right one by filtering by name, which we ask for in the task setup. We store the ID in the variable $releaseID.
Once a release definition has been selected, we create a new release by using the second method, giving a body with no artefact description:
$body = '{ "definitionId": ' + $releaseID + ', "description": "' + $releaseDescription + '" }'
The variable $releaseDescription comes from the task setup page.
To start the release we call another REST method. For Azure DevOps:
$uri = "https://$($vstsAccount).vsrm.visualstudio.com/DefaultCollection/$($projectName)/_apis/release/releases?api-version=3.0-preview.2"
And for Azure DevOps Server Farm:
$uri = "$($TFSUri)/$($projectName)/_apis/release/releases?api-version=3.0-preview.2"
The method is invoked like this:
$releaseresponse = Invoke-RestMethod -Method Post -ContentType application/json -Uri $uri -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $body
Using the custom task
The task can be used in a build or release definition.
If you select Visual Studio Team Services, you will need to enter an Azure DevOps Account Name, i.e. the part between “https://” and “.visualstudio.com”.
If Azure DevOps Server is used, you will need to enter the full URI to the Azure DevOps Server Farm instance, including protocol, port and project collection name. For example:
or
The Release Description field is used to enter a comment on the release.
You can find the complete sources of this solution on GitHub. To find out how to install this solution in your own environment, you can read my blog post Custom build/release management task to execute a SQL script.