Update auto_merge_renovate_prs.yml

This commit is contained in:
Meng Sen
2025-09-17 13:03:28 +08:00
committed by GitHub
parent f2cc2d78f5
commit 9d9488faee

View File

@@ -1,79 +1,188 @@
name: Auto merge Renovate PRs
name: Renovate Auto-merge (poll until CI passes)
on:
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:
- cron: "*/10 * * * *" # 每 10 分钟跑一次
workflow_dispatch: # 支持手动触发
- cron: "*/10 * * * *" # every 10 minutes
pull_request:
types: [ready_for_review]
permissions:
contents: read
pull-requests: write
checks: read
env:
REPO: ${{ github.repository }}
jobs:
automerge:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@v4
- name: List open PRs from Renovate
id: list
- name: Show gh version (debug)
run: |
prs=$(gh pr list \
--repo ${{ github.repository }} \
--state open \
--json number,title,author \
--jq '.[] | select(.author.login | test("renovate";"i")) | .number')
echo "Found PRs: $prs"
echo "prs=$prs" >> $GITHUB_ENV
echo "gh version:"
gh --version || true
- name: Authenticate gh with GITHUB_TOKEN
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Process each PR
if: env.prs != ''
run: |
for pr in $prs; do
if gh auth status 2>/dev/null; then
echo "gh already authenticated"
else
echo "${GITHUB_TOKEN}" | gh auth login --with-token
fi
gh auth status || true
- name: Optional initial delay (let CI start)
env:
DELAY_MIN: ${{ github.event.inputs.delay_minutes }}
run: |
delay="${DELAY_MIN:-5}"
echo "Delaying for ${delay} minute(s) to let other workflows start..."
sleep $(( delay * 60 ))
- name: Find open Renovate PR numbers into array
id: find_prs
run: |
owner_repo="${{ env.REPO }}"
echo "Listing open PRs for $owner_repo authored by renovate-like bots..."
# produce newline separated list, then mapfile into array in next step
gh pr list --repo "$owner_repo" --state open --json number,title,author \
--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 }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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 }}"
# Read PR numbers into array (skip empty lines)
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[*]}"
# compute attempts
max_wait_min="${MAX_WAIT_MIN:-30}"
interval_sec="${INTERVAL_SEC:-60}"
if ! [[ "$max_wait_min" =~ ^[0-9]+$ ]]; then max_wait_min=30; fi
if ! [[ "$interval_sec" =~ ^[0-9]+$ ]]; then interval_sec=60; fi
max_attempts=$(( (max_wait_min * 60 + interval_sec - 1) / interval_sec ))
echo "Will poll up to $max_attempts times (interval ${interval_sec}s), max wait ${max_wait_min} minutes per PR."
for pr in "${prs[@]}"; do
echo
echo "=== Processing PR #$pr ==="
body=$(gh pr view $pr --repo ${{ github.repository }} --json body --jq .body)
update_type=$(echo "$body" | grep -Eo '\|\s+[a-zA-Z0-9/_-]+\s+\|\s+(major|minor|patch)\s+\|' | head -n1 | awk -F'|' '{print $3}' | xargs)
# PR body
body=$(gh pr view "$pr" --repo "$owner_repo" --json body --jq .body 2>/dev/null || echo "")
echo "PR #$pr body (first 20 lines):"
echo "$body" | sed -n '1,20p' || true
# Detect update type from table (Update column)
# Looks for e.g. "| package/name | minor | vX -> vY |"
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)"
echo "Skipping PR #$pr because it's not patch/minor (detected=$update_type)."
continue
fi
sha=$(gh pr view $pr --repo ${{ github.repository }} --json headRefOid --jq .headRefOid)
echo "PR #$pr head SHA: $sha"
# head sha
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:-<unknown>}"
if [[ -z "$sha" ]]; then
echo "Cannot determine head SHA for PR #$pr; skipping."
continue
fi
# 最多等 30 分钟30 次 * 60 秒)
for i in {1..30}; do
echo "🔎 Check attempt $i for PR #$pr ..."
merged=0
attempt=0
while [ $attempt -lt $max_attempts ]; do
attempt=$((attempt+1))
echo "Attempt $attempt/$max_attempts for PR #$pr ..."
# 获取 combined 状态
state=$(gh api repos/${{ github.repository }}/commits/$sha/status --jq .state)
echo "Combined status = $state"
# Combined status (legacy) - summary: success / pending / failure / unknown
combined_state=$(gh api repos/"$owner"/"$repo"/commits/"$sha"/status --jq .state 2>/dev/null || echo "unknown")
echo "Combined status for $sha: $combined_state"
# 获取详细 check-runs
echo "---- Check runs for $sha ----"
gh api repos/${{ github.repository }}/commits/$sha/check-runs \
--jq '.check_runs[] | {name: .name, status: .status, conclusion: .conclusion}' || true
echo "--------------------------------"
# Show detailed check-runs
echo "Check runs for $sha:"
gh api repos/"$owner"/"$repo"/commits/"$sha"/check-runs --jq '.check_runs[] | {name: .name, status: .status, conclusion: .conclusion}' 2>/dev/null || echo "(no check-runs or API returned none)"
if [[ "$state" == "success" ]]; then
echo "✅ All checks passed for PR #$pr. Merging..."
gh pr merge $pr \
--squash \
--delete-branch \
--repo ${{ github.repository }}
break
elif [[ "$state" == "failure" ]]; then
echo "❌ Checks failed for PR #$pr. Skipping."
# Count in-progress/pending check-runs (status == in_progress OR conclusion == null)
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:]')
inprogress_count=${inprogress_count:-0}
echo "In-progress/pending check-runs: $inprogress_count"
# Decide
if [[ "$combined_state" == "success" && "$inprogress_count" -eq 0 ]]; then
echo "All checks passed for PR #$pr. Attempting merge now..."
if gh pr merge "$pr" --repo "$owner_repo" --squash --delete-branch --yes; then
echo "✅ PR #$pr merged successfully."
merged=1
break
else
echo "⚠️ gh pr merge failed for PR #$pr (might be branch protection requiring review or merge conflict). Will not retry."
break
fi
elif [[ "$combined_state" == "failure" ]]; then
echo "Checks failed for PR #$pr (combined_state=failure). Skipping merge."
break
else
echo "⏳ Still pending checks for PR #$pr. Waiting 60s..."
sleep 60
echo "Checks not yet all green (state=$combined_state, inprogress=$inprogress_count). Waiting ${interval_sec}s before retry..."
sleep "$interval_sec"
fi
done
if [ $merged -eq 0 ]; then
echo "Did not merge PR #$pr in allowed time window."
# optional: leave a comment for visibility
# gh pr comment "$pr" --repo "$owner_repo" --body "Auto-merge attempt: CI didn't become green within ${max_wait_min} minutes. Please check."
fi
# small sleep to avoid rate limits
sleep 1
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
echo "Processing complete."