Skip to content

Commit

Permalink
ci: Fix per commit CI
Browse files Browse the repository at this point in the history
The pull request event does not have write access to the repo, so it
cannot dispatch jobs.
  • Loading branch information
NickeZ committed Aug 20, 2024
1 parent c8f800e commit ca37148
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 74 deletions.
13 changes: 13 additions & 0 deletions .ci/matrix-from-commit-log
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/python

import subprocess
import sys
import json

range = sys.argv[1]

commits = subprocess.run(f'git log --format="%H" {range}', shell=True, text=True, capture_output=True)
commits = commits.stdout.strip().split()

if len(commits) > 0:
print(json.dumps({'commit': commits}))
61 changes: 0 additions & 61 deletions .github/workflows/commit-ci.yml

This file was deleted.

64 changes: 51 additions & 13 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,69 @@
# See reference docs at
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions

name: Pull request CI
on: pull_request

jobs:
pr-ci:
pr-head-ci:
runs-on: ubuntu-22.04
steps:
- name: Clone the repo
uses: actions/checkout@v4
with:
submodules: recursive

- name: Pull container image
run: ./.ci/run-container-ci pull

- name: Run CI in container
run: ./.ci/run-container-ci ${{github.workspace}} ${{ github.base_ref }}

# Generate a list of commits to run CI on
generate-matrix:
runs-on: ubuntu-22.04
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Clone the repo
uses: actions/checkout@v4
with:
#repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.base.sha }}
fetch-depth: 0

# HEAD^2~ because PR branch is second parent and we want to skip the last commit
- name: Dispatch jobs for commits in PR history
# HEAD~ because we want to skip the last commit (which is built in job above)
- name: Create jobs for commits in PR history
id: set-matrix
run: |
echo matrix=$(.ci/matrix-from-commit-log origin/${{github.base_ref}}..${{ github.event.pull_request.head.sha}}~) >> $GITHUB_OUTPUT
# Run this job for every commit in the PR except HEAD.
pr-commit-ci:
runs-on: ubuntu-22.04
needs: [ generate-matrix ]
strategy:
matrix: ${{ fromJSON(needs.generate-matrix.outputs.matrix) }}
if: needs.generate-matrix.outputs.matrix != ''
steps:
- name: Clone the repo
uses: actions/checkout@v4
with:
submodules: recursive
#repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.base.sha }}

- name: Create merge commit
env:
GIT_AUTHOR_NAME: Bot
GIT_AUTHOR_EMAIL: [email protected]
GIT_COMMITTER_NAME: Bot
GIT_COMMITTER_EMAIL: [email protected]
run: |
for commit in $(git log --format="%H" origin/${{github.base_ref}}..HEAD^2~); do
echo ::notice::Dispatching job for $commit
curl -L -s \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
${{ github.api_url }}/repos/${{ github.repository }}/actions/workflows/commit-ci.yml/dispatches \
-d "{\"ref\":\"${{ github.event.pull_request.head.ref }}\",\"inputs\":{\"sha\":\"$commit\",\"base_ref\":\"${{ github.base_ref}}\"}}"
done
git fetch origin ${{ matrix.commit }}
git merge --no-ff --no-edit ${{ matrix.commit }}
echo "merge commit parents:"
git log -1 --format="Head %H, Parents %P"
- name: Pull container image
run: ./.ci/run-container-ci pull
Expand Down

0 comments on commit ca37148

Please sign in to comment.