How to get latest build ID from Azure DevOps pipeline?

Azure Pipelines is a cloud-based service that allows you to continuously build, test, and deploy your applications. It provides a wide range of features to support continuous integration and continuous delivery (CI/CD) workflows. One of the essential features of Azure Pipelines is the ability to retrieve the latest build ID programmatically. In this blog post, we will explore how to get the latest build ID from an Azure Pipeline.

Firstly, let's understand what a build ID is. A build ID is a unique identifier assigned to each build in Azure Pipelines. It is used to track the status and progress of a particular build. The build ID is generated automatically and is incremented for each new build. It is essential to know the latest build ID to retrieve the artifacts produced by the build or to trigger the next build in the pipeline.

Now let's dive into the steps to get the latest build ID from an Azure Pipeline programmatically:

Step 1: Create a Personal Access Token (PAT)

To retrieve the build ID programmatically, we need to authenticate ourselves with Azure Pipelines. One way to do this is by creating a Personal Access Token (PAT) from Azure DevOps. A PAT is a token that provides secure access to Azure DevOps resources, including Azure Pipelines. To create a PAT, follow these steps:

  1. Go to Azure DevOps and sign in to your account.
  2. Click on your profile picture in the top-right corner and select "Security".
  3. Select "Personal access tokens" from the left-hand menu.
  4. Click on "New Token" and follow the instructions to create a new token.
  5. Ensure that the token has the "Build (read)" scope.

Step 2: Retrieve the latest build ID

To retrieve the latest build ID from an Azure Pipeline, we need to make a REST API call. The API endpoint to get the latest build ID is:

GET https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definitionId}?api-version=7.0-preview.1

With optional parameters:

GET https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definitionId}?branchName={branchName}&api-version=7.0-preview.1

Here, {organization} is the name of your Azure DevOps organization, {project} is the name of your project, and {definitionId} is the ID of your build definition. The latest build ID is returned in the response as the "id" field.

We can use the following PowerShell script to retrieve the latest build ID:

$token = "<PAT>" # Replace with your PAT

$organization = "<Organization>" # Replace with your organization name

$project = "<Project>" # Replace with your project name

$definitionId = <DefinitionId> # Replace with your build definition ID

$uri = "https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definitionId}?api-version=7.0-preview.1"

$response = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization = "Bearer $token"}

$buildId = $response.id

Replace the placeholders with your own values and run the script. The $buildId variable will contain the latest build ID.

In conclusion, retrieving the latest build ID from an Azure Pipeline is a straightforward process that involves creating a PAT and making a REST API call. The build ID is essential for tracking the status and progress of builds and triggering subsequent pipeline stages. By automating the retrieval of the latest build ID, we can streamline our CI/CD workflows and ensure efficient software delivery.

Comments

Popular posts from this blog

How to update build number in Azure DevOps pipeline?

How to install AWS System Manager (SSM) Agent on windows using PowerShell?