58 lines
1.9 KiB
YAML
58 lines
1.9 KiB
YAML
name: Cleanup Old Commits
|
||
|
||
on:
|
||
schedule:
|
||
- cron: '23 2 * * *' # 每天凌晨执行一次
|
||
workflow_dispatch: # 允许手动触发
|
||
|
||
jobs:
|
||
clean-history:
|
||
runs-on: ubuntu-latest
|
||
|
||
steps:
|
||
- name: Check out repository
|
||
uses: actions/checkout@v2
|
||
with:
|
||
fetch-depth: 0 # 完整拉取提交历史
|
||
|
||
- name: Set up Git user identity
|
||
run: |
|
||
git config --local user.name "Your Name"
|
||
git config --local user.email "your.email@example.com"
|
||
|
||
- name: Verify current branch
|
||
run: |
|
||
CURRENT_BRANCH=$(git branch --show-current)
|
||
if [ "$CURRENT_BRANCH" != "main" ]; then
|
||
echo "当前分支不是 main,停止执行。"
|
||
exit 1
|
||
fi
|
||
|
||
- name: Create a new branch with an empty commit
|
||
run: |
|
||
git checkout --orphan new_main # 创建一个新的孤立分支
|
||
git commit --allow-empty -m "Initial commit for new_main" # 创建一个空的初始提交
|
||
echo "新分支 new_main 已创建"
|
||
|
||
- name: Cherry-pick the last three commits
|
||
run: |
|
||
for commit in $(git rev-list -n 3 main | tac); do
|
||
echo "正在 cherry-pick 提交: $commit"
|
||
git cherry-pick $commit || {
|
||
echo "冲突发生,尝试处理冲突提交: $commit"
|
||
if git diff-index --quiet HEAD --; then
|
||
echo "没有变化,直接跳过提交: $commit"
|
||
git cherry-pick --skip
|
||
else
|
||
git cherry-pick --continue || git cherry-pick --skip
|
||
fi
|
||
}
|
||
done
|
||
|
||
- name: Force push the new branch as main
|
||
run: |
|
||
git branch -M main # 将当前分支重命名为 main
|
||
git push --force origin main # 强制推送到远程仓库,覆盖原有的 main 分支
|
||
|
||
- name: Notify completion
|
||
run: echo "历史清理操作已完成,只保留最近三次提交。" |