Boosting Your DevOps Workflow with Azure DevOps REST API

Boosting Your DevOps Workflow with Azure DevOps REST API


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:

  1. Create Work Items: You can use the Azure DevOps REST API to create Work Items programmatically. For example, you can create a custom script that creates a Work Item for every new bug that is reported by your application.
  2. Update Build Definitions: You can use the Azure DevOps REST API to update Build Definitions programmatically. For example, you can create a custom script that updates the build definition every time a new code commit is pushed to the repository.
  3. Query Work Items: You can use the Azure DevOps REST API to query Work Items programmatically. For example, you can create a custom script that queries all the Work Items that are assigned to a particular user.
  4. Trigger Builds: You can use the Azure DevOps REST API to trigger builds programmatically. For example, you can create a custom script that triggers a new build every time a new code commit is pushed to the repository.
  5. Create Releases: You can use the Azure DevOps REST API to create Releases programmatically. For example, you can create a custom script that creates a new release every time a new build is completed successfully.

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:

  • GET: This method is used to retrieve data from Azure DevOps services. For example, you can use the GET method to retrieve information about a Work Item or a Build Definition.
  • PUT: This method is used to update data in Azure DevOps services. For example, you can use the PUT method to update the details of a Work Item or a Build Definition.
  • POST: This method is used to create new data in Azure DevOps services. For example, you can use the POST method to create a new Work Item or a new Build Definition.
  • DELETE: This method is not used by the Azure DevOps REST API. Instead, the API uses the PATCH method to delete data from Azure DevOps services. This is because deleting data in Azure DevOps services is often a complex operation that involves updating multiple related resources, and the PATCH method allows for more granular control over the deletion process.
  • PATCH: This method is used to update specific fields or properties of an existing resource in Azure DevOps services. For example, you can use the PATCH method to update the status of a Work Item or to delete a specific field from a Work Item.

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

要查看或添加评论,请登录

Mohamed ElEmam的更多文章

  • Deploy a Highly Available Kubernetes Cluster using Rancher RKE

    Deploy a Highly Available Kubernetes Cluster using Rancher RKE

    Rancher has developed multiple tools which can make your life easier in handling K8s infrastructure. There is a nice UI…

    5 条评论
  • What is Software Testing and What is SonarQube ?!

    What is Software Testing and What is SonarQube ?!

    What is software testing? Software testing is a part of the software development life cycle, it's aim is to ensure that…

    5 条评论
  • Backup and restore Gitlab in docker

    Backup and restore Gitlab in docker

    Gitlab hosts everything about the code including the docs and the pipeline data, etc. It’s crucial to back it up.

    2 条评论
  • GitLab Container/Docker Registry

    GitLab Container/Docker Registry

    Today we are going to talk about GitLab Container Registry GitLab Container Registry is a secure and private registry…

  • How to Configure GitLab Webhook in Jenkins ??!

    How to Configure GitLab Webhook in Jenkins ??!

    What is a webhook? A Webhook is a mechanism to automatically trigger the build of a Jenkins Project upon a commit…

    3 条评论
  • Create View Only User on Kubernetes Cluster

    Create View Only User on Kubernetes Cluster

    Create View Only User on Kubernetes Cluster This is how to create view only user on all resources of k8s cluster First:…

  • Devops ?!

    Devops ?!

    DevOps is a buzzword in recent times and lot of people and organizations are using it frequently. There is no single…

  • Jeff Dean: Google's Hero

    Jeff Dean: Google's Hero

    You probably haven’t heard of Jeff Dean. But type his name into a search engine like Google and you’ll quickly discover…

  • Jeff Dean

    Jeff Dean

    ??? ????? Jeff Dean ??? ????? ??????? Google ?????? ???? ???? Google ???? ???????? ?????? ????? ??? ???? ???? ??? ????…

  • SED/AWK Basics

    SED/AWK Basics

    SED/AWK – Add to the End or Beginning of a Line From time to time it is required to modify some file very fast. And it…

社区洞察

其他会员也浏览了