Skip to content

Commit

Permalink
chore(preview_clear): gh-pages にダミーのコミットを残す
Browse files Browse the repository at this point in the history
#1401 (comment)

gh-pages からコミットがなくなると GitHub Pages の設定が消滅してしまう
ようなので、代わりにダミーの空コミット "preview_clear" を残すことにす
る。このダミーのコミットは次の preview_clear 実行時に必要であれば作り
直す。

* 追加変更: git config でユーザー・Eメールアドレスを設定する時に
  "--global" は指定しないことにする。ローカルのリポジトリで指定すれば
  十分のため。

* 追加変更: ロジックを分かりやすいように整理 & コメントで説明。
  • Loading branch information
akinomyoga committed Jan 21, 2025
1 parent b394b14 commit 70e3b76
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions .github/workflows/preview_clear.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,42 @@ jobs:
# Note: 以下の 999 という数は適当な大きな数。もし open PR の数が 999
# を超えるようならば、この数はもっと大きくする必要がある。
PREVIEW_CLEAR_PR_LIST=$(gh pr list --limit=999 --json number -q '.[].number' | tr '\n' ' ')
PREVIEW_CLEAR_PR_LIST=${PREVIEW_CLEAR_PR_LIST% }
export PREVIEW_CLEAR_PR_LIST
CPPREFJP_OPEN_PR_LIST=$(gh pr list --limit=999 --json number -q '.[].number' | tr '\n' ' ')
CPPREFJP_OPEN_PR_LIST=${CPPREFJP_OPEN_PR_LIST% }
export CPPREFJP_OPEN_PR_LIST
git config --global user.name 'github-actions[bot]'
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
git filter-branch --commit-filter '
pr=$(git log -n 1 --pretty=format:%s "$GIT_COMMIT" | sed -n '\''s/^Preview PR \([0-9]\{1,\}\).*/\1/p'\'')
case " $PREVIEW_CLEAR_PR_LIST | " in
(*" $pr "*)
git commit-tree "$@" ;;
(*)
skip_commit "$@" ;;
esac
commit_message=$(git log -n 1 --pretty=format:%s "$GIT_COMMIT")
if [ "$commit_message" = preview_clear ]; then
# drop a dummy commit by previous preview_clear
skip_commit "$@"
exit
fi
pr=$(echo "$commit_message" | sed -n '\''s/^Preview PR \([0-9]\{1,\}\).*/\1/p'\'')
if [ -n "$pr" ]; then
case " $CPPREFJP_OPEN_PR_LIST " in
(*" $pr "*)
# preserve a commit for an open PR
git commit-tree "$@" ;;
(*)
# drop a commit for a closed PR
skip_commit "$@" ;;
esac
exit
fi
# we preserve all the other commits
git commit-tree "$@"
' HEAD || true
commit_count=$(git rev-list HEAD --count || true)
if [ -z "$commit_count" ] || [ "$commit_count" = 0 ]; then
# When no commits are left, we remove the branch
git push -f origin :gh-pages
else
git push -f origin gh-pages
# When no commits are left, we create a dummy commit
git reset
git commit --allow-empty -m 'preview_clear'
fi
git push -f origin gh-pages

0 comments on commit 70e3b76

Please sign in to comment.