Unverified 提交 909a00b0 authored 作者: Will Chen's avatar Will Chen 提交者: GitHub

Add workflow to cancel CI runs after PR merge (#2315)

#skip-bb ## Summary This PR adds a new GitHub Actions workflow that automatically cancels in-progress and queued CI workflow runs when a pull request is merged. This helps reduce unnecessary CI resource consumption and prevents stale workflow runs from completing after their changes have already been integrated. ## Changes - Added `.github/workflows/cancel-ci-after-merge.yml` workflow that: - Triggers when a pull request is closed (merged) - Finds the CI workflow in the repository - Queries for all in-progress and queued CI runs on the merged PR's branch - Cancels each identified workflow run with error handling and logging - Includes comprehensive console logging for debugging and monitoring ## Implementation Details - The workflow uses `actions/github-script@v7` to interact with the GitHub Actions API - It specifically targets the "CI" workflow by name - Handles both "in_progress" and "queued" statuses to catch all active runs - Includes try-catch error handling to gracefully handle cancellation failures - Requires `actions: write` permission to cancel workflow runs - Only executes when `github.event.pull_request.merged == true` to avoid running on rejected PRs <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Automatically cancel active CI runs for the merged PR’s head commit. Frees up runners and prevents stale builds from finishing. - **New Features** - Adds .github/workflows/cancel-ci-after-merge.yml triggered on PR closed, gated to merged PRs. - Targets the CI workflow via file path ci.yml and lists runs for the PR head SHA. - Cancels active runs (in_progress, queued, pending, waiting) with try/catch error handling and de-duplicates by run ID. - Supports fork and non-fork PRs; uses actions/github-script@v7 with actions: write permissions and clear console logs. <sup>Written for commit 4d631b3ccb9d127c4781b185b9d6eac90d2fd710. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --------- Co-authored-by: 's avatarClaude <noreply@anthropic.com>
上级 b402b089
name: Cancel CI after merge
on:
pull_request:
types: [closed]
jobs:
cancel-ci:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
actions: write
steps:
- name: Cancel CI workflows for merged PR branch
uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const pr = context.payload.pull_request;
// Use head SHA instead of branch name - works for both fork and non-fork PRs
// since workflow runs are registered to the base repo regardless
const headSha = pr.head.sha;
console.log(`Looking for CI workflows to cancel for SHA: ${headSha}`);
// Use workflow file path instead of display name for reliable matching
const workflowFile = 'ci.yml';
// Query all cancelable statuses: in_progress, queued, pending, waiting
const statuses = ['in_progress', 'queued', 'pending', 'waiting'];
const allRuns = [];
for (const status of statuses) {
try {
const { data } = await github.rest.actions.listWorkflowRuns({
owner,
repo,
workflow_id: workflowFile,
head_sha: headSha,
status,
});
allRuns.push(...data.workflow_runs);
} catch (error) {
// Status may not be supported or no runs found
console.log(`No runs found for status '${status}': ${error.message}`);
}
}
if (allRuns.length === 0) {
console.log('No cancelable CI workflows found for this SHA');
return;
}
// Deduplicate runs by ID (in case a run appears in multiple status queries)
const uniqueRuns = [...new Map(allRuns.map(run => [run.id, run])).values()];
console.log(`Found ${uniqueRuns.length} workflow run(s) to cancel`);
// Cancel each workflow run
for (const run of uniqueRuns) {
console.log(`Cancelling workflow run ${run.id} (status: ${run.status})`);
try {
await github.rest.actions.cancelWorkflowRun({
owner,
repo,
run_id: run.id,
});
console.log(`Successfully cancelled workflow run ${run.id}`);
} catch (error) {
console.log(`Failed to cancel workflow run ${run.id}: ${error.message}`);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论