Clean Old Actions Runs #8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Clean Old Actions Runs | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 4 * * *' # 每天凌晨 4 点运行 | |
jobs: | |
cleanup: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Delete old workflow runs (keep last 7) | |
env: | |
GH_TOKEN: ${{ secrets.ACTIONS_TRIGGER_PAT }} | |
run: | | |
echo "🧼 清理旧的 GitHub Actions 运行记录(保留 7 个)" | |
runs=$(gh api -X GET /repos/${{ github.repository }}/actions/runs --paginate -q '.workflow_runs | sort_by(.created_at) | .[].id') | |
count=0 | |
for run_id in $runs; do | |
count=$((count + 1)) | |
if [ $count -le 7 ]; then | |
continue | |
fi | |
echo "🗑️ 删除运行记录 ID: $run_id" | |
gh api -X DELETE /repos/${{ github.repository }}/actions/runs/$run_id || echo "⚠️ 删除失败:$run_id" | |
done |