【问题标题】:Prevent parallel execution of same release pipeline in Azure DevOps防止在 Azure DevOps 中并行执行相同的发布管道
【发布时间】:2020-11-24 17:50:26
【问题描述】:

我正在使用 Azure 发布管道 进行部署。我有一个自托管 Linux 代理和 MS 托管 Linux 代理。发布管道有 3 个阶段,一个是主要部署,另外两个是并行运行的测试。我已经为部署和一个测试阶段配置了自托管代理,而对于另一个测试阶段,使用了 MS 托管代理。当我触发发布和部署开始执行时的当前问题,如果新版本出现。自托管代理将执行新版本,而不是在同一版本中执行测试阶段。我需要防止这种情况发生。

【问题讨论】:

  • 您好,刚刚检查一下这个问题现在是否仍然阻碍您?这个问题有更新吗?

标签: azure-devops


【解决方案1】:

每个版本都将从第 1 阶段开始。因此我们可以添加一个 PowerShell 任务作为第 1 阶段的第一个任务,以检查是否有之前正在进行的部署。

在这个 PowerShell 任务中,我们可以调用 Rest API 来检查发布阶段状态。

Power shell 脚本:

# Base64-encodes the Personal Access Token (PAT) appropriately
    $token = "$(pat)"
    $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
    $success = $false
    $count = 0
do{
    try{
    $stageurl2 = "https://vsrm.dev.azure.com/{org name}/{project name}/_apis/release/deployments?definitionId={release definition ID}&deploymentStatus=inProgress&api-version=6.0"
    $stageinfo2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $stageurl2 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
   
    $inprogressdeployments = $stageinfo2.value | where {($_.deploymentStatus -eq "inProgress")  -and ($_.release.name -ne $ENV:RELEASE_RELEASENAME)  -and ($_.releaseEnvironment.name -ne 'stop services')} | Sort-Object -Property completedOn -Descending
    #write-output $inprogressdeployments
    $stageurl3 = "https://vsrm.dev.azure.com/{org name}/{project name}/_apis/release/deployments?definitionId={release definition ID}&operationStatus=QueuedForAgent&api-version=6.0"
    $stageinfo3 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $stageurl3 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    $queueddeployments = $stageinfo3.value
    #write-output $queueddeployments
        if($inprogressdeployments) {            
            Write-output "Deployment In Progress, Waiting for it to finish!"  
            Write-output "Next attempt in 30 seconds"
            Start-sleep -Seconds 30          
      } else {            
            Write-Host "No Current Deployment in Progress"
            if($queueddeployments) {
            write-output "Current Queued deployments"
            Write-output "if 2 - Next attempt in 30 seconds"
            Start-sleep -Seconds 30 
            }
            else{
            write-output "No Queued deployments, starting release"
            $success = $true      
            }      
      }
    }
    catch{
        Write-output "catch - Next attempt in 30 seconds"
        write-output "1"
        Start-sleep -Seconds 30
      # Put the start-sleep in the catch statemtnt so we
      # don't sleep if the condition is true and waste time
    }
    
    $count++
    
}until($count -eq 2000 -or $success)
if(-not($success)){exit}

结果:

Stage1 将继续检查,直到所有以前的版本都完成

另外,您需要购买并行作业或创建多个自托管代理

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多