In Azure DevOps REST API, Create Release is a specific endpoint within the Azure DevOps API that empowers users to create new releases. Additionally by utilizing this API, developers can seamlessly integrate release management into their CI/CD workflows, automating the process of delivering software updates to various environments.
Azure DevOps REST API
The Azure DevOps REST API is a set of web services provided by Microsoft, allowing users to manage Azure DevOps services programmatically. Additionally, this API enables seamless automation of various aspects of the Azure DevOps platform, encompassing projects, repositories, builds, releases. Moreover, developers can use various tools, such as cURL or other HTTP clients, to interact with the entire stack. Furthermore, the API supports multiple authentication methods, including Personal Access Tokens (PAT), OAuth, and other supported authentication mechanisms. Again, this offers developers the flexibility to choose the most suitable approach based on their scenario and access requirements. Further, if you need more detailed information about the API and its endpoints, you can refer to the official reference available at: https://learn.microsoft.com/en-us/rest/api/azure/devops/.
Release
In Azure DevOps, a release is the process of deploying software changes from one environment to another, such as from development to testing or from testing to production. It involves defining a release pipeline with stages, tasks, and approvals, and using artifacts (build result) to deploy the software. Releases can be automated, ensuring consistent and reliable deployments, and they provide visibility into the deployment status and metrics.
Flow of release creation
Authenticate
Authentication for the API can be achieved using various methods. The most common approaches include Personal Access Tokens (PAT), OAuth, and other supported authentication methods.
$PAT = "<TOKEN>"
$proj = "<Project>"
$org = "<Organisation>"
$apiVersion = "7.0"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$headers = @{Authorization = ("Basic {0}" -f $base64AuthInfo) }
$url = "https://vsrm.dev.azure.com/${org}/${proj}/_apis/release/definitions?api-version=${apiVersion}"
Invoke-RestMethod -Uri $url -Headers $headers -Method Get
Retrieve the release definition you want to create a release for using the GET
method
Get the Release Definition
$name = "<NAME OF PIPELINE>"
$url = "https://vsrm.dev.azure.com/${org}/${proj}/_apis/release/definitions?searchText=${name}&api-version=${apiVersion}"
$definition = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
Create a Release
Use the retrieved release definition to create a new release by making a POST request to the releases endpoint. In the request body, provide the necessary details such as the definition ID, environment ID and buildId (artifacts)
$body = [PSCustomObject]@{
artifacts = @([ordered]@{
alias = "_${alias}"
instanceReference = @{
id = ${buildId}
}
definitionId = $definitionId
manualEnvironments = $manualEnvironments
})
} | ConvertTo-Json -Depth 10
$url = "https://vsrm.dev.azure.com/${org}/${proj}/_apis/release/releases?api-version=${apiVersion}"
Invoke-RestMethod -Uri $url -Headers $headers -Method post -Body $body -ContentType "application/json"
Code - Create Release in Azure DevOps
Here is the working version of the code.
$PAT = "<TOKEN>"
$proj = "<Project>"
$org = "<Organisation>"
$apiVersion = "7.0"
$name = "<NAME OF PIPELINE>"
$manualEnvironments = @("QA")
# create headers
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$headers = @{Authorization = ("Basic {0}" -f $base64AuthInfo) }
# get definition id
$url = "https://vsrm.dev.azure.com/${org}/${proj}/_apis/release/definitions?searchText=${name}&api-version=${apiVersion}"
$definition = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
# get build id
$buildDefinitionId = $definition.id
$url = "https://dev.azure.com/${org}/${proj}/_apis/build/builds?definitions=${buildDefinitionId}&branchName=refs/heads/master&`$top=1&api-version=${apiVersion}"
$buildInfo = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
$buidId = $buildInfo.value[0].id
# create release
$body = [PSCustomObject]@{
artifacts = @([ordered]@{
alias = "_${name}"
instanceReference = @{
id = ${buildId}
}
definitionId = $definitionId
manualEnvironments = $manualEnvironments
})
} | ConvertTo-Json -Depth 10
$url = "https://vsrm.dev.azure.com/${org}/${proj}/_apis/release/releases?api-version=${apiVersion}"
Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $body -ContentType "application/json"
Summary
In this post, I showed you how to create a release using Azure DevOps REST API. This is just an example, and there are many other aspects to consider, such as:
- Triggering: How will the release be triggered?
- Approvals: How to approve before and after deployment?
- Checking the status: How to check status of the release automatically?
- Creating a definition: How to create release pipeline from scratch?
In the future, I may delve into the aforementioned topics.
[…] Create Release […]