Chris Duffy
Chris Duffy

Reputation: 167

Azure DevOps pipeline task to wait to run for another pipeline to complete

I had a question regarding Azure DevOps pipelines and tasks and was wondering if anyone could help.

I have a pipeline with a task that runs a PowerShell script. This script kicks off a separate pipeline, but once that script is run, the original task returns a "pass" (as expected) and the next task in the original pipeline begins to run.

Ideally, I would like the next task in pipeline 1 to wait until the pipeline that was kicked off by the script is complete (and returns a pass). Does anyone know of a way this can be achieved? The steps are using YAML. So far I have seen conditions to wait for other steps in the same pipeline, but nothing to stop a step from running until a completely separate pipeline is completed (and passes successfully).

Hopefully I am making sense. I can provide screenshots if that would help as well!

Upvotes: 13

Views: 20411

Answers (3)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41545

Instead of trigger the build with your PowerShell script, you can install the Trigger Build Task extension and use it. there you have an option Wait till the triggered builds are finished before build continues:

enter image description here

In YAML:

- task: TriggerBuild@3
  displayName: 'Trigger a new build of Test'
  inputs:
    buildDefinition: Test
    waitForQueuedBuildsToFinish: true
    waitForQueuedBuildsToFinishRefreshTime: 60

If this option is enabled, the script will wait until the all the queued builds are finished. Note: This can take a while depending on your builds and your build will not continue. If you only have one build agent you will even end up in a deadlock situation!

Upvotes: 10

Bevan
Bevan

Reputation: 1424

The main problem you face here is that all variables are evaluated on queue with YAML.

You could do a few things like utilise an external service such as an Azure Storage Account. Doing this you could do other stuff like write comments or statuses from the pipeline into a text file and read the values into your first pipeline.

At the end of script 1 in pipeline 1:

Do {
   $File = check for storage account file
   if ($File) {
      $FileExists = $true
   }
} until ($FileExists)

At the end of your other pipeline

Do stuff
Write stuff to file
Upload file to storage account

If you just want to wait for completion you could use azure devops cli at the end of your first powershell step. This is a good link: https://techcommunity.microsoft.com/t5/ITOps-Talk-Blog/Get-Azure-Pipeline-Build-Status-with-the-Azure-CLI/ba-p/472104 . You could run your logic on the returned status or result

Upvotes: 1

mbb5079
mbb5079

Reputation: 766

According to the description, the whole process could be separated to four parts:

  1. The task1 in Pipeline1 should trigger the task in Pipeline2 and if the task in Pipeline is not editable you might need to create a new task in it at last for next step usement

  2. The last task in Pipeline2 should do something like create a txt file in a specific folder or any another things that could be detected by task2 in Pipeline1

  3. The task2 in Pipeline1 should wait and listen if a txt file in the folder is created which means the Pipeline1 is completed successfully.

  4. Run the task2

Upvotes: 1

Related Questions