如何在PowerShell中使用API​​重新部署TFS版本 [英] How to redeploy TFS release using api in PowerShell

查看:46
本文介绍了如何在PowerShell中使用API​​重新部署TFS版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以CD为前提使用TFS 2017.我正在尝试使用PowerShell重新部署发行版.是否有任何API可以重新部署版本?

解决方案

您需要通过

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 ?

解决方案

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

这篇关于如何在PowerShell中使用API​​重新部署TFS版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆