TFS Build vNext rest api:请求的队列构建

时间:2016-07-11 09:40:28

标签: powershell tfs tfsbuild azure-pipelines azure-devops-rest-api

我尝试使用 TFS Rest API PowerShell 排队新版本。我能够为新构建排队,但我想设置requestedBy属性。在docs中可以读到您可以传递其他参数。我无法找到有关这些参数的更多文档。有谁知道这是否可以做到?

使用tfsbuild.exe(对XAML构建进行排队),你可以传递一个额外的参数:

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

修改

我能够让这个工作。请求正文如下:

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

你只能使用id属性。例如,使用uniqueName将失败。

以下是完整的 PowerShell 代码:

$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 个答案:

答案 0 :(得分:3)

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 :(得分:1)

我能够让这个工作。请求正文如下:

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

你只能使用id属性。例如,使用uniqueName将失败。