Afnan Ashraf
Afnan Ashraf

Reputation: 316

Prevent parallel execution of same release pipeline in Azure DevOps

I'm using Azure Release Pipeline for my deployment. I have a single self-hosted Linux agent and MS hosted Linux agents. Release pipeline has 3 stages, one is the main deployment and the other two are tests which will be running in parallel. I have configured self-hosted agent for deployment and for one testing stage, and for another testing stage, MS hosted agent is used. Current problem when I trigger the release and deployment starts executing if a new release comes up. The self-hosted agent will execute new release instead of executing the testing stage in the same release. I need to prevent this from happening.

Upvotes: 0

Views: 662

Answers (1)

Vito Liu
Vito Liu

Reputation: 8298

Each release will start from stage 1. Thus we can add a PowerShell task as the first task for stage 1 to check if there are previous in-progress deployments.

In this PowerShell task, we can call this Rest API to check release stage status.

Power shell script:

# 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}

Result:

Stage1 will continue to check until all previous versions are complete

enter image description here

In addition, you need purchase parallel jobs or create multiple self-hosted agent

Upvotes: 1

Related Questions