我们有一种情况:
TFS 2015用户使用CI提交签入,作为CI构建的一部分,我们运行GIT命令,该命令会将当前的TFS 2015存储库同步到VSTS GIT存储库。
下面是代码(同步部分)
在VSTS中同步代码后,立即触发VSTS中的CI构建。 (针对该特定回购)。例如:内部版本号为 Testbuild_0.1.8
要求是动态读取VSTS生成状态,并在TFS 2015中更新状态。我想出了一个脚本(如下所示),该脚本可获取Build和显示状态(但我必须发送一个硬编码值,有没有一种方法可以动态读取?
代码1:
<pre> <code> git remote add vsts https://Personal%20Access%20Token:$PATToken@test.visualstudio.com/$GITProject/_git/$GITRepo 2>&1|Write-Host
git checkout ${env:BUILD_SOURCEBRANCHNAME}
git reset --hard origin/master
git push vsts ${env:BUILD_SOURCEBRANCHNAME}
</pre> </code>
代码2:
我必须动态读取值Build ID或Build number并传递该值以获取构建状态。在[string] $ buildNumber =“ Testbuild_0.1.8”(标为粗体)下方应动态读取吗?
<pre> <code >
[CmdletBinding()]
Param(
[string] $TFSURL = "https://remotevsts.visualstudio.com",
[string] $TeamProject = "Test TP",
**[string]$buildNumber = "Testbuild_0.1.8",**
[string]$user = "test@live.com",
[string]$token = "tokenid"
)
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$uri = "$TFSURL/$TeamProject/_apis/build/builds?api-version=2.0&buildNumber=$($buildNumber)"
$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
if ($result.count -eq 0)
{
throw "Unable to locate Build ID for Build Number $($buildNumber)"
}
$buildId = $result.value[0].id
Write-Host "$buildId"
$uri = "https://remotevsts.visualstudio.com/Test TP/_apis/build/builds/$($buildId)?api-version=2.0"
$responseFromGet = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Uri -Headers @{Authorization=("Basic {0}" -f $base64authinfo)}
$status = $responseFromGet.status
Write-Host "$status"
while($status -ne "completed"){
Start-Sleep -Seconds 60
$responseFromGet = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Uri -Headers @{Authorization=("Basic {0}" -f $base64Authinfo)}
$status = $responseFromGet.status
Write-Host "Status: " + $status
}
if ($responseFromGet.result -eq "succeeded")
{
Write-Host "##vso[task.complete result=Succeeded;]Done"
}
else
{
Write-Error "##vso[task.complete result=Failed;]Error testing if demo site is up"
exit 1
}
</pre> </code>
答案 0 :(得分:0)
您将需要使用List
api来查询最近的构建(您已经可以使用您知道的参数来确定该构建的范围)和then use the SourceBranch/SourceVersion to match来查找必须找到正确构建的git ref
或者,不要在VSTS构建上设置CI,而不要使用Build / Queue API从TFS将构建排队。这样,您就可以在队列时间知道确切的构建ID。