Files

103 lines
3.5 KiB
YAML

name: Workflow Stopper
on:
workflow_dispatch:
inputs:
github_sha:
description: 'The SHA of the commit to stop workflows for'
required: true
github_repository:
description: 'The repository (owner/repo) to stop workflows in'
required: true
github_ref_name:
description: 'The ref name (branch) to stop workflows for'
required: true
github_workflow_id:
description: 'The workflow run ID that triggered this'
required: true
github_job_name:
description: 'The job name that triggered this'
required: true
jobs:
stop-workflows:
runs-on: ubuntu-latest
steps:
- name: Generate App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ secrets.WORKFLOW_STOPPER_APP_ID }}
private-key: ${{ secrets.WORKFLOW_STOPPER_APP_KEY }}
owner: pezkuwichain
repositories: pezkuwi-sdk
- name: Cancel running workflows
uses: actions/github-script@v7
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const [owner, repo] = '${{ github.event.inputs.github_repository }}'.split('/');
const sha = '${{ github.event.inputs.github_sha }}';
const triggerRunId = '${{ github.event.inputs.github_workflow_id }}';
const triggerJob = '${{ github.event.inputs.github_job_name }}';
console.log(`Stopping workflows for ${owner}/${repo} at SHA ${sha}`);
console.log(`Triggered by run ${triggerRunId}, job ${triggerJob}`);
// Get all running workflow runs for this SHA
const { data: runs } = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
status: 'in_progress',
head_sha: sha,
per_page: 100
});
console.log(`Found ${runs.workflow_runs.length} running workflows`);
// Cancel each running workflow (except the trigger)
for (const run of runs.workflow_runs) {
if (run.id.toString() === triggerRunId) {
console.log(`Skipping trigger workflow run ${run.id}`);
continue;
}
try {
await github.rest.actions.cancelWorkflowRun({
owner,
repo,
run_id: run.id
});
console.log(`Cancelled workflow run ${run.id} (${run.name})`);
} catch (error) {
console.log(`Failed to cancel run ${run.id}: ${error.message}`);
}
}
// Also get queued workflows
const { data: queuedRuns } = await github.rest.actions.listWorkflowRunsForRepo({
owner,
repo,
status: 'queued',
head_sha: sha,
per_page: 100
});
console.log(`Found ${queuedRuns.workflow_runs.length} queued workflows`);
for (const run of queuedRuns.workflow_runs) {
try {
await github.rest.actions.cancelWorkflowRun({
owner,
repo,
run_id: run.id
});
console.log(`Cancelled queued workflow run ${run.id} (${run.name})`);
} catch (error) {
console.log(`Failed to cancel queued run ${run.id}: ${error.message}`);
}
}
console.log('Workflow stopping complete');