您可以使用 REST API 来实现这一点(使用 API 版本 api-version=2.3-preview.1):
我使用以下 PS 脚本在 TFS 2015 Update 4 (Version 14.114.26403.0) 上进行了测试,它按预期工作。
以下 PowerShell 脚本供您参考:
创建新版本:(参见 REST API here)
Param(
[string]$collectionurl = "http://server:8080/tfs/Collection",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$user = "username",
[string]$token = "token",
[string]$releasedDefinitionId = "2"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
{
"definitionId":$releasedDefinitionId,
"description":"",
"artifacts":[
{
"alias":"VSTEST",
"instanceReference":{
"name":"20171123.1",
"id":"1256",
"sourceBranch":"$/0418Scrum/web0418"}
}],
"isDraft":false,
"manualEnvironments":[]
}
"@
return $value
}
$json = CreateJsonBody
$uri = "$($collectionurl)/$($projectName)/_apis/Release/releases?api-version=2.3-preview.1"
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$ReleaseID = $result.id
Write-Host "ReleaseID:" $ReleaseID
将发布部署到指定环境:(部署本示例中的第三个环境,根据您的要求进行更改即可。)
Param(
[string]$baseurl = "http://server:8080/tfs/CollectionLC",
[string]$projectName = "ProjectName",
[string]$keepForever = "true",
[string]$user = "username",
[string]$token = "token",
[string]$ReleaseID = "3"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
#Get releaseresponse
$Releaseurl= "$baseurl/$projectName/_apis/Release/releases/$ReleaseID"
$releaseresponse = Invoke-RestMethod -Method Get -UseDefaultCredentials -ContentType application/json -Uri $Releaseurl
#Get all of the environment IDs from the release response:
$environmentIDs = $releaseresponse.environments.ForEach("id")
#Get the specific environment ID by grabbing the element in the environment IDs array:
$firstEnvironment = $environmentIDs[0]
$secondEnvironment = $environmentIDs[1]
$thirdEnvironment = $environmentIDs[2] # ...
#Create the JSON body for the deployment:
$deploymentbody = @"
{"status": "inprogress"}
"@
#Invoke the REST method to redeploy the release:
$DeployUrl = "$baseurl/$projectName/_apis/release/releases/$releaseid/environments/"+$thirdEnvironment+"?api-version=2.3-preview.1" # Change the envrionment ID accordingly based on your requirement.
$DeployRelease = Invoke-RestMethod -Method Patch -ContentType application/json -Uri $DeployUrl -Headers @{Authorization=("Basic {0}" -f $base64authinfo)} -Body $deploymentbody
write-host "environmentIDs:" $environmentIDs
您还可以将它们放在一起,创建一个新版本并在一个脚本中部署特定环境。在another thread 中查看我的答案。