3
votes

I'm trying to queue a new build from PowerShell using the TFS Rest API. I'm able to queue a new build but I would like to set the requestedBy property. In the docs one can read that you can pass additional parameters. I can't find any further documentation on what these parameters can be. Does anyone know if this can be done?

Using the tfsbuild.exe (to queue XAML builds) you can pass an additional argument like this:

&$tfsBuild.Exe start "url" project definition /requestedFor:"$buildRequestedFor" /queue

edit

I've been able to get this working. The body of the request looks like:

$json = "{
        ""definition"": {
            ""id"" : 174
        }
        ,""requestedFor"": {
            ""id"": ""6f4d7323-fa51-4cda-9eb4-7342b02ba087""
        }
      }" `

You can only use the id propery. Using uniqueName for example will fail.

Here's the full PowerShell code:

$user = ""
$pass= ""

$uri = "http://Instance/DefaultCollection/Project/_apis/build/builds?api-version=2.0"

$json = "{
        ""definition"": {
            ""id"" : 174
        }
        ,""requestedFor"": {
            ""id"": ""6f4d7323-fa51-4cda-9eb4-7342b02ba087""
        }
      }" 

$secpasswd = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secpasswd)

Invoke-RestMethod -Uri $uri -Method Post -Credential $cred -ContentType "application/json" -Body $json
2
Seems to be a powershell solution here: stackoverflow.com/questions/32496022/…CJBS
I don't see why those two links help. I can trigger a new build, that's not the problem. I can't set the requestedBy property.E. Staal

2 Answers

3
votes

You should be able to use PowerShell to queue a build with the Invoke-RestMethod cmdlet. This link might help. The challenge is that the REST API doesn't look like it's fully documented yet so there are some properties that can only be found by using a tool like Fiddler. You might be able to change the body to be something like this but I haven't tried it yet.

$body = @"
        { 
            "definition": {
                "id": $Build_Definition_ID
            },
            "sourceVersion": {
                "requestedBy": {name here}
            }
        }
    "@

Here's an example that you should be able to modify (missing declaration of a few variables but this should get you started):

$body = @"
    { 
        "definition": {
            "id": $Build_Definition_ID
        } 
    }
"@

$baseUri = $TFSInstanceURL+"/"+$ProjectCollection+"/"+$TeamProject+"/_apis/build"

$postUri = $baseUri+"/builds?api-version=2.0"

Write-Host $postUri

##Create a new PSCredential based on username/password

$securePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($User, $securePassword)

### Queue a build ###

##Call the REST API for TFS that does a POST request to queue a build with the body of the request to be the build definition

$buildResponse = Invoke-RestMethod -Method Post -Credential $credential -ContentType application/json -Uri $postUri -Body $body

Write-Host (ConvertTo-Json $buildResponse)

See this link for more examples.

1
votes

I've been able to get this working. The body of the request looks like:

$json = "{
        ""definition"": {
            ""id"" : 174
        }
        ,""requestedFor"": {
            ""id"": ""6f4d7323-fa51-4cda-9eb4-7342b02ba087""
        }
      }"

You can only use the id propery. Using uniqueName for example will fail.