Skip to content

Commit f4b3c06

Browse files
committed
ci: Run CI for every commit in PR
* Split out Pull Request CI into separate file * Dispatch one job per commit in PR
1 parent ad5a40b commit f4b3c06

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# See reference docs at
22
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
3-
name: ci
4-
on: [push, pull_request]
3+
name: Default branch ci
4+
on:
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- master
59

610
jobs:
711
linux-docker:
@@ -14,4 +18,4 @@ jobs:
1418
- name: Pull CI container image
1519
run: ./.ci/run-container-ci pull
1620
- name: Run CI in container
17-
run: ./.ci/run-container-ci ${{github.workspace}} ${{github.base_ref}}
21+
run: ./.ci/run-container-ci ${{github.workspace}}

.github/workflows/commit-ci.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# See reference docs at
2+
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
3+
name: Per commit CI
4+
on:
5+
workflow_dispatch:
6+
inputs:
7+
sha:
8+
description: Git commit to check
9+
required: true
10+
base_ref:
11+
description: Base reference we are comparing against (e.g., 'master')
12+
required: true
13+
14+
jobs:
15+
ci:
16+
runs-on: ubuntu-22.04
17+
steps:
18+
- name: Set commit status to pending
19+
run: |
20+
curl -L -s \
21+
-X POST \
22+
-H "Accept: application/vnd.github+json" \
23+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
24+
-H "X-GitHub-Api-Version: 2022-11-28" \
25+
${{ github.api_url }}/repos/${{ github.repository }}/statuses/${{ inputs.sha }} \
26+
-d '{"state":"pending","target_url":"${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}","description":"The build started!","context":"ci / per-commit-build"}'
27+
28+
- name: Clone the repo
29+
uses: actions/checkout@v4
30+
with:
31+
submodules: recursive
32+
ref: ${{ inputs.base_ref }}
33+
34+
- name: Create merge commit
35+
env:
36+
GIT_AUTHOR_NAME: Bot
37+
GIT_AUTHOR_EMAIL: bot@bitbox.swiss
38+
GIT_COMMITTER_NAME: Bot
39+
GIT_COMMITTER_EMAIL: bot@bitbox.swiss
40+
run: |
41+
git fetch origin ${{ inputs.sha }}
42+
git merge --no-ff --no-edit ${{ inputs.sha }}
43+
echo "merge commit parents:"
44+
git log -1 --format=%P
45+
46+
- name: Pull container image
47+
run: ./.ci/run-container-ci pull
48+
49+
- name: Run CI in container
50+
run: ./.ci/run-container-ci ${{github.workspace}} ${{ inputs.base_ref }}
51+
52+
- name: Set status
53+
if: always()
54+
run: |
55+
curl -L -s \
56+
-X POST \
57+
-H "Accept: application/vnd.github+json" \
58+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
59+
-H "X-GitHub-Api-Version: 2022-11-28" \
60+
${{ github.api_url }}/repos/${{ github.repository }}/statuses/${{ inputs.sha }} \
61+
-d '{"state":"${{job.status}}","target_url":"${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}","description":"The build ${{ job.status }}!","context":"ci / per-commit-build"}'

.github/workflows/pr-ci.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See reference docs at
2+
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
3+
name: Pull request CI
4+
on: pull_request
5+
6+
jobs:
7+
pr-ci:
8+
runs-on: ubuntu-22.04
9+
steps:
10+
- name: Clone the repo
11+
uses: actions/checkout@v4
12+
with:
13+
submodules: recursive
14+
fetch-depth: 0
15+
16+
# HEAD^2~ because PR branch is second parent and we want to skip the last commit
17+
- name: Dispatch jobs for commits in PR history
18+
run: |
19+
for commit in $(git log --format="%H" origin/${{github.base_ref}}..HEAD^2~); do
20+
echo ::notice::Dispatching job for $commit
21+
curl -L -s \
22+
-X POST \
23+
-H "Accept: application/vnd.github+json" \
24+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
25+
-H "X-GitHub-Api-Version: 2022-11-28" \
26+
${{ github.api_url }}/repos/${{ github.repository }}/actions/workflows/commit-ci.yml/dispatches \
27+
-d "{\"ref\":\"${{ github.event.pull_request.head.ref }}\",\"inputs\":{\"sha\":\"$commit\",\"base_ref\":\"${{ github.base_ref}}\"}}"
28+
done
29+
30+
- name: Pull container image
31+
run: ./.ci/run-container-ci pull
32+
33+
- name: Run CI in container
34+
run: ./.ci/run-container-ci ${{github.workspace}} ${{ github.base_ref }}

0 commit comments

Comments
 (0)