Boosting Your DevOps Workflow with Azure DevOps REST API
Mohamed ElEmam
DevOps Manager |Driving Digital Transformation ?? |OpenShift, Kubernetes, Automation, CI/CD and DevSecOps Expert |Cloud Architect (Azure, AWS)
Azure DevOps is a comprehensive set of services designed for DevOps teams to plan, collaborate, build, test, deploy and monitor their applications. The platform offers a wide range of functionalities that enable developers to streamline their processes and automate their workflows. One of the key features of Azure DevOps is its REST API, which provides developers with a powerful way to interact with the platform programmatically. In this article, we will explore the Azure DevOps REST API and how it can be used in Azure DevOps pipelines.
What is Azure DevOps REST?API?
The Azure DevOps REST API is a set of web services that allow developers to programmatically interact with Azure DevOps services, such as Work Items, Builds, Releases, Test Plans, and more. The API is based on the HTTP protocol and uses a simple request-response model to exchange data with Azure DevOps services. It supports both JSON and XML formats for data serialization and provides a wide range of functionality for developers to automate their workflows.
How to use Azure DevOps REST API in Azure DevOps Pipelines?
Azure DevOps Pipelines is a powerful CI/CD tool that allows developers to automate their build, test, and deployment processes. It provides a rich set of pre-defined tasks that can be used to build and deploy applications to various platforms, including Azure, AWS, and GCP. However, sometimes these pre-defined tasks may not be sufficient to meet your specific requirements, and you may need to extend the pipeline functionality by using custom scripts or code.
This is where the Azure DevOps REST API comes in handy. By using the API, you can programmatically interact with Azure DevOps services and automate your workflows. Here are some common scenarios where the API can be used in Azure DevOps Pipelines:
To use the Azure DevOps REST API in Azure DevOps Pipelines, you need to create a service connection that allows your pipeline to access Azure DevOps services. You can create a service connection by following these steps:
1. Go to your Azure DevOps project settings and select Service connections.
2. Click on New service connection and select the Azure DevOps option.
3. Follow the instructions to authenticate and authorize the service connection.
Once you have created the service connection, you can use it in your pipeline by adding a task that executes a custom script or code that interacts with the Azure DevOps REST API. Here’s an example of how to use the API to trigger a new build:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/build/builds?api-version=6.1-preview.6"
$pat = "$(system.accesstoken)"
$body = @{
definition = @{
id = $(Build.DefinitionId)
}
} | ConvertTo-Json -Depth 99
$headers = @{
Authorization = "Bearer $pat"
'Content-Type' = 'application/json'
}
Invoke-RestMethod -Uri $url -Method POST -Headers $headers -Body $body
In this example, we are using the PowerShell task to execute a custom script that triggers a new build using the Azure DevOps REST API. The script first constructs the API URL and includes the API version to use. Then, it retrieves the personal access token (PAT) provided by Azure DevOps and constructs the request body in JSON format, including the ID of the build definition to trigger. Finally, it sets the request headers and uses the Invoke-RestMethod cmdlet to send a POST request to the API endpoint and trigger the new build.
领英推荐
Azure DevOps API HTTP?Methods
There are some differences between the HTTP methods used by the Azure DevOps REST API and standard HTTP methods. Here’s a brief overview of the HTTP methods supported by the Azure DevOps REST API:
Keep in mind that the Azure DevOps REST API uses a specific URL format for its API endpoints, which includes the organization and project names, as well as the API version. For example, the URL for retrieving information about a Work Item might look like this:
https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=6.1
In this URL, the {organization} and {project} placeholders are replaced with the actual names of the Azure DevOps organization and project, and the {id} placeholder is replaced with the ID of the specific Work Item you want to retrieve.
In summary, while the Azure DevOps REST API uses many of the same HTTP methods as standard HTTP, there are some differences, such as the use of PATCH instead of DELETE for deleting data. Understanding these differences and the specific URL format used by the Azure DevOps REST API is essential for using the API effectively in Azure DevOps Pipelines.
Conclusion
Azure DevOps REST API is a powerful tool that enables developers to interact with Azure DevOps services programmatically. It provides a wide range of functionalities that can be used to automate workflows in Azure DevOps Pipelines. By using custom scripts or code that interacts with the API, developers can extend the pipeline functionality and meet their specific requirements.
Finally, I hope this tutorial being useful to you, for any questions feel free to comment, or contact me directly on [email protected]
Link on Medium