Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Run CI for every commit in PR #1275

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 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 All @@ -14,4 +18,4 @@ jobs:
- name: Pull CI container image
run: ./.ci/run-container-ci pull
- name: Run CI in container
run: ./.ci/run-container-ci ${{github.workspace}} ${{github.base_ref}}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

github.base_ref is only available when the event type is pull request. Therefore we remove it.

run: ./.ci/run-container-ci ${{github.workspace}}
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 }}