TFS Release - 获取给定环境的可用版本列表
Posted
技术标签:
【中文标题】TFS Release - 获取给定环境的可用版本列表【英文标题】:TFS Release - get list of available releases for given environment 【发布时间】:2021-12-21 19:25:25 【问题描述】:我正在尝试在本地 TFS 2017/2018 上使用 TFS 发布管理。
每次创建工件时,我都会创建版本。我还有几个环境可以部署版本。 QA-1 环境会在每次我们有新的工件时更新(一天可以几次)。 QA-2 环境应该每天更新一次。
如何获取可部署到 QA-2 环境的版本列表?
如果我已经在一夜之间部署了 Release-20。白天,我们创建了 Release-21、22、23(全部部署到 QA-1)。有没有办法使用 API 判断 21、22、23 可用于 QA-2 环境?
我试过了,但它显示了已经部署的内容:
https://<server>/<project>/_apis/release/deployments?definitionId=12&definitionEnvironmentId=28
它不显示尚未部署的那些(我正在寻找的那些)。
谢谢。
【问题讨论】:
【参考方案1】:除非我目前看不到更好的方法,否则这可能有效。
获取环境中最新部署的版本。
https://<server>/<collection>/<project>/_apis/release/deployments?definitionId=<def_ID>&definitionEnvironmentId=<env_ID>&$top=1&api-version=4.1-preview.2
获取从 JSON 创建此版本的时间。将要在下一个命令中使用的 加一秒:
https://<server>/<collection>/<project>/_apis/release/releases?definitionId=<def_ID>&minCreatedTime=<datetime>&api-version=4.1-preview.2
现在我们可以在这个 JSON 结果中迭代发布。
这是获取概念的 powershell 脚本:
param(
[String]$TFSurl = "", # URL to access TFS server
[String]$TFScollection = "", # Collection name in TFS
[String]$TFSproject = "", # Name of the project in TFS
[String]$APIversion = "4.1-preview.2", # Version of API to be used
[String]$definitionId = "", # Release definition ID
[String]$definitionEnvironmentId = "", # Environment ID inside the release definition
$debugTrace = "SilentlyContinue" # SilentlyContinue, Continue
)
$dtformat = 'yyyy-MM-ddTHH:mm:ss.FFFFFK' # DateTime format used by TFS API and returned in Json.
# Get last release that was deployed to the environment to get date and time when it was created
$uri = "$TFSurl/$TFScollection/$TFSproject/_apis/release/deployments?definitionId=$definitionId&definitionEnvironmentId=$definitionEnvironmentId&`$top=1&api-version=$APIversion"
Write-Host "Calling REST: $uri"
$result = Invoke-RestMethod -Method 'Get' -ContentType 'application/json' -uri $uri -UseDefaultCredentials
Write-Host "Release Definition: " $result.value.releaseDefinition.name
Write-Host "Environment name: " $result.value.releaseEnvironment.name
Write-Host "Last release deployed: " $result.value.release.name
Write-Host "Release create date: " $result.value.completedOn
# add 1 second to create date to make sure we don't get the same realease again
$dateTimeNew=[datetime]::ParseExact($result.value.completedOn,$dtformat,$Null).AddSeconds(1).ToString($dtformat)
Write-Host "Release time +1 second:" $dateTimeNew
# Get list of releases avaialble since specified date and time
$uri = "$TFSurl/$TFScollection/$TFSproject/_apis/release/releases?definitionId=$definitionId&minCreatedTime=$dateTimeNew&queryOrder=ascending&api-version=$APIversion"
Write-Host "Calling REST: $uri"
$result = Invoke-RestMethod -Method 'Get' -ContentType 'application/json' -uri $uri -UseDefaultCredentials
foreach ($V in $result.value)
Write-Host "Release: $($V.releaseDefinition.name) (id=$($V.releaseDefinition.id)) $($V.name) (id=$($V.id))"
【讨论】:
以上是关于TFS Release - 获取给定环境的可用版本列表的主要内容,如果未能解决你的问题,请参考以下文章