Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure REST API for running builds or pipelines

I am trying to automate the creation of Azure Pipelines for a particular branch using their REST api.

However, I am struggling to use almost all their API's, as their documentation lacks examples.

Things like List and Get are simple enough.

However, when it comes to queuing a build: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.0

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=6.0
{
    "parameters": <parameters>, // how do i send paramters
    "definition": {
        "id": 1
    },
    "sourceBranch": "refs/heads/feature/my-pipeline",
    "sourceVersion": "d265f01aeb4e677a25725f44f20ceb3ff1d7d767"
}

I am currently struggling to send parameters. I have tried:

Simple JSON like:

"parameters": {
    "appId": "bab",
    "platform": "android",
    "isDemo": true
}

and stringify version of JSON like:

"parameters": "{\"appId\": \"bab\",\"platform\": \"android\",\"isDemo\": true}"

but none seems to work.

It keeps giving me the error:

{
    "$id": "1",
    "customProperties": {
        "ValidationResults": [
            {
                "result": "error",
                "message": "A value for the 'appId' parameter must be provided."
            },
            {
                "result": "error",
                "message": "A value for the 'platform' parameter must be provided."
            },
            {
                "result": "error",
                "message": "A value for the 'isDemo' parameter must be provided."
            }
        ]
    },
    "innerException": null,
    "message": "Could not queue the build because there were validation errors or warnings.",
    "typeName": "Microsoft.TeamFoundation.Build.WebApi.BuildRequestValidationFailedException, Microsoft.TeamFoundation.Build2.WebApi",
    "typeKey": "BuildRequestValidationFailedException",
    "errorCode": 0,
    "eventId": 3000
}

The docs is very unclear in how to send this data: https://docs.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.1#propertiescollection

Thank you very much for you help.

like image 758
Yahya Uddin Avatar asked Aug 30 '20 05:08

Yahya Uddin


2 Answers

I believe you cannot pass runtime parameters trough the Queue API. Instead, use Runs API

With that, your request body (use Content-type: application/json) should look something similar to this:

{
    "resources": {
        "repositories": {
            "self": {
                "refName": "refs/heads/feature/my-pipeline"
            }
        }
    },
    "templateParameters": {
        "appId": "bab"
        "platform": "android"
        "isDemo": true
    }
}
like image 193
LJ. Avatar answered Oct 04 '22 20:10

LJ.


I just realized that in the api-version=6.0 you can also send templateParameters on the Queue Service:

POST https://dev.azure.com/{organization}/{project}/_apis/build/builds?sourceBuildId={BUILD_BUILDID}&api-version=6.0
{
    "templateParameters": { "doReleaseBuild": "True" }, 
    "definition": {
        "id": 1
    },
    "sourceBranch": "refs/heads/feature/my-pipeline",
    "sourceVersion": "d265f01aeb4e677a25725f44f20ceb3ff1d7d767"
}
like image 30
Res Riedel Avatar answered Oct 04 '22 20:10

Res Riedel