好吧,就像 Yan Sklyarenko 所说,TFS 2015(和 2013,经过一些更新)具有出色的 REST API。
我创建了一个非常非常粗略的基本 PowerShell 脚本,它可以满足我的需求。我不能足够强调这段代码需要重构多少——我真的只是需要它作为概念证明,我们将针对不同的需求开发多个脚本,但是对于来这里获取代码示例的人,你会发现在这里。
- 连接到 TFS 的构建系统
- 列出构建定义项目(对于我自己,Poc)
- 搜索一些字符串并获取构建 ID
- 使用硬编码的 ID 7 开始构建(因为我知道这会起作用,因此我的工作已经完成)
- 获取工件(我在其中合并了 VSO 构建任务“发布工件服务器”)
- Extract 说收到的Artifacts,因为 TFS 压缩它们。
从那时起,我将把这些脚本和输出合并到 MS 发布管理服务中 - 并准备好在为本地 TFS 2015 发布时迁移到 VSO Release vNext!
$projectId ='{ProjectIdGuid}'
$buildNr = '3945'
$username = 'username'
$password = 'password'
$zipDestination = 'C:\temp\unzip\temp.zip'
$workingFolder = ('C:\temp\unzip\' + [System.DateTime]::Now.ToString("yyyyMMddhhmmss")) #temp because of file already exist warnings... after completion we should delete the working directory content
$tfsURL = 'http://myTFS:8080/tfs/MyCollection/'+ $projectId
$cred = New-Object System.Management.Automation.PSCredential($username, (ConvertTo-SecureString -String $password -AsPlainText -Force))
#write list of build definitions (to be used later)
$allbuildDefs = (Invoke-RestMethod -Uri ($tfsURL + '/_apis/build/definitions?api-version=2.0') -Method GET -Credential $cred).value | Where-Object {$_.name -like '*buildName*'} | Out-Default | select name
Write-Host($allbuildDefs)
$buildDefs = ConvertFrom-Json($allbuildDefs)
$buildId = ($buildDefs.value).id;
#Get build Definition for what you want to build
$buildDefinitionURI = $tfsURL + '/_apis/build/requests?api-version=1.0'
#kick off build
$body = '{ "definition": { "id": '+ 7 + '}, reason: "Manual", priority: "Normal"}'
$BuildReqBodyJson = $body | ConvertTo-Json
$buildOutput = Invoke-RestMethod -Method Post -Uri $buildDefinitionURI -Credential $cred -ContentType 'application/json' -Body $body
#get buildNr
#build URI for buildNr
$BuildURI = $tfsURL + '/_apis/build/builds/' + $buildNr + '/artifacts'
#get artifact downloadPath
$downloadURL = (Invoke-RestMethod -Uri $BuildURI -Credential $cred).Value.Resource.downloadUrl
#download ZIP
Invoke-WebRequest -uri $downloadURL -Credential $cred -OutFile $zipDestination
#unzip
Add-Type -assembly 'system.io.compression.filesystem'
[io.compression.zipfile]::ExtractToDirectory($zipDestination, $workingFolder)