1
1
name : " Action to commit changes to the repository"
2
+ inputs :
3
+ token :
4
+ description : " GitHub token"
5
+ required : true
2
6
outputs :
3
7
sha :
4
8
description : " SHA of generated commit"
@@ -7,23 +11,47 @@ outputs:
7
11
runs :
8
12
using : " composite"
9
13
steps :
10
- - name : Commit if changed
14
+ - name : Commit if changed, create a PR if protected
11
15
id : commit
16
+ env :
17
+ GITHUB_TOKEN : ${{ inputs.token }}
12
18
run : |
13
19
set -x
14
20
if [ -n "$(git status --porcelain)" ]; then
15
21
echo "Changed"
16
- git fetch
17
- if [ -n "${GITHUB_HEAD_REF}" ]; then
22
+ protected=${{ github.ref_protected }}
23
+ if [ "${protected}" = "true" ]; then
24
+ current_branch=$(git branch --show-current)
25
+ new_branch=gha-commit
26
+ git checkout -b ${new_branch}
18
27
git add .
19
- git stash save
20
- git switch ${GITHUB_HEAD_REF}
21
- git merge origin/${GITHUB_BASE_REF} --no-edit
22
- git stash pop
28
+ git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
29
+ # Force-push, used in only one place
30
+ # Alternative: separate branch names for each usage
31
+ git push -u origin HEAD -f
32
+
33
+ existing_pr=$(gh pr list --state open --base main --head ${new_branch} --json number --jq '.[] | .number')
34
+ if [ -n "${existing_pr}" ]; then
35
+ echo "Existing PR: ${existing_pr}"
36
+ else
37
+ gh pr create --base main --head ${new_branch} --title "chore: Auto-update from GitHub Actions" --body "Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
38
+ fi
39
+
40
+ gh workflow run rcc -f ref=$(git rev-parse HEAD)
41
+ else
42
+ git fetch
43
+ if [ -n "${GITHUB_HEAD_REF}" ]; then
44
+ git add .
45
+ git stash save
46
+ git switch ${GITHUB_HEAD_REF}
47
+ git merge origin/${GITHUB_BASE_REF} --no-edit
48
+ git stash pop
49
+ fi
50
+ git add .
51
+ git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
52
+ git push -u origin HEAD
23
53
fi
24
- git add .
25
- git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
26
- git push -u origin HEAD
27
- echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
28
54
fi
55
+ # Unconditionally set the output because it could come from a manually triggered run
56
+ echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
29
57
shell : bash
0 commit comments