12

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

Jayendran
  • 9,638
  • 8
  • 60
  • 103
Moshe Shmukler
  • 1,270
  • 2
  • 21
  • 43

2 Answers2

26

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

John
  • 29,788
  • 18
  • 89
  • 130
Jayendran
  • 9,638
  • 8
  • 60
  • 103
  • Hello Jayendran . I am trying the same for creating release pipeline . But I get page not found . Can you please help . https://stackoverflow.com/questions/53225115/release-pipeline-api-is-not-working-in-azure-devops – Bala Nov 09 '18 at 12:01
  • @bkr glad you got the answer by yourself – Jayendran Nov 09 '18 at 14:40
  • 4
    *Important*: In the newest version of the API `parameters` is a string and no JSON object anymore. But the string should contain JSON. So you have to escape a JSON object, like so: `"parameters": "{ \"param1\": \"value1\" }"` – D.R. Jul 04 '19 at 09:29
  • I am struggling with either methods: https://stackoverflow.com/questions/63654387/azure-rest-api-for-building-pipelines Please help. – Yahya Uddin Aug 30 '20 at 05:53
  • This just isnt working for me, no build is started, no response is given – Dan Jan 30 '23 at 17:23
0

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

cmousset
  • 625
  • 7
  • 21