Skip to content

Conversation

@xmkg
Copy link
Member

@xmkg xmkg commented Apr 7, 2025

Allow in-progress workflows to be canceled when an external event requests them (e.g., the workflow run has been superseded with a new commit).

MULTI-1939

@xmkg xmkg force-pushed the enhancement/gh-wf-linux-allow-cancel-in-progress branch 2 times, most recently from 8d4cafa to f94535f Compare April 7, 2025 15:11
@xmkg xmkg marked this pull request as draft April 7, 2025 15:12
@xmkg xmkg force-pushed the enhancement/gh-wf-linux-allow-cancel-in-progress branch 2 times, most recently from f91d132 to 378e167 Compare April 8, 2025 07:01
@codecov
Copy link

codecov bot commented Apr 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.53%. Comparing base (896fcb7) to head (614b3bd).
Report is 3421 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4030   +/-   ##
=======================================
  Coverage   89.53%   89.53%           
=======================================
  Files         260      260           
  Lines       14741    14741           
=======================================
  Hits        13198    13198           
  Misses       1543     1543           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xmkg xmkg force-pushed the enhancement/gh-wf-linux-allow-cancel-in-progress branch from 7016758 to 0ca7ffb Compare April 16, 2025 20:16
@ricab
Copy link
Collaborator

ricab commented Apr 16, 2025

Good, this addresses #4048 now too.

@xmkg
Copy link
Member Author

xmkg commented Apr 16, 2025

Good, this addresses #4048 now too.

Github's workflow cancellation is a real headache. The matrix part of the job seems resistant to every kind of cancellation request I've tried, except for force cancel. I'm still investigating why that is. I'll add some trace steps to jobs to see whether the cancellation signals (SIGINT, SIGTERM, etc.) are reaching to the steps at all.

@xmkg
Copy link
Member Author

xmkg commented Apr 16, 2025

Good, this addresses #4048 now too.

Github's workflow cancellation is a real headache. The matrix part of the job seems resistant to every kind of cancellation request I've tried, except for force cancel. I'm still investigating why that is. I'll add some trace steps to jobs to see whether the cancellation signals (SIGINT, SIGTERM, etc.) are reaching to the steps at all.

In case anyone needs I'm using the following bash functions to kill workflows forcefully:

github-kill-workflow(){
gh api \
  --method POST \
  -H "Accept: application/vnd.github+json" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  /repos/$1/actions/runs/$2/force-cancel
}


github-kill-workflow-by-url() {
url="$1"

repo=$(echo "$url" | awk -F'[/?]' '{print $4 "/" $5}')
run_id=$(echo "$url" | awk -F'[/?]' '{print $8}')

echo "repo: $repo"
echo "run_id: $run_id"

github-kill-workflow $repo $run_id

}

Include them in your .bashrc, then use it like

github-kill-workflow-by-url https://github.com/canonical/multipass/actions/runs/14502559305?pr=4030

@xmkg
Copy link
Member Author

xmkg commented Apr 16, 2025

The matrix part of the job seems resistant to every kind of cancellation request I've tried, except for force cancel

Finally figured out why that is.

    if: |
      ${{
        success()
        || needs.Lint.result == 'skipped'
        || ( needs.Lint.result == 'failure'
             && github.event_name == 'pull_request' )
      }}

The BuildAndTest is cancellable after commenting out the precondition.

To cancel the workflow run, the server re-evaluates if conditions for all currently running jobs. If the condition evaluates to true, the job will not get canceled.

So, this expression somehow evaluates to true on re-check, even with !cancelled() && (...) addition. That's enough head-banging for today: I'll wrap this up tomorrow.

@xmkg xmkg force-pushed the enhancement/gh-wf-linux-allow-cancel-in-progress branch 2 times, most recently from fc3b1c9 to 4b1ccb8 Compare April 18, 2025 08:26
xmkg added 3 commits April 18, 2025 11:26
allow in progress workflows to be cancelled when an external event
requests, (e.g. the workflow run has been superseded with a new
commit).

Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
also replace instances of !failed() with success().

Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
@xmkg xmkg force-pushed the enhancement/gh-wf-linux-allow-cancel-in-progress branch from 4b1ccb8 to 566db19 Compare April 18, 2025 08:26
@xmkg xmkg requested a review from ricab April 18, 2025 08:27
@xmkg xmkg marked this pull request as ready for review April 18, 2025 08:27
@xmkg
Copy link
Member Author

xmkg commented Apr 18, 2025

@ricab finally, everything works as it should. A new push cancels the previous runs, and the "cancel workflow" button works.

@xmkg xmkg changed the title [gh/wf/linux.yml] set concurrency.cancel-in-progress to true [gh/wf/linux.yml] allow workflow cancellation Apr 18, 2025
ricab
ricab previously approved these changes Apr 21, 2025
Copy link
Collaborator

@ricab ricab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job Mustafa, thanks! 2 tiny things inline, but pre-approving.

@ricab ricab requested a review from sharder996 April 21, 2025 10:46
Signed-off-by: Mustafa Kemal Gilor <mustafa.gilor@canonical.com>
Comment on lines +71 to +80
if: ${{ !cancelled() &&
(
success() ||
needs.Lint.result == 'skipped' ||
(
needs.Lint.result == 'failure' &&
github.event_name == 'pull_request'
)
)
}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lot of line splitting now, but ok...

Copy link
Collaborator

@ricab ricab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, LGTM, thanks again @xmkg!

@ricab
Copy link
Collaborator

ricab commented Apr 21, 2025

I would just like to get @sharder996's review on this one if possible.

Copy link
Collaborator

@sharder996 sharder996 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I think it looks good. Biggest thing is that Github will group CI runs by head-ref now.

@ricab ricab enabled auto-merge April 21, 2025 16:47
@ricab ricab added this pull request to the merge queue Apr 21, 2025
Merged via the queue into main with commit e598ab0 Apr 22, 2025
15 checks passed
@ricab ricab deleted the enhancement/gh-wf-linux-allow-cancel-in-progress branch April 22, 2025 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants