azure-devops - 如何在部署新版本之前检查以前的版本是否已完成

标签 azure-devops azure-pipelines azure-pipelines-release-pipeline

作为我们发布管道的一部分,我们有一个任务(最后一个任务)将发布分支合并回主分支。

我想知道是否有办法在允许新版本排队之前检查此任务或以前的版本是否已完成。门可以用于此吗?

理想情况下,发布经理随后可以决定是继续发布还是取消发布。

最佳答案

您不能将 Invoke Rest API gate 与 Azure DevOps API url 一起使用,因为要检查上次发布状态,您需要检查环境(阶段)状态,为此,您需要发布 ID(因此您不知道它会是什么并将其放在其余 API 入口 URL 中)。

但是,您可以使用 PowerShell 检查最后一个版本,如果不成功,则让该阶段失败。

在您的版本中添加一个 PowerShell 任务以检查最后一个版本:

$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }

# Replace {org} with your organization
# Replace {project} with your project
# Replace {defId} with your release definition id

$url = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases?definitionId={defId}&api-version=5.1"
$releases = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ContentType 'application/json'

$releaseUrl = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases/$($releases.value[1].id)?api-version=5.1"
$releaseInfo = Invoke-RestMethod -Method Get -Uri $releaseUrl -Headers $headers -ContentType 'application/json'

$releaseEvnriomentId =  $releaseInfo.environments.Where({ $_.name -eq 'THE STAGE NAME WHERE YOU DO MERGE' }).id

$envUrl = "https://vsrm.dev.azure.com/{org}/{project}/_apis/Release/releases/$($releases.value[1].id)/environments/$($releaseEvnriomentId)?api-version=5.1-preview.1"
$environment = Invoke-RestMethod -Method Get -Uri $envUrl -Headers $headers -ContentType 'application/json'

$envStatus = $environment.status

if($envStatus -ne "succeeded")
{
    Write-Error "Previous release not succeeded!"
}
else
{
    Write-Host "Previous release succeeded :)" 
}

在代理作业选项中,您需要允许脚本访问 OAuth token :

enter image description here

Azure 函数还支持 PowerShell,因此您也可以使用 Azure 函数门来实现:

1) 使用 VS Code 创建一个新的 Azure Function,如解释的那样 here .

2) 在您的 run.ps1 文件中将代码替换为以下代码:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

$defnitionId = $Request.Query.DefinitionId

# Generate PAT and put it in the {YOUR PAT}
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,"{YOUR PAT}")))
$headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}

# Replace {org} with your organization
# Replace {project} with your project

$url = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases?definitionId=$($defnitionId)&api-version=5.1"
$releases = Invoke-RestMethod -Method Get -Uri $url -Headers $headers -ContentType 'application/json'
Write-Debug $releases

$releaseUrl = "https://vsrm.dev.azure.com/{org}/{project}/_apis/release/releases/$($releases.value[1].id)?api-version=5.1"
$releaseInfo = Invoke-RestMethod -Method Get -Uri $releaseUrl -Headers $headers -ContentType 'application/json'
Write-Debug $releaseInfo

$releaseEvnriomentId =  $releaseInfo.environments.Where({ $_.name -eq 'THE STAGE NAME WHERE YOU DO MERGE' }).id

$envUrl = "https://vsrm.dev.azure.com/{org}/{project}/_apis/Release/releases/$($releases.value[1].id)/environments/$($releaseEvnriomentId)?api-version=5.1-preview.1"
$environment = Invoke-RestMethod -Method Get -Uri $envUrl -Headers $headers -ContentType 'application/json'
Write-Debug $environment

$envStatus = $environment.status
Write-Debug $envStatus

if($envStatus -ne "succeeded")
{
    $status = [HttpStatusCode]::BadRequest
    $body = "failed"
}
else
{
    $status = [HttpStatusCode]::OK
    $body = "success"
}


# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = $status
    Body = $body
})

3) 将函数发布到 Azure。

4) 在您的发布中创建一个 Invoke Azure Function 门:

enter image description here

另一种选择,采用上面的代码,将其转换为 C# 或其他语言并使用 Rest API,部署到它的 Web 服务器并使用 Invoke Rest API gate。

关于azure-devops - 如何在部署新版本之前检查以前的版本是否已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709812/

相关文章:

azure-devops - App serverice 部署从 vsts 失败 4294967295

azure-devops - 有条件的内部版本号以便发布预发布的 nuget 包?

git - 允许 PR 构建验证构建而不发布工件

Azure Pipeline 空工件

azure-devops - 通过 Azure DevOps 通过一个发布过程部署许多应用程序

docker-compose - 如何从 Azure Pipeline 中的 Docker Compose 任务获取多容器应用程序的 URL?

azure - 如果任务中报告任何错误,如何使 Azure 管道失败

powershell - Azure DevOps 服务器调用 RestMethod 错误 : No API version provided for the "PUT" request

azure - 如何使用yml文件为azure管道中的不同分支设置不同的参数

GitHub 无法创建服务 Hook 订阅 无法在选定的 GitHub 上配置服务...资源无法通过集成访问