|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Commit a single auto-generated history file and push it to the current branch. |
| 4 | +# |
| 5 | +# Multiple CI jobs (the webui bundle-size job and the JS runtime perf job) run in |
| 6 | +# parallel on every push to `main` and each commit a different history file back |
| 7 | +# to the branch. They all check out the same triggering SHA, so the first one to |
| 8 | +# push advances `origin/main` and the others become non-fast-forward and get |
| 9 | +# rejected. Because every job touches a distinct, append-only history file, we can |
| 10 | +# safely fetch + rebase our single commit onto the latest branch tip and retry the |
| 11 | +# push until it lands. |
| 12 | +# |
| 13 | +# Usage: commit-ci-history.sh <file> <commit-message> |
| 14 | + |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +if [[ $# -ne 2 ]]; then |
| 18 | + echo "Usage: $0 <file> <commit-message>" >&2 |
| 19 | + exit 2 |
| 20 | +fi |
| 21 | + |
| 22 | +FILE="$1" |
| 23 | +MESSAGE="$2" |
| 24 | +BRANCH="${GITHUB_REF_NAME:-$(git rev-parse --abbrev-ref HEAD)}" |
| 25 | +MAX_ATTEMPTS="${PUSH_MAX_ATTEMPTS:-5}" |
| 26 | + |
| 27 | +git config user.name "github-actions[bot]" |
| 28 | +git config user.email "github-actions[bot]@users.noreply.github.com" |
| 29 | + |
| 30 | +git add "$FILE" |
| 31 | +if git diff --cached --quiet; then |
| 32 | + echo "No changes to ${FILE}; nothing to commit." |
| 33 | + exit 0 |
| 34 | +fi |
| 35 | + |
| 36 | +git commit -m "$MESSAGE" |
| 37 | + |
| 38 | +for attempt in $(seq 1 "$MAX_ATTEMPTS"); do |
| 39 | + if git push origin "HEAD:${BRANCH}"; then |
| 40 | + echo "Pushed ${FILE} on attempt ${attempt}/${MAX_ATTEMPTS}." |
| 41 | + exit 0 |
| 42 | + fi |
| 43 | + |
| 44 | + echo "Push rejected (attempt ${attempt}/${MAX_ATTEMPTS}); rebasing onto origin/${BRANCH} and retrying..." |
| 45 | + git fetch origin "$BRANCH" |
| 46 | + # The racing job only ever touches a different history file, so replaying our |
| 47 | + # single commit onto the new tip never conflicts. |
| 48 | + git rebase "origin/${BRANCH}" |
| 49 | +done |
| 50 | + |
| 51 | +echo "Failed to push ${FILE} after ${MAX_ATTEMPTS} attempts." >&2 |
| 52 | +exit 1 |
0 commit comments