Warm tip: This article is reproduced from stackoverflow.com, please click
azure-devops azure-pipelines

How to make an Azure DevOps Pipeline wait for an external process to complete?

发布于 2020-04-13 10:36:29

I am using Azure DevOps Server deployed on premises. I would like to achieve the following, using Azure DevOps Pipelines:

  1. Pull, build and package a C# solution
  2. Call out to a proprietary deployment server (deployed in the same network as ADOS) to pick up the package and deploy it to a target machine
  3. Have the deployment server signal Azure DevOps that it's done deploying
  4. Original (or dependent?) Pipeline runs some tests against the newly deployed target

I've not been able to find a suitable task in the documentation to get this done. Am I missing something? Can I write a custom task of my own to make the Pipeline wait for an external signal?

Questioner
urig
Viewed
230
urig 2020-02-03 23:55

To make the Pipeline launch and then wait for my external process, I chose the path of least resistance and coded it as a PowerShell Task.

The external process is controlled through a REST API. Launching is done via a POST request and then a loop keeps polling the API with a GET request to see if the work is done. If a certain amount of time has passed without the process finishing successfully, the loop is aborted and the task is failed.

Here's the gist of my code:

$TimeoutAfter = New-TimeSpan -Minutes 5
$WaitBetweenPolling = New-TimeSpan -Seconds 10

# Launch external process
Invoke-RestMethod ...

$Timeout = (Get-Date).Add($TimeoutAfter)
do
{
    # Poll external process to see if it is done
    $Result = Invoke-RestMethod ...
    Start-Sleep -Seconds $WaitBetweenPolling.Seconds
}
while (($Result -eq "IN_PROGRESS") -and ((Get-Date) -lt $Timeout))

if ($Result -ne "SUCCESS")
{
    exit 1
}

PS - It's a good idea to sprinkle meaningful Write-Host messages in the above code, to make it easier to debug it when running in the pipeline.