From cbf1404c2d0fed812f5a87cc793c7163367b331b Mon Sep 17 00:00:00 2001 From: Priyank Patel Date: Mon, 3 Aug 2020 16:50:13 +0000 Subject: [PATCH] github-actions: Cancel previous runs on pushes to forks and PRs. It only cancel previous runs on forks or for pull request builds and does not run for zulip/zulip repo pushes. It finishes pretty quickly (under 1 minute) so it fine to have it as a workflow rather than to add a new step under every single job to cancel it's previous runs. The only downside to this is that GitHub creates a notification for the cancelled job just like it does for failed jobs!!! --- .github/workflows/cancel-previous-runs.yml | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/cancel-previous-runs.yml diff --git a/.github/workflows/cancel-previous-runs.yml b/.github/workflows/cancel-previous-runs.yml new file mode 100644 index 0000000000..87f6ee79c3 --- /dev/null +++ b/.github/workflows/cancel-previous-runs.yml @@ -0,0 +1,41 @@ +name: Cancel Previous Runs +on: [push, pull_request] + +defaults: + run: + shell: bash + +jobs: + cancel: + name: Cancel Previous Runs + runs-on: ubuntu-latest + timeout-minutes: 3 + + # Don't run this job for zulip/zulip pushes since we + # want to run those jobs. + if: ${{ github.event_name != 'push' || github.event.repository.full_name != 'zulip/zulip' }} + + steps: + # We get workflow IDs from GitHub API so we don't have to maintain + # a hard-coded list of IDs which need to be updated when a workflow + # is added or removed. And, workflow IDs are diffrent for other forks + # so this is required. + - name: Get workflow IDs. + id: workflow_ids + env: + # This is in / format e.g. zulip/zulip + REPOSITORY: ${{ github.repository }} + run: | + workflow_api_url=https://api.github.com/repos/$REPOSITORY/actions/workflows + curl $workflow_api_url -o workflows.json + + script="const {workflows} = require('./workflows'); \ + const ids = workflows.map(workflow => workflow.id); \ + console.log(ids.join(','));" + ids=$(node -e "$script") + echo "::set-output name=ids::$ids" + + - uses: styfle/cancel-workflow-action@0.4.1 + with: + workflow_id: ${{ steps.workflow_ids.outputs.ids }} + access_token: ${{ github.token }}