Trigger Azure Pipelines build via API

27.5k Views Asked by At

I made a working Azure Pipeline to build my codebase.

Looking for a way to trigger the Azure Pipelines build via API, ideally REST. If REST is not possible, perhaps I could try invoking the build via Azure Functions using a resource ID of sorts. I would like my own repository monitor to issue an API request that would trigger the build when my conditions are met. Another question - is it possible to set "pipeline variables" via API - e.g. I make an API call passing values to be used as my pipeline variables' values then triggers the build.

Thank you

3

There are 3 best solutions below

5
On BEST ANSWER

You can use the VSTS REST API or DevOps REST API to queue the build by giving the ID

VSTS POST:

https://account.visualstudio.com/project/_apis/build/builds?api-version=4.1

DevOps POST:

https://dev.azure.com/account/project/_apis/build/builds?api-version=6.1-preview.6

Body

{ 
        "definition": {
            "id": number
        } 
}

Refer to this solution

For your second question, Yes this is also possible, Just giving the parameters within the body

DevOps Body

{
    "parameters":  "{\"Parameter1\":  \"a value\"}",
    "definition":  {
                       "id":  2
                   }
}

Reference

Note: For these API calls make sure you use Basic Auth and pass a Personal Access Token as the value

0
On

I got in here while trying to trigger a Release Pipeline via cURL with PAT and I have some slightly different input that might by handy to somebody else with the same problem.

There's a Build, a Run and a Release API out there, and for this case (trigger a release pipeline), the Release API, create method, should be used:

https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/create?view=azure-devops-rest-7.1&tabs=HTTP

Moreover, by trial and error, I've found out that this doesn't work:

--header 'Authorization: Basic $PAT_TOKEN_ENCODED_IN_BASE64'

Following the Basic Authentication Scheme we have the following pattern:

base64(user:password)

And for PAT authentication, even though, user is empty, the colon is required so the information is correctly parsed in the Microsoft's API side.

This is what I've ended up doing in bash:

AZURE_ORGANIZATION=...
AZURE_PROJECT=...
PIPELINE_ID=...
MY_PARAMETER=...
PAT=...

# IMPORTANT: mind the colon before PAT
PAT_TOKEN_ENCODED_IN_BASE64_WITH_COLON=$(echo -n ":$PAT" | base64)

curl \
  --request POST "https://vsrm.dev.azure.com/$AZURE_ORGANIZATION/$AZURE_PROJECT/_apis/release/releases?api-version=7.1-preview.8" \
  --header "Authorization: Basic $PAT_TOKEN_ENCODED_IN_BASE64_WITH_COLON" \
  --header 'Content-Type: application/json' \
  --data-raw '{
      "definitionId": "'$PIPELINE_ID'",
      "variables": {
        "MY_VAR": {
          "value": "'$MY_PARAMETER'"
        }
      }
    }'

Last but not least, regarding variables, for Release Pipelines, variables must be declared in Pipeline's Variables Tab and marked as "Settable at release time".

I hope it helps.

0
On

An full answer for queuing a Build using the Azure DevOps REST API 7.1 (still in preview), using cURL, with PAT (Personal Access Token) authentication and custom parameters:

YOUR_PAT_TOKEN_ENCODED_IN_BASE64=...
AZURE_ORGANIZATION=...
AZURE_PROJECT=...
PIPELINE_ID=...
FULL_GIT_SHA=... # Optional, see usage below

curl --location \
  --request POST 'https://dev.azure.com/$AZURE_ORGANIZATION/$AZURE_PROJECT/_apis/build/builds?api-version=7.1-preview.7' \
  --header 'Authorization: Basic $YOUR_PAT_TOKEN_ENCODED_IN_BASE64' \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "definition": {
      "id": "$PIPELINE_ID"
    },
    "sourceVersion" : "$FULL_GIT_SHA", # Optional: only if you want to override the default sourceVersion
    "parameters": "{\"your.parameter\": \"your value\"}" # Yes this is a stringified JSON inside a stringified JSON...
  }'

Don't know what your PIPELINE_ID is? Go to the Azure Pipeline website, click on your pipeline and look at the URL: https://dev.azure.com/yourorganization/yourproject/_build?definitionId=42 -> the definitionId is the one you want