是否有一个变量可以找到最后成功部署到阶段的变量? [英] Is there a variable to find the last successful deployment to a stage?

查看:109
本文介绍了是否有一个变量可以找到最后成功部署到阶段的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当部署到发布管道中的某个阶段时,当前已完成的过程的一部分是整理该阶段所部署的先前版本,这意味着告诉当前部署先前的部署版本是什么.目前,这是使用自定义变量手动完成的,但似乎应该是可以从代理中检索到的东西.鉴于在发行版的较早版本中将出现不同版本的,具有递增修订的版本,因此使用的变量是每个阶段的,而不是知道整个发行版中的先前版本. 有人知道是否有办法找回这个吗?

When deploying to a stage within a release pipeline, part of the process currently in place is to tidy up from the previous version deployed on that stage, which means telling the current deployment what the previous deployed version was. This is currently being done manually with a custom variable, but it seems like it should be the sort of thing that can be retrieved from the agent. Given a different number of releases with incremented revisions will occur earlier in the pipeline, the variables being used are per stage, rather than knowing what the previous version was throughout the whole pipeline. Does anyone know if there is a way to retrieve this?

推荐答案

There isn't a previous release variable in the predefined Release Variables, however, you should be able to achieve this by querying the Azure DevOps REST API using a PowerShell task from within your pipeline.

您的脚本在构建管道的安全上下文下运行.为此,请.

Your scripts with run under the security context of the build pipeline. To enable this, the agent phase needs to have the "allow scripts to access the OAuth token" turned on.

列出部署终结点可用于查询所有部署,但可对其进行过滤以找到适合您的版本定义和当前环境的成功版本.

The List Deployments endpoint can be used to query all deployments, but it can be filtered to find the successful releases for your release definition and current environment.

使用以下脚本添加PowerShell任务:

Add a PowerShell task with the following script:

param( )

# use this function to invoke the scripts locally with a PAT token
function getAuthToken($user, $accessToken) {
  $userString = "{0}:{1}" -f $user, $accessToken
  $base64String = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes($userString))
  return "Basic {0}" -f $base64String
}

function getOAuthToken() {
  return "Bearer {0}" -f $env:SYSTEM_ACCESSTOKEN
}

function getServerUrl() {
  return [string]::Format("https://{0}{1}", $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI, $env:SYSTEM_TEAMPROJECTID)
}

function InvokeRestApi($relativeUri, $authHeader) {
   $baseUrl = getServerUrl
   $url = [Uri]::EscapeUriString((getServerUrl) + $relativeUri + "?api-version=5.0")
   Write-Host "Querying:" $url
   return Invoke-WebRequest $url -Headers @{Authorization=($authHeader)} | ConvertFrom-Json
}

$auth = getAuthToken

$url =  "/release/deployments?definitionId=" + $env:RELEASE_DEFINITIONID
$url += "&definitionEnvironmentId=" + $env:RELEASE_DEFINITIONENVIRONMENTID
$url += "&deploymentStatus=succeeded"
$url += "&queryOrder=descending"

$json = InvokeRestApi $url $auth

$lastRelease = $json.value[0]

这篇关于是否有一个变量可以找到最后成功部署到阶段的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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