19 March 2024

Using Postman To Trigger A Pipeline Run

Rob Kersey

Something I had been working on recently needed me to start a number of ADO pipeline jobs quickly to "load test" a self hosted agent I had configured.

I wanted to fire off a number of Pipeline jobs in quick succession to the hosted agent and see how it reacted, to do this I needed to trigger a lot of jobs quickly! My KVM skills were not quick enough to get me around the ADO web UI fast enough, so I needed to find another way!

ADO is based on a REST API, so if you can find the right API there is usually a way to do most things auotmagically using scripts or via Postman!

A quick look at the documentation and I could see there was a Pipelines section in the API!

First we need to be able to interact with the ADO API, this requires some kind of authentication via Postman to ADO.

Authentication

To do this I created a new collection in postman called ./phazeOneBlog and added a few variables to the collection:

  • organization
  • PAT
  • base_url

I can then substitute these later in my URL call.

Next I needed to configure the authorization tab in the postman collection as below:

We now need to grab a PAT token from ADO. I went for a "custom defined" token so it was short lived, and the Build - Read & Execute and Project and Team - Read scope permissions as this seemed the most relevant to what we are doing:

Next we need to configure a New HTTP request in postman and configure the URL to make sure it is set to GET (Tip: Remember to hit save in Postman!):

Finally click Send and check the results that comeback... boom my 2 x ADO projects!

Getting The Pipeline ID

Looking ath the run pipeline documentation, we need to perform a http POST to the url below:

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

We already have an {organization} variable in postman, but it looked like we would also need to supply the project and pipelineID.

The project was easy to figure out, as we had that from our previous request and could use the project name according to the documentation, now we needed to get the pipeline id from somewhere.

To get the pipelineID in ADO go to pipelines then click on the pipeline you wish to run, then click on the edit button in the top right corner next to the Run Pipeline button:

Once the editor is open, copy the URL and look athe the parameters of the URL, these will be after the ? in the URL. The first parameter should be "pipelineId" and after the = is the ID of the pipeline.

https://dev.azure.com/phazeone/Jungle/_apps/hub/ms.vss-build-web.ci-designer-hub?pipelineId=2&nonce=3WJJ9l0Ijlnaz8SE26zlbQ%3D%3D&branch=no-love-in-the-jungle

Running The Pipeline

Now we had all the parameters to run the pipeline.. Let's run it! Remeber to click SAVE then SEND it!

Doh... an Error returned!

{
		"$id": "1",
    "innerException": null,
    "message": "Value cannot be null.\r\nParameter name: runParameters",
    "typeName": "System.ArgumentNullException, mscorlib",=
    "typeKey": "ArgumentNullException",
    "errorCode": 0,
   "eventId": 0
}

It appeared that we were missing some run parameters, a quick google and I came across the Stackoverflow post here. We need to add some detail to the body of the request, giving it the branch to run the pipeline against!

Adding the following JSON to the body of the request in postman (raw) tells the POST request to run the pipeline aginst the branch we want, in my case no-love-in-the-jungle.

{
  "resources": {
    "repositories": {
      "self": {
        "refName": "refs/heads/no-love-in-the-jungle"
      }
    }
  }
}

Hit SEND and boom the pipeline is running:

I've been listening to Fred Again quite abit recently, "Jungle" was playing when I started this post :)