Skip to content

Commit

Permalink
cancel running workflows from the same branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ivvist committed Apr 21, 2024
1 parent 4fdcef1 commit b6c93d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci_reuse_go_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ on:
short_test:
required: false
type: string
running_workflow:
required: false
type: string
secrets:
reporeading_token:
required: true
Expand Down Expand Up @@ -46,6 +49,15 @@ jobs:
token: ${{ secrets.reporeading_token }}
pr_number: ${{ github.event.number }}

- name: Check if another workflow running and cancel it
if: github.event_name == 'pull_request'
run: curl -s https://raw.githubusercontent.com/untillpro/ci-action/master/scripts/cancelworkflow.sh | bash
env:
GH_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
repo: ${{ GITHUB.REPOSITORY }}
branch: ${{ github.event.pull_request.head.ref }}
running-workflow: ${{ inputs.running_workflow }}

- name: Cache Go - Modules
uses: actions/cache@v4
with:
Expand Down
11 changes: 7 additions & 4 deletions scripts/cancelworkflow.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ if [ "$#" -ne 3 ]; then
exit 0
fi

repo=$1
br=$2
wf=$3
rp=${repo}
br=${branch{
wf=${running-workflow}
echo "repo: ${rp}"
echo "branch: ${br}"
echo "workflow: ${wf}"
# Declare an array to hold the workflow details
declare -a workflows=()
Expand Down Expand Up @@ -35,7 +38,7 @@ while IFS= read -r line; do
i=0
temp=()
fi
done < <(gh run list -b "${br}" --workflow "${wf}" --limit 3 -e pull_request_target -s in_progress --json 'databaseId' --jq '.[] | .databaseId' -R "${repo}")
done < <(gh run list -b "${br}" --workflow "${wf}" --limit 3 -e pull_request_target -s in_progress --json 'databaseId' --jq '.[] | .databaseId' -R "${rp}")
# Output the results
for workflow in "${workflows[@]}"; do
Expand Down

0 comments on commit b6c93d5

Please sign in to comment.