mirror of
https://github.com/QYG2297248353/appstore-1panel.git
synced 2026-06-16 01:02:14 +08:00
Update auto_merge_renovate_prs.yml
This commit is contained in:
@@ -1,141 +1,65 @@
|
|||||||
name: Renovate Auto-merge (poll until CI passes)
|
name: Merge Renovate PRs
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
|
||||||
delay_minutes:
|
|
||||||
description: 'Delay in minutes before starting checks (default 5)'
|
|
||||||
required: false
|
|
||||||
default: '5'
|
|
||||||
max_wait_minutes:
|
|
||||||
description: 'Max minutes to wait for CI per PR (default 30)'
|
|
||||||
required: false
|
|
||||||
default: '30'
|
|
||||||
interval_seconds:
|
|
||||||
description: 'Polling interval seconds (default 60)'
|
|
||||||
required: false
|
|
||||||
default: '60'
|
|
||||||
schedule:
|
schedule:
|
||||||
- cron: "*/10 * * * *"
|
- cron: "0 * * * *"
|
||||||
pull_request:
|
|
||||||
types: [ready_for_review]
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
pull-requests: write
|
|
||||||
checks: read
|
|
||||||
|
|
||||||
env:
|
|
||||||
REPO: ${{ github.repository }}
|
|
||||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 👈 关键:给 gh CLI 用的变量
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
automerge:
|
merge-renovate-prs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Show gh version (debug)
|
- name: Authenticate GitHub CLI
|
||||||
run: gh --version
|
run: |
|
||||||
|
echo "${{ github.token }}" | gh auth login --with-token
|
||||||
- name: Optional initial delay (let CI start)
|
|
||||||
env:
|
env:
|
||||||
DELAY_MIN: ${{ github.event.inputs.delay_minutes }}
|
GH_TOKEN: ${{ github.token }}
|
||||||
|
|
||||||
|
- name: List Renovate PRs
|
||||||
|
id: list
|
||||||
run: |
|
run: |
|
||||||
delay="${DELAY_MIN:-5}"
|
prs=$(gh pr list --repo "$GITHUB_REPOSITORY" --state open \
|
||||||
echo "Delaying for ${delay} minute(s) to let other workflows start..."
|
--json number,author \
|
||||||
sleep $(( delay * 60 ))
|
--jq '.[] | select(.author.login | test("renovate"; "i")) | .number')
|
||||||
|
echo "Found PRs: $prs"
|
||||||
|
# 把 PR 列表保存成 JSON 数组,避免多行字符串解析出错
|
||||||
|
prs_json=$(printf '%s\n' "$prs" | jq -R . | jq -s .)
|
||||||
|
echo "prs=$prs_json" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Find open Renovate PR numbers into array
|
- name: Poll and merge
|
||||||
id: find_prs
|
if: steps.list.outputs.prs != '[]'
|
||||||
run: |
|
run: |
|
||||||
owner_repo="${{ env.REPO }}"
|
echo "PR list JSON: ${{ steps.list.outputs.prs }}"
|
||||||
echo "Listing open PRs for $owner_repo authored by renovate-like bots..."
|
prs=$(echo '${{ steps.list.outputs.prs }}' | jq -r '.[]')
|
||||||
gh pr list --repo "$owner_repo" --state open --json number,title,author \
|
for pr in $prs; do
|
||||||
--jq '.[] | select(.author.login | test("renovate"; "i")) | .number' > /tmp/renovate_prs.txt || true
|
|
||||||
echo "Saved PR list lines:"
|
|
||||||
sed -n '1,200p' /tmp/renovate_prs.txt || true
|
|
||||||
echo "PR_FILE=/tmp/renovate_prs.txt" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Process Renovate PRs (poll CI, merge when green)
|
|
||||||
if: steps.find_prs.outputs.PR_FILE != ''
|
|
||||||
env:
|
|
||||||
REPO: ${{ env.REPO }}
|
|
||||||
MAX_WAIT_MIN: ${{ github.event.inputs.max_wait_minutes }}
|
|
||||||
INTERVAL_SEC: ${{ github.event.inputs.interval_seconds }}
|
|
||||||
run: |
|
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
owner_repo="${REPO}"
|
|
||||||
owner="${owner_repo%%/*}"
|
|
||||||
repo="${owner_repo##*/}"
|
|
||||||
prfile="${{ steps.find_prs.outputs.PR_FILE }}"
|
|
||||||
|
|
||||||
mapfile -t prs < <(sed '/^\s*$/d' "$prfile" || true)
|
|
||||||
if [ "${#prs[@]}" -eq 0 ]; then
|
|
||||||
echo "No renovate PRs found. Exiting."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
echo "Found ${#prs[@]} renovate PR(s): ${prs[*]}"
|
|
||||||
|
|
||||||
max_wait_min="${MAX_WAIT_MIN:-30}"
|
|
||||||
interval_sec="${INTERVAL_SEC:-60}"
|
|
||||||
max_attempts=$(( (max_wait_min * 60 + interval_sec - 1) / interval_sec ))
|
|
||||||
echo "Polling up to $max_attempts times (interval ${interval_sec}s, max ${max_wait_min}min) per PR."
|
|
||||||
|
|
||||||
for pr in "${prs[@]}"; do
|
|
||||||
echo
|
|
||||||
echo "=== Processing PR #$pr ==="
|
echo "=== Processing PR #$pr ==="
|
||||||
|
|
||||||
body=$(gh pr view "$pr" --repo "$owner_repo" --json body --jq .body 2>/dev/null || echo "")
|
sha=$(gh pr view "$pr" --repo "$GITHUB_REPOSITORY" --json headRefOid -q .headRefOid)
|
||||||
update_type=$(echo "$body" | grep -m1 -Po '\|\s*[^|]+\s*\|\s*\K(major|minor|patch)(?=\s*\|)' || true)
|
|
||||||
update_type=${update_type:-unknown}
|
|
||||||
echo "Detected update_type='$update_type'"
|
|
||||||
if [[ "$update_type" != "patch" && "$update_type" != "minor" ]]; then
|
|
||||||
echo "Skipping PR #$pr (not patch/minor)."
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
sha=$(gh pr view "$pr" --repo "$owner_repo" --json headRefOid --jq .headRefOid 2>/dev/null || true)
|
|
||||||
if [[ -z "$sha" ]]; then
|
|
||||||
sha=$(gh api repos/"$owner"/"$repo"/pulls/"$pr" --jq .head.sha 2>/dev/null || true)
|
|
||||||
fi
|
|
||||||
echo "PR #$pr head SHA: $sha"
|
echo "PR #$pr head SHA: $sha"
|
||||||
|
|
||||||
merged=0
|
merged=false
|
||||||
attempt=0
|
for attempt in $(seq 1 180); do
|
||||||
while [ $attempt -lt $max_attempts ]; do
|
combined=$(gh api repos/$GITHUB_REPOSITORY/commits/$sha/status --jq .state)
|
||||||
attempt=$((attempt+1))
|
inprogress=$(gh api repos/$GITHUB_REPOSITORY/commits/$sha/check-runs --jq '[.check_runs[] | select(.status!="completed")] | length')
|
||||||
echo "Attempt $attempt/$max_attempts for PR #$pr ..."
|
|
||||||
|
|
||||||
combined_state=$(gh api repos/"$owner"/"$repo"/commits/"$sha"/status --jq .state 2>/dev/null || echo "unknown")
|
echo "Attempt $attempt: combined=$combined, inprogress=$inprogress"
|
||||||
inprogress_count=$(gh api repos/"$owner"/"$repo"/commits/"$sha"/check-runs \
|
|
||||||
--jq '.check_runs[] | select(.status == "in_progress" or .conclusion == null) | .name' 2>/dev/null | wc -l || echo 0)
|
|
||||||
inprogress_count=$(echo "$inprogress_count" | tr -d '[:space:]')
|
|
||||||
|
|
||||||
echo "Combined=$combined_state, In-progress=$inprogress_count"
|
# 判断条件:成功 或 无检查
|
||||||
|
if { [[ "$combined" == "success" ]] || [[ "$inprogress" -eq 0 && "$combined" == "pending" ]]; }; then
|
||||||
if [[ "$combined_state" == "success" && "$inprogress_count" -eq 0 ]]; then
|
echo "Checks passed or none required. Merging PR #$pr ..."
|
||||||
echo "All checks passed for PR #$pr. Attempting merge..."
|
gh pr merge "$pr" --repo "$GITHUB_REPOSITORY" --merge --delete-branch
|
||||||
if gh pr merge "$pr" --repo "$owner_repo" --squash --delete-branch --yes; then
|
merged=true
|
||||||
echo "✅ PR #$pr merged successfully."
|
|
||||||
merged=1
|
|
||||||
break
|
|
||||||
else
|
|
||||||
echo "⚠️ Merge failed for PR #$pr (branch protection or conflicts)."
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
elif [[ "$combined_state" == "failure" ]]; then
|
|
||||||
echo "❌ CI failed for PR #$pr. Skipping."
|
|
||||||
break
|
break
|
||||||
else
|
|
||||||
echo "Not all checks green. Waiting ${interval_sec}s..."
|
|
||||||
sleep "$interval_sec"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Not ready yet, waiting 10s..."
|
||||||
|
sleep 10
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ $merged -eq 0 ]; then
|
if [ "$merged" = false ]; then
|
||||||
echo "PR #$pr was not merged within time window."
|
echo "❌ Failed to merge PR #$pr within 30min timeout"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user