azurepowershellazure-devopsazure-pipelines

Azure Yaml pipeline cancelTimeoutInMinutes not working


click Here ScreenShot for reference

I manually triggered this pipeline, and after 1 minute, I cancelled it via the UI. However, the pipeline continued running even after 1 minute. The cancelTimeoutInMinutes: 1 setting did not work. Why?

This condition: eq(variables['Build.SourceBranch'], 'refs/heads/main') is causing issue. If I remove this condition then pipeline jobs also cancelled. If I include conditions then it is not cancelled.

trigger:
- none

pool:
  vmImage: windows-latest

stages:
- stage:
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
    jobs:
    - job:
        #timeoutInMinutes: 1
        cancelTimeoutInMinutes: 1
        steps:
        - task: PowerShell@2
          inputs:
            targetType: 'inline'
            script: |
              for ($i=0; $i -le 200; $i++){
                $i
                start-sleep -s 2
              }

Solution

  • Azure Yaml pipeline cancelTimeoutInMinutes not working as expected

    When you define the condition at stage level, Azure DevOps starts evaluates before stage starts, and similarly it doesn't track the cancellation during execution the same way it does at job level.

    During the cancellation of run, the stage is still executing in the similar way because the condition already passed. This is because job inside the stage won't validate.

    To overcome this, move the condition to job level

    trigger:
    - none
    
    pool:
      vmImage: windows-latest
    
    stages:
    - stage:
        jobs:
        - job:
            condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
            cancelTimeoutInMinutes: 1
            steps:
            - task: PowerShell@2
              inputs:
                targetType: 'inline'
                script: |
                  for ($i=0; $i -le 200; $i++) {
                    $i
                    Start-Sleep -Seconds 2
                  }
    

    This setup ensures the job runs only if the branch condition is true.

    Refer:

    https://learn.microsoft.com/en-us/azure/devops/pipelines/process/phases?view=azure-devops&tabs=yaml#conditions