Repository: linz/geostore (language: python)

Issue:
Cancel current GitHub Action workflow (if still running) when subsequent commits are made
### Enabler

<!-- A description of the enabler that covers what needs to be done why it needs to be done. It should be understandable by all members of the team -->

So that we reduce GitHub Actions minutes and costs, we want to ensure that any currently running GitHub Action workflow for a particular Pull Request is cancelled when another commit is made at a later date (i.e. within 40min or before the original run is complete).

When a Pull Request is opened, a GitHub Action workflow is triggered as part of CI. This process is about 30 to 40 minutes in total. Occasionally, we discovered that the changes made in this Pull Request isn't complete or that we may have missed something (or another test has failed). We make the appropriate changes and push the code up. This triggers yet another GitHub Action workflow run. The previous workflow is left running until completion. This seems rather wasteful as the previous workflow is no longer relevant (in most cases).

Reference:
https://stackoverflow.com/a/72408109


#### Acceptance Criteria

<!-- Requirements to accept this enabler as completed -->

-  [ ] Ensure only one GitHub Action workflow run per Pull Request per branch is allowed at any one time. 
-  [ ] Previous workflow run cancelled when new commits are made (or when new workflow is triggered).
-  [ ] Ensure it doesn't affect workflow in another branch (i.e. it doesn't end up cancelling workflow on a different pull request)
-  [ ] Ensure proper clean up is established when an existing run is cancelled (i.e. no artifacts left behind in s3 etc).

#### Additional context

What happens if the step function is in mid run when the job is cancelled? Would it run to completion and output the files onto a s3 bucket that may have been deleted prior? Would it fail?




Discussion / hints:

Fix patch:
diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml
index a2c94bab2..8d4215bfd 100644
--- a/.github/workflows/codeql-analysis.yml
+++ b/.github/workflows/codeql-analysis.yml
@@ -6,6 +6,10 @@ on:
   pull_request:
     branches: [master]
 
+concurrency:
+  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
+  cancel-in-progress: true
+
 jobs:
   CodeQL-Build:
     runs-on: ubuntu-20.04
diff --git a/.github/workflows/package-cli.yml b/.github/workflows/package-cli.yml
index 000a56325..02711e230 100644
--- a/.github/workflows/package-cli.yml
+++ b/.github/workflows/package-cli.yml
@@ -6,6 +6,10 @@ on:
   pull_request:
     types: [opened, reopened, synchronize]
 
+concurrency:
+  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
+  cancel-in-progress: true
+
 jobs:
   build:
     runs-on: ubuntu-20.04


Test patch:
diff --git a/.github/workflows/prod-upgrade-deploy-test.yml b/.github/workflows/prod-upgrade-deploy-test.yml
index da5d2c07e..26ea3030b 100644
--- a/.github/workflows/prod-upgrade-deploy-test.yml
+++ b/.github/workflows/prod-upgrade-deploy-test.yml
@@ -3,6 +3,10 @@ on:
   pull_request:
     types: [labeled, synchronize]
 
+concurrency:
+  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
+  cancel-in-progress: true
+
 jobs:
   test-prod-upgrade:
     if:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index afb9c10e4..2bd3c295c 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -3,6 +3,10 @@ on:
   pull_request:
     types: [opened, reopened, synchronize]
 
+concurrency:
+  group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
+  cancel-in-progress: true
+
 jobs:
   linter:
     runs-on: ubuntu-20.04