How to redeploy TFS release using api in PowerShel

2019-08-20 09:11发布

问题:

I am using TFS 2017 on premise for CD . I am trying to redeploy a release using PowerShell. Is there any API to redeploy release ?

回答1:

You need to get the specific Release ID and the Environment ID via REST API, then invoke the REST API to re-deploy the old release.

You can use below PowerShell script to re-deploy the specific release environment:

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



标签: api tfs tfs2017