Skip to content

Fix pipeline cancel command to cascade to child pipelines

What does this MR do and why?

Issue: #273378 (closed)

MR this builds on: !82347 (merged)

Another draft approach was started here: !82149 (diffs)

This MR fixes pipeline cancelations only causing parent pipelines to cancel.

All child pipeline jobs and parent pipeline jobs cancel when a given pipeline is cancelled.

This approach takes on tech debt because it handles the logic about getting the child pipelines in 2 places(CancelPendingPipelines and in cancel_running) by passing in a cascade_to_decendants parameter, because queries were optimized for timeouts in CancelPendingPipelines and in the interest of fewer changes at once. #355047

Screenshots or screen recordings

Screen_Shot_2022-07-08_at_2.53.24_PM

How to set up and validate locally

  1. Setup a .gitlab-ci.yml that looks like:
      microservice_a:
        trigger:
          include: path/to/microservice_a.yml
    
      microservice_b:
        trigger:
          include: path/to/microservice_a.yml
    
      job1:
        script: sleep 10
    
      job2:
        script: sleep 10
  2. Add a second file in the same project path/to/microservice_a.yml
    nested_servicea:
        trigger: 
            include: nested_service.yml
    
    nested_serviceb:
        trigger: 
            include: nested_service.yml
    
    job1:
        script: sleep 30
    
  3. Setup another CI file nested_service.yml
    job1:
        script: sleep 30
  4. Run the pipeline a few times cancelling at various times. Through the ui or api (Illustrated below):
# Using httpie
http POST "http://localhost:3000/api/v4/projects/<project_id>/pipeline?ref=main" "PRIVATE-TOKEN: <api_token>"
http POST "http://localhost:3000/api/v4/projects/<project_id/pipelines/<pipeline_id>/cancel" "PRIVATE-TOKEN: <api_token>"
  1. Observe that the jobs/pipelines in the children(including nested children) both have the status of canceled
rails c
[97] pry(main)> Ci::Pipeline.find(pipeline_id).builds_in_self_and_descendants.map {|x| [x.status, x.id, x.pipeline.id, x.pipeline.root_ancestor.id, x.class.name] }

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #273378 (closed)

Edited by Allison Browne

Merge request reports