Skip to content

Commit d1821d2

Browse files
authored
[CI] try to trigger CI using empty commits (#1438)
1 parent 0f0f172 commit d1821d2

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: trigger-ci-empty-commit
2+
3+
on:
4+
pull_request_target:
5+
types: [labeled]
6+
7+
concurrency:
8+
group: trigger-ci-${{ github.event.pull_request.number }}
9+
cancel-in-progress: false
10+
11+
12+
13+
jobs:
14+
trigger_ci:
15+
name: Trigger CI with empty commit
16+
if: ${{ github.event.label.name == 'trigger-ci' }}
17+
runs-on: ubuntu-latest
18+
19+
permissions:
20+
contents: write
21+
issues: write
22+
23+
steps:
24+
25+
- name: Remove label trigger-ci
26+
uses: actions-ecosystem/action-remove-labels@v1
27+
with:
28+
labels: trigger-ci
29+
30+
- name: Determine if we can push to the PR branch
31+
id: can_push
32+
run: |
33+
IS_FORK="${{ github.event.pull_request.head.repo.full_name != github.repository }}"
34+
MAINTAINER_CAN_MODIFY="${{ github.event.pull_request.maintainer_can_modify }}"
35+
36+
if [ "$IS_FORK" = "true" ] && [ "$MAINTAINER_CAN_MODIFY" != "true" ]; then
37+
echo "can_push=false" >> "$GITHUB_OUTPUT"
38+
echo "Cannot push: PR is from a fork and 'Allow edits from maintainers' is disabled."
39+
exit 0
40+
fi
41+
42+
echo "can_push=true" >> "$GITHUB_OUTPUT"
43+
44+
- name: Checkout PR branch
45+
if: steps.can_push.outputs.can_push == 'true'
46+
uses: actions/checkout@v4
47+
with:
48+
fetch-depth: 0
49+
repository: ${{ github.event.pull_request.head.repo.full_name }}
50+
ref: ${{ github.event.pull_request.head.ref }}
51+
token: ${{ secrets.GITHUB_TOKEN }}
52+
53+
- name: Create and push empty commit
54+
if: steps.can_push.outputs.can_push == 'true'
55+
env:
56+
HEAD_REF: ${{ github.event.pull_request.head.ref }}
57+
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
58+
run: |
59+
git config --global --add safe.directory '*'
60+
git config user.name "github-actions[bot]"
61+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
62+
63+
git commit --allow-empty -m "[gh-action] trigger CI with empty commit"
64+
git push origin "HEAD:${HEAD_REF}"
65+
66+
- name: Explain why we could not push (fork PRs without maintainer edits)
67+
if: steps.can_push.outputs.can_push != 'true'
68+
uses: actions/github-script@v6
69+
with:
70+
script: |
71+
const owner = context.repo.owner;
72+
const repo = context.repo.repo;
73+
const issue_number = context.payload.pull_request.number;
74+
75+
const msg = [
76+
"I can't push the empty commit to this PR branch because it comes from a fork and **'Allow edits from maintainers'** is disabled.",
77+
"",
78+
"To trigger CI, either:",
79+
"- enable **Allow edits from maintainers** on the PR, then re-add the `trigger-ci` label, or",
80+
"- push an empty commit yourself with message `[gh-action] trigger CI with empty commit`."
81+
].join("\n");
82+
83+
await github.rest.issues.createComment({ owner, repo, issue_number, body: msg });

0 commit comments

Comments
 (0)