5
votes

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.

2
In the documentation it says that it is a string, so i will try with something like this parameters: "appId=xxxxx platform=xxxx isDemo=xxxxx", this is how i send the varaibles of a build with the Azure Cli for powershell and in that documentation its like this a string. Try it!!Nacho Martínez-Aedo
Tried that just now: appId=test platform=android isDemo=true, but get error on that as well.Yahya Uddin

2 Answers

6
votes

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
    }
}
0
votes

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"
}