-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
85 lines (72 loc) · 3.05 KB
/
Copy pathaction.yml
File metadata and controls
85 lines (72 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
name: Trigger CI with empty commit
description: Push an empty commit to a PR branch to trigger CI, or explain why push is not possible.
author: Shamrock-code
branding:
icon: refresh-cw
color: green
inputs:
github_token:
description: PAT used for label removal and pushing to the PR head branch.
required: true
label:
description: Label to remove and mention in the fork PR comment.
required: false
default: trigger-ci
runs:
using: composite
steps:
- name: Remove label
uses: actions-ecosystem/action-remove-labels@v1
with:
labels: ${{ inputs.label }}
github_token: ${{ inputs.github_token }}
- name: Determine if we can push to the PR branch
id: can_push
shell: bash
run: |
IS_FORK="${{ github.event.pull_request.head.repo.full_name != github.repository }}"
MAINTAINER_CAN_MODIFY="${{ github.event.pull_request.maintainer_can_modify }}"
if [ "$IS_FORK" = "true" ] && [ "$MAINTAINER_CAN_MODIFY" != "true" ]; then
echo "can_push=false" >> "$GITHUB_OUTPUT"
echo "Cannot push: PR is from a fork and 'Allow edits from maintainers' is disabled."
exit 0
fi
echo "can_push=true" >> "$GITHUB_OUTPUT"
- name: Checkout PR branch
if: steps.can_push.outputs.can_push == 'true'
uses: actions/checkout@v7
with:
fetch-depth: 0
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
token: ${{ inputs.github_token }}
allow-unsafe-pr-checkout: true
- name: Create and push empty commit
if: steps.can_push.outputs.can_push == 'true'
shell: bash
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
git config --global --add safe.directory '*'
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git commit --allow-empty -m "[gh-action] trigger CI with empty commit"
git push origin "HEAD:${HEAD_REF}"
- name: Explain why we could not push (fork PRs without maintainer edits)
if: steps.can_push.outputs.can_push != 'true'
uses: actions/github-script@v6
with:
github-token: ${{ inputs.github_token }}
script: |
const label = '${{ inputs.label }}';
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.payload.pull_request.number;
const msg = [
"I can't push the empty commit to this PR branch because it comes from a fork and **'Allow edits from maintainers'** is disabled.",
"",
"To trigger CI, either:",
`- enable **Allow edits from maintainers** on the PR, then re-add the \`${label}\` label, or`,
"- push an empty commit yourself with message `[gh-action] trigger CI with empty commit`."
].join("\n");
await github.rest.issues.createComment({ owner, repo, issue_number, body: msg });