fix(ci): add file locking to prevent rebase race conditions

This commit is contained in:
m3tm3re
2026-02-04 19:09:06 +01:00
parent 49156324ec
commit f1702eb994

View File

@@ -330,29 +330,70 @@ jobs:
fi fi
fi fi
echo "::group::Git Operations" LOCKFILE="/tmp/$USER/nix-update-git-lock"
echo "Current commit: $(git rev-parse HEAD)"
echo "Pending commits: $(git rev-list --count origin/master..HEAD)"
echo "" trap 'rm -f "$LOCKFILE"; exit' EXIT INT TERM
echo "Pulling latest changes (rebase)..."
if git pull --rebase origin master; then if [ -f "$LOCKFILE" ]; then
echo "✅ Rebase successful" echo "🔍 Found existing lock file, checking if stale..."
else STALE_LOCK_PID=$(cat "$LOCKFILE" 2>/dev/null || echo "")
echo "⚠️ Rebase failed, attempting force push..." if [ -n "$STALE_LOCK_PID" ] && ! kill -0 "$STALE_LOCK_PID" 2>/dev/null; then
git reset --hard origin/master echo "🗑️ Stale lock detected (PID $STALE_LOCK_PID no longer running), removing..."
git push --force-with-lease origin master rm -f "$LOCKFILE"
echo "✓ Force push completed" fi
exit 0
fi fi
echo "" (
echo "Pushing changes to master..." flock -x 200 || exit 1
git push origin master
echo "" echo $$ > "$LOCKFILE"
echo "✅ Successfully pushed updates for: $PACKAGES"
echo "::endgroup::" MY_PID=$$
echo "🔒 Lock acquired by PID $MY_PID"
echo "::group::Git Operations"
echo "Current commit: $(git rev-parse HEAD)"
echo "Pending commits: $(git rev-list --count origin/master..HEAD)"
git rebase --abort 2>/dev/null || true
if [ -n "$(git status --porcelain)" ]; then
echo ""
echo "⚠️ Unstaged changes detected, auto-committing..."
git add .
git commit -m "chore: auto-commit before rebase [skip ci]"
echo "✅ Unstaged changes committed"
fi
echo ""
echo "Pulling latest changes (autostash + rebase)..."
if git pull --autostash --rebase origin master; then
echo "✅ Rebase successful"
else
echo "⚠️ Rebase failed, attempting merge (-Xtheirs)..."
git rebase --abort 2>/dev/null || true
if git merge -Xtheirs origin/master; then
echo "✅ Merge successful (-Xtheirs strategy)"
else
echo "⚠️ Merge failed, attempting force push..."
git reset --hard origin/master
git push --force-with-lease origin master
echo "✅ Force push completed"
exit 0
fi
fi
echo ""
echo "Pushing changes to master..."
git push origin master
echo ""
echo "✅ Successfully pushed updates for: $PACKAGES"
echo "🔒 Lock released by PID $MY_PID"
echo "::endgroup::"
) 200>"$LOCKFILE"
- name: Upload Build Logs - name: Upload Build Logs
if: failure() if: failure()