How can I stop reusable workflows called from a caller workflow in GitHub Actions?

235 views Asked by At

I have a workflow which is calling a reusable workflow. This caller workflow is defined with

name: My CI Pipeline

on:
  push:
    branches: [ "master" ]

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
  ...

When I push changes in my repository the Action Runner starts the workflow and this calls a reusable workflow (which, in turn, calls a further reusable workflow, ...). Fine! If another push into the repository takes place the starting workflow gets interrupted. Fine! The problem: The called reusable workflow continues to run.

Because I rely on a value stored in a repository variable during processing the first reusable workflow I may run into problems. Hence, how can I stop all called workflows when the concurrency feature takes place?

At the end of the main workflow I have

  run_build_wf:
    needs: check_changes
    if: needs.check_changes.outputs.changed_part_be == 'backend' || needs.check_changes.outputs.changed_part_fe == 'frontend'
    uses: ./.github/workflows/build_workflow.yml
    secrets: inherit
    with:
      changed_part_be: ${{needs.check_changes.outputs.changed_part_be}}
      changed_part_fe: ${{needs.check_changes.outputs.changed_part_fe}}

Do I have to put some concurrency stuff in the called workflow?

3

There are 3 answers

2
Krzysztof Madej On

Concurrency is also available on reusable workflows.

Please try this

jobs:
  checks:
    concurrency:
      group: ${{ github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/unit_tests_and_linters.yaml
0
du-it On

Just putting

name: My called reusable workflow

on:
  workflow_call:

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

in each called (reusable) workflow seems to do the magic.

0
Alvin On

You can add concurrency to the jobs calling reuable workflows, specify the group with the name of jobs.

e.g.,

...

jobs:
  job1:
    ...

  reusable1:
    concurrency:
      group: reusable1-${{ github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/reusable1.yaml
      
  reusable2:
    concurrency:
      group: reusable2-${{ github.ref }}
      cancel-in-progress: true
    uses: ./.github/workflows/reusable2.yaml
...

Though it's not a graceful way. :P