DevOps | Scripts | Automation

Azure DevOps

How to run ADO Pipeline using Rest API?

Overview

Hello Everyone,

This article aims to run the Azure DevOps (ADO) pipeline using the Rest API. This article will use Microsoft documentation for the POST method to run the pipeline. There are some pre-requisite before we move further.

  • ADO account (Create a free one from the DevOps site if you don’t have one)
  • Knowledge of creating and running basic Azure Pipelines.
  • Postman software should be installed on your laptop.

We will run the Azure pipeline using REST API via PostMan software and later we can convert code as per your required languages.

In this article, we will cover

  1. Create and Run Azure Pipeline
  2. How to use Rest API in Postman?
  3. Generate Personal Access Token (PAT)
  4. Configure PAT in Postman
  5. Convert Postman query to code (Powershell, python, etc).

Alright, let’s begin.

Create and Run Azure Pipeline

We have created the below one test azure pipeline which will call the PowerShell task to print the statement.

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- None

pool:
  vmImage: ubuntu-latest

steps:
  - task: PowerShell@2
    inputs:
      targetType: 'inline'
      script: 'Write-Host "Rest-API call is successful"'
    displayName: RestAPI task

The output of the above-triggered pipeline.

Pipeline task

Use Rest-API in Postman

Ok, now the first mission is successful of creating a pipeline and running it. Now let’s browse through the Microsoft documentation to run the pipeline using RestAPI.

https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/run-pipeline

We need to utilize the very first POST method to trigger the pipeline. As I’m writing this article, the current API version is 7.0 which may be changed in near future.

POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=7.0

Alright, now we need to provide parameters that are covered in curly braces to run the pipeline using rest api,

ADO Organization and Project.
  • Organization: Name of the organization. In my project, chiragnagrekar is the organization.
  • Project: TheAutomationCode is the project name.
  • PipelineId: You will get the pipeline ID or the build definition ID from the URL once you open the pipeline. In the below image, the pipeline ID is 3.
Pipeline ID

So our Post request forms this way.

https://dev.azure.com/chiragnagrekar/TheAutomationCode/_apis/pipelines/3/runs?api-version=7.0

Let’s open the PostMan software and run the query and check if that is sufficient to run the pipeline. Make sure to select POST method and click on send.

PostMan output

If you see the output says “Azure DevOps Services | Sign In” that says the Azure resources are not authenticated to run. So we need to pass the authentication in the PostMan software. Your organization might have multi-factor authentication enabled or may not allow direct login to third-party software.

In that case, Personal Access Token (PAT) is the best way for authentication.

Generate PAT (Personal Access Token)

PAT or personal access tokens are critical and used for Azure resources authentication. For more information about PAT check the MS website

https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=Windows

Let’s create one for our project.

Click on the User Access Settings on the right side of your Azure DevOps Webpage and click on “Personal Access Tokens“.

Personal Access Tokens

Click on the + New Token if you don’t have the existing one and you will be redirected to the New Personal Token page.

You need to verify your organization, set the expiry date (Default is 30 days), and Scope.

The scope you can define custom like only providing access to Pipeline or work items, Wiki, etc but for the demo purpose we are providing full access.

New Personal Access Token Page

Once you create it, the PAT token will be generated. Keep that token in a safe place (Key vault is the best) because you won’t get it again once the page is closed.

Generated PAT

Configure PAT in PostMan

In the PostMan software click on the Authorization tab and Select “Basic Auth” to set the PAT token as a Password. Leave the UserName field blank.

Add PAT token in Postman

Now run the API by clicking on Send button.

Parameter Error

You will get the error message “Parameters cannot be null” although we don’t have any parameter specified in the Azure Pipeline that is a mandatory part of the API URL processing. If you see the MS Url for running the pipeline we can pass the templateParameters property as an object.

template parameters

So, in this case, we can define the template parameter as a JSON header and for our example, we can keep it blank because we haven’t specified any parameter name.

In the Postman software, click on the Body -> raw and specify below.

{
    "templateParameters": {}
}
Template parameter in the Body part

Run it now. It should success now.

Content type error

Wait, we have an exception again saying that the content type for the POST method should be ‘application/json‘. Let’s add it.

In the Postman Headers, add the key Content-Type, and the value should be ‘application/json‘.

Set Content-Type in POSTMan

Let’s run it now. Hopefully, it should execute the pipeline now.

Status : 200 OK

Yay!! that works. Status is 200 OK and State is InProgress. Let’s check the pipeline status. You can see the pipeline execution is started now.

Pipeline execution using RestAPI

In case you have specified the parameters in the Azure pipeline, you can add them into the templateParameters in JSON format. For example,

{
    "templateParameters": {"vmName":"TestVM1"}
}

Ok. Our mission is successful now. You can download the script/code for the same from PostMan. From the top right, you can see the code icon (</>) to convert.

Convert Postman query to code

Convert to script using Code feature

Let’s convert it into the PowerShell code.

PowerShell conversion

and in the code snippet, you will see the script generated for it.

Loading