From b6c93d59559331b30ea6ea7655c023c277761ab0 Mon Sep 17 00:00:00 2001 From: ivvist Date: Sun, 21 Apr 2024 16:58:28 +0200 Subject: [PATCH] cancel running workflows from the same branch --- .github/workflows/ci_reuse_go_pr.yml | 12 ++++++++++++ scripts/cancelworkflow.sh | 11 +++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci_reuse_go_pr.yml b/.github/workflows/ci_reuse_go_pr.yml index 9056d70..44211e6 100644 --- a/.github/workflows/ci_reuse_go_pr.yml +++ b/.github/workflows/ci_reuse_go_pr.yml @@ -14,6 +14,9 @@ on: short_test: required: false type: string + running_workflow: + required: false + type: string secrets: reporeading_token: required: true @@ -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: diff --git a/scripts/cancelworkflow.sh b/scripts/cancelworkflow.sh index 744f9f3..7cff5b4 100644 --- a/scripts/cancelworkflow.sh +++ b/scripts/cancelworkflow.sh @@ -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=() @@ -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