Skip to content

Commit 44f6dba

Browse files
committed
ci: Fix per commit CI
The pull request event does not have write access to the repo, so it cannot dispatch jobs.
1 parent c8f800e commit 44f6dba

File tree

3 files changed

+62
-74
lines changed

3 files changed

+62
-74
lines changed

.ci/matrix-from-commit-log

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python
2+
3+
import subprocess
4+
import sys
5+
import json
6+
7+
range = sys.argv[1]
8+
9+
commits = subprocess.run(f'git log --format="%H" {range}', shell=True, text=True, capture_output=True)
10+
commits = commits.stdout.strip().split()
11+
12+
if len(commits) > 0:
13+
print(json.dumps({'commit': commits}))

.github/workflows/commit-ci.yml

Lines changed: 0 additions & 61 deletions
This file was deleted.

.github/workflows/pr-ci.yml

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,67 @@
11
# See reference docs at
22
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
3+
34
name: Pull request CI
45
on: pull_request
56

67
jobs:
7-
pr-ci:
8+
pr-head-ci:
9+
runs-on: ubuntu-22.04
10+
steps:
11+
- name: Clone the repo
12+
uses: actions/checkout@v4
13+
with:
14+
submodules: recursive
15+
16+
- name: Pull container image
17+
run: ./.ci/run-container-ci pull
18+
19+
- name: Run CI in container
20+
run: ./.ci/run-container-ci ${{github.workspace}} ${{ github.base_ref }}
21+
22+
# Generate a list of commits to run CI on
23+
generate-matrix:
24+
runs-on: ubuntu-22.04
25+
outputs:
26+
matrix: ${{ steps.set-matrix.outputs.matrix }}
27+
steps:
28+
- name: Clone the repo
29+
uses: actions/checkout@v4
30+
with:
31+
fetch-depth: 0
32+
33+
# HEAD~ because we want to skip the last commit (which is built in job above)
34+
- name: Create jobs for commits in PR history
35+
id: set-matrix
36+
run: |
37+
echo matrix=$(.ci/matrix-from-commit-log origin/${{github.base_ref}}..${{ github.event.pull_request.head.sha}}~) >> $GITHUB_OUTPUT
38+
39+
# Run this job for every commit in the PR except HEAD.
40+
pr-commit-ci:
841
runs-on: ubuntu-22.04
42+
needs: [ generate-matrix ]
43+
strategy:
44+
matrix: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }}
45+
if: needs.generate-matrix.outputs.matrix != ''
946
steps:
1047
- name: Clone the repo
1148
uses: actions/checkout@v4
1249
with:
1350
submodules: recursive
1451
fetch-depth: 0
52+
ref: ${{ github.event.pull_request.base.sha }}
1553

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
54+
- name: Create merge commit
55+
env:
56+
GIT_AUTHOR_NAME: Bot
57+
GIT_AUTHOR_EMAIL: bot@bitbox.swiss
58+
GIT_COMMITTER_NAME: Bot
59+
GIT_COMMITTER_EMAIL: bot@bitbox.swiss
1860
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
61+
git fetch origin ${{ matrix.commit }}
62+
git merge --no-ff --no-edit ${{ matrix.commit }}
63+
echo "merge commit parents:"
64+
git log -1 --format="Head %H, Parents %P"
2965
3066
- name: Pull container image
3167
run: ./.ci/run-container-ci pull

0 commit comments

Comments
 (0)