如何在PowerShell中使用api重新部署TFS版本

时间:2017-10-10 11:27:29

标签: api tfs tfs2017

我正在使用TFS 2017 for CD。我正在尝试使用PowerShell重新部署版本。是否有任何API可以重新部署版本?

1 个答案:

答案 0 :(得分:1)

您需要通过REST API获取特定的版本ID 环境ID ,然后调用REST API重新部署旧版本。< / p>

您可以使用以下PowerShell脚本重新部署特定的发布环境:

Param(
   [string]$Collecitonurl = "http://server:8080/tfs/DefaultCollection",
   [string]$projectName = "YouTeamProjectName",
   [string]$keepForever = "true",
   [string]$user = "Domain\User",
   [string]$token = "your token",
   [string]$releaseid = "45" # Give the specific Release ID here
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

#Get releaseresponse
$Releaseurl= "$Collecitonurl/$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 = "$Collecitonurl/$projectName/_apis/release/releases/$releaseid/environments/"+$firstEnvironment+"?api-version=3.2-preview" # 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

enter image description here