0
votes

I have 2 Azure Pipelines, deploy and test. As their names imply one is used for deploying a product and the other is used for testing. When a developer wants to run their own tests on the existing deployment they trigger test. When a deployment is required they trigger deploy. If the test pipeline is in execution when the deploy pipeline is triggered I want the deploy to wait till the test has finished executing.

Is there a way to configure this dependency within the pipeline.yaml themselves, or a workaround to achieve the mentioned requirement

2
Hi @JAbeywrdana Is there any update about this ticket? Feel free to let me know if the suggestion could give you some help. Just a remind of this.Kevin Lu-MSFT
Hi @Kevin Lu-MSFT, Sorry for the late response, I am in the process of trying out your solution, I actually have to write the querying process in bash instead of powershell which is why there maybe a small delay. But I tried executing a few curl commands to see the status and it should work. I will provide a confirmation on this ASAPJAbeywrdana

2 Answers

1
votes

Is there a way to configure this dependency within the pipeline.yaml themselves, or a workaround to achieve the mentioned requirement

Here are two methods could meet your requirement:

1.You could add the Environment in your Yaml Pipeline. Add you could add Invoke Rest API check in the environment. Rest API: Latest - Get

enter image description here

In Yaml Pipeline, you could call this environment.

Example:

stages:
- stage: deploy
  jobs:
  - deployment: DeployWeb
    displayName: deploy Web App
    pool:
      vmImage: 'Ubuntu-latest'
    environment: 'EnvironmentName'
    strategy:
      runOnce:
        deploy:
          steps:
          ...

When you run the pipeline, the environment will check the latest build status of the test Pipeline. If the build has completed , it will run the deploy pipeline.

Result:

enter image description here

2.You could directly add a Powershell task in the Deploy task to check the status of the Test Pipeline.

$token = "PAT"

$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/definitions/{DefinitionID}?includeLatestBuilds=true&api-version=5.1"


$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))


$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json



$buildid = $response.latestBuild.id
echo $buildid


$success = $false

do{
    try{
    $Buildurl2 = "https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$($buildid)?api-version=5.0"


    $Buildinfo2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Buildurl2 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

    $BuildStatus= $Buildinfo2.status 
    $result = $Buildinfo2.result
    echo $result
    echo $BuildStatus


 
   
        if($BuildStatus -eq "completed"  ) {            

            write-output "No Running Pipeline, starting Next Pipeline"
            $success = $true 
                       
      } else {   
            Write-output "Pipeline Build In Progress, Waiting for it to finish!"  
            Write-output "Next attempt in 30 seconds"
            Start-sleep -Seconds 30         

            }
    
      
    }
    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 -eq $true )
if ($result -ne "succeeded" )
{
   echo "##vso[task.logissue type=error]Something went very wrong."
}

if(-not($success)){exit}

You can also refer to my another ticket.

0
votes

You will probably have to merge the pipelines into one and, depending on the steps in them, you can convert them to jobs or even to stages. In both cases, you can specify dependencies via dependsOn (e.g. docs for jobs).

So you will have something like:

jobs:
- job: test
  steps:
  - step1...
  - step2...

- job: deploy
  dependsOn: test
  steps:
  - step1...
  - step2...

Also, if you go this way, consider using deployment jobs for deployment, they have some related build-in functionality.