Skip to content

Commit 7d78d5f

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d98273b commit 7d78d5f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

.github/workflows/main.yml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
ci-config:
1010
runs-on: ubuntu-latest
1111
outputs:
12-
enabled: ${{ steps.check-ref.outputs.enabled }}
12+
enabled: ${{ steps.check-ref.outputs.enabled }}${{ steps.skip-if-redundant.outputs.enabled }}
1313
steps:
1414
- name: try to clone ci-config branch
1515
run: |
@@ -34,6 +34,43 @@ jobs:
3434
enabled=no
3535
fi
3636
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+
}
3774
3875
windows-build:
3976
needs: ci-config

0 commit comments

Comments
 (0)