Automating Azure DevOps Build Pipelines using API – Guidelines

Automating Azure DevOps Build Pipelines using Run Pipeline API

Today in this article, we will see how to use API for Automating Azure DevOps Build Pipelines using Run Pipeline API.

We will see an example of how to trigger Azure Pipelines build via API. AS we learned in the last article we have below two approaches to call REST API for Build pipeline,

  • Build -Queue API
  • Runs – Run Pipeline API

We already looked at the Build queue approach in the previous article.

Today in this article, we will cover below aspects,

For more details please see below article,

Both approaches have similar settings/configuration steps and hence they are easy to configure.

Trigger a build using – Run Pipeline

Build pipeline

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

Build pipeline With optional parameters

POST API

POST POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?pipelineVersion={pipelineVersion}&api-version=6.0-preview.1

Input Parameters

In the above Post request below are the input parameters,

  • organization – The name of the Azure DevOps organization.
  • project – Project ID or project name
  • api version – Version of the API to use.

Request Body

  • definition – The build number/name of the build.

Below is our sample repository,

https://dev.azure.com/jan2022-thecodebuzz/globalexceptionhandler/_build

start a build and passing variables through azure devops rest api

Configuration and security

Let’s now configure the API to be invoked securely.

Step1- Click on the User settings and set up the personal access token.

start a build and passing variables through azure devops rest api

Define a name for this token and authorize the scope of access with this token. You can choose this token access for Build pipeline trigger or release pipeline trigger etc.

Finally below is our URL and input request object defined,

URL

https://dev.azure.com/jan2022-thecodebuzz/globalexceptionhandler/_apis/pipelines/5/runs?api-version=6.0-preview.1

Let’s now use Postman to execute the API. Please set up the Basic authentication in the Postman by setting up the Token name as UserName and Password as a personal access token.

Request Object for Build Pipeline API

Below is my request body,

{
    "resources": {
        "repositories": {
            "self": {
                "refName": "refs/heads/master"
            }
        }
    }
}

Response from Build Pipeline API

Below is a sample response,

Automating Azure DevOps Build Pipelines using Run Pipeline API

Once you execute the POST API, please check your pipeline. You shall see it is executed and triggered for a build successfully,

trigger azure pipelines build via api

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

One thought on “Automating Azure DevOps Build Pipelines using Run Pipeline API

Leave a Reply

Your email address will not be published. Required fields are marked *