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

时间:2019-11-05 10:52:34

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

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

我想知道是否有一种方法可以在允许新版本进入队列之前检查此任务或以前的版本是否已完成。可以使用门吗?

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

1 个答案:

答案 0 :(得分:0)

您不能将Invoke Rest API门与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令牌:

enter image description here

Azure函数还支持PowerShell,因此您也可以使用Azure函数gate:

1)使用VS代码创建新的Azure函数,如说明的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)在您的版本中创建一个“调用Azure功能”门:

enter image description here

另一种方法是,使用上面的代码,将他转换为C#或使用Rest API的另一种语言,然后将其部署到Web服务器并使用Invoke Rest API Gate。