Skip to content

Commit

Permalink
ci: Run CI for every commit in PR
Browse files Browse the repository at this point in the history
* Split out Pull Request CI into separate file
* Dispatch one job per commit in PR
  • Loading branch information
NickeZ committed Aug 17, 2024
1 parent ad5a40b commit a6ddf03
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# See reference docs at
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
name: ci
on: [push, pull_request]
name: Default branch ci
on:
workflow_dispatch:
push:
branches:
- master

jobs:
linux-docker:
Expand Down
61 changes: 61 additions & 0 deletions .github/workflows/commit-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# See reference docs at
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions
name: Per commit CI
on:
workflow_dispatch:
inputs:
sha:
description: Git commit to check
required: true
base_ref:
description: Base reference we are comparing against (e.g., 'master')
required: true

jobs:
ci:
runs-on: ubuntu-22.04
steps:
- name: Set commit status to pending
run: |
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 }}/statuses/${{ inputs.sha }} \
-d '{"state":"pending","target_url":"${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}","description":"The build started!","context":"ci / per-commit-build"}'
- name: Clone the repo
uses: actions/checkout@v4
with:
submodules: recursive
ref: ${{ inputs.base_ref }}

- name: Create merge commit
env:
GIT_AUTHOR_NAME: Bot
GIT_AUTHOR_EMAIL: [email protected]
GIT_COMMITTER_NAME: Bot
GIT_COMMITTER_EMAIL: [email protected]
run: |
git fetch origin ${{ inputs.sha }}
git merge --no-ff --no-edit ${{ inputs.sha }}
echo "merge commit parents:"
git log -1 --format=%P
- name: Pull container image
run: ./.ci/run-container-ci pull

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

- name: Set status
if: always()
run: |
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 }}/statuses/${{ inputs.sha }} \
-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"}'
34 changes: 34 additions & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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:
runs-on: ubuntu-22.04
steps:
- name: Clone the repo
uses: actions/checkout@v4
with:
submodules: recursive
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
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
- 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 }}

0 comments on commit a6ddf03

Please sign in to comment.