3

我正在尝试使用TFS Rest API从PowerShell对新构建进行排队。我可以对新构建进行排队,但我想设置 requestedBy 属性。在文档中可以读到您可以传递其他参数。我找不到任何关于这些参数可以是什么的进一步文档。有谁知道这是否可以做到?

使用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
4

2 回答 2

3

您应该能够使用 PowerShell 通过Invoke-RestMethodcmdlet 对构建进行排队。此链接可能会有所帮助。挑战在于 REST API 看起来还没有完整记录,因此有些属性只能通过使用 Fiddler 之类的工具才能找到。你也许可以把身体改成这样,但我还没有尝试过。

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

这是一个您应该能够修改的示例(缺少一些变量的声明,但这应该可以帮助您入门):

$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)

有关更多示例,请参见此链接

于 2016-07-11T19:59:47.320 回答
1

我已经能够得到这个工作。请求的正文如下所示:

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

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

于 2016-07-14T13:20:42.260 回答