Skip to content

Commit

Permalink
cancel outdated action runs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoucheng361 committed Dec 19, 2024
1 parent 4826ace commit f0d3a43
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
77 changes: 77 additions & 0 deletions .github/actions/cancel-outdate-runs/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: 'Cancel Outdate Runs'
description: 'Cancel Outdate Runs'
inputs:
head_sha:
description: 'head_sha triggers the workflow runs'
required: true
type: string
per_page:
description: 'Page size of runs to cancel'
required: true
type: number
default: 5
page:
description: 'Page number of runs to cancel'
required: true
type: number
default: 1
github_token:
description: 'GITHUB_TOKEN'
required: true
type: string

runs:
using: "composite"
steps:
- name: display parameters
shell: bash
run: |
echo "head_sha is ${{inputs.head_sha}}"
echo "per_page is ${{inputs.per_page}}"
echo "page is ${{inputs.page}}"
- uses: octokit/[email protected]
id: get_active_workflows
with:
route: GET /repos/${{github.repository}}/actions/runs?status=in_progress&event=pull_request&per_page=${{inputs.per_page}}&page=${{inputs.page}}&head_sha=${{inputs.head_sha}}
env:
GITHUB_TOKEN: ${{inputs.github_token}}

- name: display active workflows
shell: bash
env:
data: ${{ steps.get_active_workflows.outputs.data }}
run: |
echo "$data" | jq '.workflow_runs | map({id, head_sha, pull_request_number:.pull_requests[0].number})'
- name: Extract workflow ids
shell: bash
id: extract_workflow_ids
env:
data: ${{ steps.get_active_workflows.outputs.data }}
run: |
echo pull_request_number is ${{ github.event.pull_request.number }}
echo head_sha is ${{ github.event.pull_request.head.sha }}
workflow_ids=$(echo "$data" | \
jq '.workflow_runs | map({id, head_sha, pull_request_number:.pull_requests[0].number})' | \
jq 'map(select( .pull_request_number == ${{ github.event.pull_request.number }} and .head_sha != "${{ github.event.pull_request.head.sha }}")) | map(.id)' | \
jq 'join(",")')
echo workflow_ids is $workflow_ids
echo 'WORKFLOW_IDS='$(echo $workflow_ids | tr -d '"') >> $GITHUB_ENV
- name: Cancel active workflows
shell: bash
run: |
for i in ${WORKFLOW_IDS//,/ }
do
echo "Cancelling workflow with id: $i"
# use curl here as I have no idea how to use a github action in a loop
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{inputs.github_token}}" \
https://api.github.com/repos/${{ github.repository }}/actions/runs/$i/cancel
done
69 changes: 69 additions & 0 deletions .github/workflows/cancel_outdate_runs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: cancel_outdate_runs
on:
pull_request:
branches:
- main
- release**
env:
OSS_PREFIX: oss://juicefs-com-static

jobs:
cancel-outdate-runs:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
with :
fetch-depth: 1

- name: Install ossutils
timeout-minutes: 3
run: |
wget -O /usr/local/bin/ossutil64 https://gosspublic.alicdn.com/ossutil/1.7.9/ossutil64
chmod +x /usr/local/bin/ossutil64
ossutil64 config --access-key-id=${{secrets.OSS_KEY_ID}} --access-key-secret=${{ secrets.OSS_KEY_SECRET }} -e oss-cn-shanghai.aliyuncs.com
- name: Get previours head_sha from oss
timeout-minutes: 1
id: get_previous_head_sha
run: |
pr_number=${{ github.event.pull_request.number }}
echo "downloaded head_sha from https://juicefs-com-static.oss-cn-shanghai.aliyuncs.com/head_sha/${pr_number}"
ossutil64 cp -f ${OSS_PREFIX}/head_sha/${pr_number} previous_head_sha || echo "no previous head sha found"
if [ ! -f previous_head_sha ]; then
echo "no previous head sha found"
echo "::set-output name=exist::false"
exit 0
else
echo "::set-output name=exist::true"
previous_head_sha=$(cat previous_head_sha)
echo "previous head sha is ${previous_head_sha}"
echo "previous_head_sha=${previous_head_sha}" >> $GITHUB_ENV
fi
- name: Upload head_sha to oss
timeout-minutes: 3
run: |
pr_number=${{ github.event.pull_request.number }}
echo ${{ github.event.pull_request.head.sha }} > head_sha
ossutil64 cp -f head_sha "${OSS_PREFIX}/head_sha/${pr_number}"
echo "uploaded head_sha to https://juicefs-com-static.oss-cn-shanghai.aliyuncs.com/head_sha/${pr_number}"
- name : Cancel Outdate Runs
uses: ./.github/actions/cancel-outdate-runs
with:
per_page: 8
page: 1
head_sha: ${{ env.previous_head_sha }}
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Wait Runs Cancelled
run: |
sleep 10s
- name : Cancel Outdate Runs
uses: ./.github/actions/cancel-outdate-runs
with:
per_page: 8
page: 1
head_sha: ${{ env.previous_head_sha }}
github_token: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit f0d3a43

Please sign in to comment.