Catch cancellation request from a custom AzureDevOps task

101 views Asked by At

Azure DevOps pipelines. Can a custom task somehow detect that the user has cancelled the build/release job mid-execution and quit gracefully? I'm wondering about both the Node API and the Powershell API.

To clarify, I'm talking about this kind of cancel:

enter image description here

Not about conditional execution of tasks.

Nothing to that effect at https://github.com/microsoft/azure-pipelines-task-lib/blob/master/node/docs/azure-pipelines-task-lib.md

"Impossible" is an acceptable answer.

1

There are 1 answers

6
wade zhou - MSFT On

Can a custom task somehow detect that the user has cancelled the build/release and quit gracefully?

In DevOps pipeline, Cancel is not detected inside the task, but in the condition expression.

For example, the last task will run only when it's canceled before. enter image description here

You can set the condition on stage, job and step level. Please check the official docs below for the details:

Pipeline behavior when build is canceled

Job status check functions

With the condition, make sure the task will run even it's canceled by user, then you can consider what to check inside the task.

For example, to get the job, task status in current build(rest api here)

pool:
  vmImage: ubuntu-latest

jobs:
  - job: job1
    steps:
      - script: |
          echo step1
          sleep 30   #  can cancel for testing.
        displayName: step1

      - script: echo step2
        displayName: step2

      - script: echo step3
        condition: canceled()
        displayName: execute only when canceled before

  - job: job2
    condition: always()
    dependsOn: job1
    steps:
      - script: echo step 1
        displayName: step1 in job2
      - powershell: |
          # Define the URL for the REST API call
          $url = "$(System.CollectionUri)$(System.TeamProject)/_apis/build/builds/$(build.buildid)/timeline?api-version=6.0"

          # Make the REST API call
          $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method Get

          # Loop through the records and print the status of each stage, job, and step
          foreach ($record in $response.records) {
              Write-Host ("Name: " + $record.name)
              Write-Host ("Type: " + $record.type)
              Write-Host ("Status: " + $record.result)
              Write-Host ("--------------------------------")
          }
        env:
          SYSTEM_ACCESSTOKEN: $(system.accesstoken)

enter image description here

Edit, update based on the new screenshot:

As per the screenshot, you are canceling for classic release pipeline.

The situation could be complicated:

  1. If you start the release pipeline, and immediately cancel it, cannot detect the cancel in task.

If you check the run log, it shows no stage deployed as below:

enter image description here

  1. If you cancel the release run after at least 1 task started(like the screenshot you shared), as stated before, you can detect the cancel in the new task. The referred rest api here.

In the powershell task:

enter image description here

With custom condition in the task behind: enter image description here

enter image description here

Can detect the canceling in rest api response -> deploysteps -> operationstatus.

enter image description here