0

I am trying to create a build request and specify new values for custom variables defined in the TFS build definition. I assume I can do this without updating the build definition first. I posted the following JSON to the URL: http://<server-name>/tfs/DefaultCollection/<project-name>/_apis/build/builds?api-version=3.1. The build queued up but the variable value passed in did not override the default value. What am I missing? Do I need to specify the variable name differently?

{
    "definition": {
        "id": 24,
        "variables": {
            "IssueNumber": {
                "value": "98765"
            }
        }
    }
}
Colin
  • 331
  • 3
  • 19

1 Answers1

4

You're providing the wrong JSON structure. It's parameters, not variables, and the way you're specifying the key/value pairs is incorrect.

This PowerShell snippet should point you in the right direction:

$url = 'http://test-tfs-instance:8080/tfs/myCollection'

$body = @{
    definition = @{
        id = 1435
    }
    parameters = '{"MyParam":"OverriddenValue","system.debug":"false"}'
}

Invoke-RestMethod -Uri "$($url)/TeamProject/_apis/build/builds?api-version=3.1" -UseDefaultCredentials -Method Post -ContentType 'application/json' -body ($body | convertto-json -Compress -Depth 10)

For what it's worth, this kind of thing is trivial to discover by opening up the developer tools in your browser and looking at the REST call the TFS UI makes. Sometimes the documentation is unclear (as it is in this case), but it's hard to get mixed up when you're copying the same REST calls the application makes.

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • Thanks! I hadn't thought about inspecting what the browser was doing to discover it's making the same REST calls. I was just going off the documentation. – Colin May 04 '18 at 15:20
  • 1
    @Colin The REST API documentation is unfortunately weak in places, especially when you're looking for older API versions. The browser never lies, though. – Daniel Mann May 04 '18 at 15:21
  • Thanks, do you know how to do this for overwriting task group variables when queuing a build? – Elliptica Sep 20 '19 at 15:55
  • For future folks reaching this: note that the parameters value is a string in JSON format, not an array. – JohnTortugo Jan 25 '20 at 21:45