Search code examples
azure-devopsazure-pipelinesazure-pipelines-yaml

Post cancellation task in Azure Pipelines


We have a scenario where once we manually cancel a pipeline in Azure DevOps, we need to terminate a particular process (exe). We wanted to know how can a task be triggered in YAML post cancellation, to achieve this.


Solution

  • Please try with the following ways:

    1. If there are multiple jobs in your pipeline, make sure the job that runs the task to terminate the particular process (exe) is processed after all other jobs have completed processing (Succeeded, Failed or Canceled). You can use the 'dependsOn' key to set the execution order of the jobs. For more details, see "Dependencies".

      Then, as @ShaykiAbramczyk mentioned, on the job that terminates the particular process (exe), you can use the 'condition' key to specify the condition under which the job runs. For more details, see "Conditions".

      jobs:
      . . .
      - job: string
        dependsOn: string
        condition: eq(variables['Agent.JobStatus'], 'Canceled')
      
    2. If there is only one job in your pipeline, make sure the task to terminate the particular process (exe) is the last step in the job. You need to put this task to the bottom of the steps list of this job.

      Then also need to specify the condition on the task to terminate the particular process (exe).

      • If using the 'condition' key on this step, this step will be always listed and display in the job run, regardless of whether it is skipped or not.

        steps:
        - step: string
           condition: eq(variables['Agent.JobStatus'], 'Canceled')
        
      • If using the 'if' statement on the step, this step will be listed and display in the job run only when the 'if' condition is met and to run this step. If the condition is not met and the step is skipped, this step will be hidden in the job run.

        steps:
        - ${{ if eq(variables['Agent.JobStatus'], 'Canceled') }}:
           - step: string