# Is it possible to cancel job automatically when specific job is approved?

**URL:** <https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487>\
**Category:** Build Environment\
**Created:** [September 23, 2020, 8:31am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487 "2020-09-23T08:31:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![baeharam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/baeharam/32/25130_2.png) [@baeharam](https://discuss.circleci.com/u/baeharam)\
**Post date:** [September 23, 2020, 8:31am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/1 "2020-09-23T08:31:49Z")

</div>

I have two approval jobs called `test-qa` and `test-develop`, and the level of jobs is same. I want to cancel other job when I approved specific job. Is it possible?

For example,

1. I approved `test-qa` job → `test-develop` job is canceled or failed
2. I approved `test-develop` job → `test-qa` job is canceled or failed

If it is possible, how can I apply this feature in `config.yml` ?

THX!

---

<div class="post-metadata">

**Author:** ![yannCI](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/yannci/32/24866_2.png) [@yannCI](https://discuss.circleci.com/u/yannCI)\
**Post date:** [October 1, 2020, 2:20pm UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/2 "2020-10-01T14:20:30Z")

</div>

Hi @baeharam,

Although it isn’t possible to directly cancel an approval-type job, you can however cancel the “on\_hold” workflow containing the approval job.

Assuming you have 2 defined jobs (each with their own executor, steps, …etc) in your `config.yml`, here are suggestions on how to proceed.

_Note: You’ll need to create an environment variable containing your_ [_personal API token_](https://circleci.com/docs/2.0/managing-api-tokens/#creating-a-personal-api-token).

&nbsp;

```auto
version: 2.1

jobs:
  job-qa:
    .....
    .....
    .....
    .....
                                                     
  job-develop:
    .....
    .....
    .....
    .....
                                                     
  cancel-other-hold:
    docker:
      - image: circleci/python
    steps:
      - run:
          name: Cancelling the other on_hold workflow
          command: |
              other_on_hold_id=$(curl -G "https://circleci.com/api/v2/pipeline/<<pipeline.id>>/workflow" -H "Circle-Token: $MyToken"|jq -rs --arg CIRCLE_WORKFLOW_ID "$CIRCLE_WORKFLOW_ID" '.[].items[]|select(.status == "on_hold")|select(.id != $CIRCLE_WORKFLOW_ID)|.id')
                    echo $other_on_hold_id
                    curl -X POST "https://circleci.com/api/v2/workflow/${other_on_hold_id}/cancel" -H "Circle-Token: ${MyToken}"
              
                   
workflows:
  qa:
    jobs:
      - job-qa
      - test-qa:
          type: approval
          requires:
            - job-qa
      - cancel-other-hold:
          requires:
            - test-qa
                
  develop:
    jobs:
      - job-develop
      - test-develop:
          type: approval
          requires:
            - job-develop
      - cancel-other-hold:
          requires:
            - test-develop

```

&nbsp;

You could also use a parameterized job:

```auto
version: 2.1

jobs:
  job-qa:
    .....
    .....
    .....
    .....
                                                     
  job-develop:
    .....
    .....
    .....
    .....
   
cancel-on-hold: # defines a parameterized job
    description: A job that just cancels a workflow
    parameters:
      other_wf:
        type: string
    docker:
      - image: circleci/python
    steps:
      - run:
          name: Cancelling the other on_hold workflow
          command: |
              other_on_hold_id=$(curl -G "https://circleci.com/api/v2/pipeline/<<pipeline.id>>/workflow" -H "Circle-Token: $MyToken"|jq -rs --arg parameters.other_wf $parameters.other_wf '.[].items[]|select(.name == "<< parameters.other_wf >>")|.id')
              curl -X POST "https://circleci.com/api/v2/workflow/$other_on_hold_id/cancel" -H "Circle-Token: $MyToken"
              
                   
workflows:
  qa:
    jobs:
      - job-qa
      - test-qa:
          type: approval
          requires:
            - job-qa
      - cancel-on-hold:
          other_wf: develop
          requires:
            - test-qa
                
  develop:
    jobs:
      - job-develop
      - test-develop:
          type: approval
          requires:
            - job-develop
      - cancel-on-hold:
          other_wf: qa
          requires:
            - test-develop

```

&nbsp;

Also, depending on the jobs included in your actual `config.yml`, you could use [`post-steps`](https://circleci.com/docs/2.0/configuration-reference/#pre-steps-and-post-steps-requires-version-21), which would eliminate the need for the additional job.  
But keep in mind that `post-steps` **do not** work if they directly follow an approval-type job.

Here’s an example of `post-steps` you could add:

```auto
          post-steps:
            - run:
                name: Cancelling the other "on_hold" workflow
                command: |
                    other_on_hold_id=$(curl -G "https://circleci.com/api/v2/pipeline/<<pipeline.id>>/workflow" -H "Circle-Token: $MyToken"|jq -rs --arg CIRCLE_WORKFLOW_ID "$CIRCLE_WORKFLOW_ID" '.[].items[]|select(.status == "on_hold")|select(.id != $CIRCLE_WORKFLOW_ID)|.id')
                    echo $other_on_hold_id
                    curl -X POST "https://circleci.com/api/v2/workflow/${other_on_hold_id}/cancel" -H "Circle-Token: ${MyToken}"

```

---

<div class="post-metadata">

**Author:** ![baeharam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/baeharam/32/25130_2.png) [@baeharam](https://discuss.circleci.com/u/baeharam)\
**Post date:** [October 29, 2020, 8:20am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/3 "2020-10-29T08:20:08Z")

</div>

@yannCI

Sorry for the late response, it took many days to apply.  
Your idea is great, but `on_hold` job doesn’t have `job_number` as response. Therefore, I have to approve `approval job` and cancel that job. I think it is best for this case. How do you think?

---

<div class="post-metadata">

**Author:** ![yannCI](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/yannci/32/24866_2.png) [@yannCI](https://discuss.circleci.com/u/yannCI)\
**Post date:** [November 5, 2020, 9:23am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/4 "2020-11-05T09:23:38Z")

</div>

Hi @baeharam,

Indeed, approval jobs don’t have a job number, hence my advice to cancel the whole workflow that contains the approval job.

---

<div class="post-metadata">

**Author:** ![baeharam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/baeharam/32/25130_2.png) [@baeharam](https://discuss.circleci.com/u/baeharam)\
**Post date:** [November 9, 2020, 5:30am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/5 "2020-11-09T05:30:44Z")

</div>

@yannCI

Ah… I understood. But, I cannot cancel whole workflow containing `on_hold` job. Is there other way to only cancel `on_hold` job?

---

<div class="post-metadata">

**Author:** ![yannCI](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/yannci/32/24866_2.png) [@yannCI](https://discuss.circleci.com/u/yannCI)\
**Post date:** [November 11, 2020, 1:04pm UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/6 "2020-11-11T13:04:56Z")

</div>

Hi @baeharam,

As you previously pointed out, it is not possible to directly cancel an `on_hold` job as it doesn’t have a `job_number`.

You could add a workflow that contains the `on_hold` job and another job that sets the value of a parameter, and have the [execution of downstream workflows conditioned by that parameter](https://circleci.com/docs/2.0/configuration-reference/#using-when-in-workflows).

Then if the workflow containing the `on_hold` job is cancelled, the downstream workflows won’t be executed.

---

<div class="post-metadata">

**Author:** ![baeharam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/baeharam/32/25130_2.png) [@baeharam](https://discuss.circleci.com/u/baeharam)\
**Post date:** [November 26, 2020, 6:57am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/7 "2020-11-26T06:57:22Z")

</div>

@yannCI

Sorry for late response.  
I think I have to change CI flow for this solution. Anyway, this is the best solution for `on_hold` job.  
Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [December 6, 2020, 6:57am UTC](https://discuss.circleci.com/t/is-it-possible-to-cancel-job-automatically-when-specific-job-is-approved/37487/8 "2020-12-06T06:57:26Z")

</div>

Closed on December 6, 2020, 6:57am UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
