docs(cn): translate FAQs document #2
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cancel Workflows on PR Merge | |
on: | |
pull_request: | |
types: [closed] | |
jobs: | |
cancel_workflows: | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
steps: | |
# Wait a moment to ensure that all the CI actions we want to cancel have | |
# been initiated. In the future, consider whether a more precise mechanism | |
# is needed to ensure that all actions that should be canceled can be | |
# properly canceled. | |
- name: Wait for workflows to be initiated | |
run: sleep 30 | |
- name: Cancel Related Workflow Runs | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const { owner, repo } = context.repo; | |
const prNumber = context.payload.pull_request.number; | |
const headSha = context.payload.pull_request.head.sha; | |
// Get the current workflow's run ID. | |
const currentRunId = context.run_id; | |
console.log(`Cancelling workflows related to PR #${prNumber} (SHA: ${headSha})`); | |
// Retrieve all workflow runs associated with the head SHA of the PR. | |
const runs = await github.rest.actions.listWorkflowRunsForRepo({ | |
owner, | |
repo, | |
head_sha: headSha, | |
status: 'in_progress', | |
}); | |
if (runs.data.workflow_runs.length === 0) { | |
console.log('No workflow runs found to cancel.'); | |
return; | |
} | |
// Cancel all related workflow runs. | |
for (const run of runs.data.workflow_runs) { | |
if (run.id === currentRunId) { | |
console.log(`Skipping cancellation of current workflow run: ${run.id} (${run.name})`); | |
continue; | |
} | |
console.log(`Cancel workflow run: ${run.id} (${run.name})`); | |
await github.rest.actions.cancelWorkflowRun({ | |
owner, | |
repo, | |
run_id: run.id, | |
}); | |
} | |
console.log('All related workflow runs have been canceled.'); |