@matheusbsbs / matheus-bagliano / commits / 7d78d5fc1a
Fork 0

ci: skip GitHub workflow runs for already-tested commits/trees

When pushing a commit that has already passed a CI or PR build successfully, it makes sense to save some energy and time and skip the new build. Let's teach our GitHub workflow to do that. For good measure, we also compare the tree ID, which is what we actually test (the commit ID might have changed due to a reworded commit message, which should not affect the outcome of the run). Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Oct 8, 2020 at 15:29 UTC 7d78d5fc1a91b683dde970e5e48b6d9a873cfd99
1 file changed +38 -1
.github/workflows/main.yml
+38 -1
@@ -9,7 +9,7 @@ jobs:
9 ci-config:
10 runs-on: ubuntu-latest
11 outputs:
12 - enabled: ${{ steps.check-ref.outputs.enabled }}
12 + enabled: ${{ steps.check-ref.outputs.enabled }}${{ steps.skip-if-redundant.outputs.enabled }}
13 steps:
14 - name: try to clone ci-config branch
15 run: |
@@ -34,6 +34,43 @@ jobs:
34 enabled=no
35 fi
36 echo "::set-output name=enabled::$enabled"
37 + - name: skip if the commit or tree was already tested
38 + id: skip-if-redundant
39 + uses: actions/github-script@v3
40 + if: steps.check-ref.outputs.enabled == 'yes'
41 + with:
42 + github-token: ${{secrets.GITHUB_TOKEN}}
43 + script: |
44 + // Figure out workflow ID, commit and tree
45 + const { data: run } = await github.actions.getWorkflowRun({
46 + owner: context.repo.owner,
47 + repo: context.repo.repo,
48 + run_id: context.runId,
49 + });
50 + const workflow_id = run.workflow_id;
51 + const head_sha = run.head_sha;
52 + const tree_id = run.head_commit.tree_id;
53 +
54 + // See whether there is a successful run for that commit or tree
55 + const { data: runs } = await github.actions.listWorkflowRuns({
56 + owner: context.repo.owner,
57 + repo: context.repo.repo,
58 + per_page: 500,
59 + status: 'success',
60 + workflow_id,
61 + });
62 + for (const run of runs.workflow_runs) {
63 + if (head_sha === run.head_sha) {
64 + core.warning(`Successful run for the commit ${head_sha}: ${run.html_url}`);
65 + core.setOutput('enabled', ' but skip');
66 + break;
67 + }
68 + if (tree_id === run.head_commit.tree_id) {
69 + core.warning(`Successful run for the tree ${tree_id}: ${run.html_url}`);
70 + core.setOutput('enabled', ' but skip');
71 + break;
72 + }
73 + }
74
75 windows-build:
76 needs: ci-config