Files
computer-playbook/scripts/meta/resolve/push_ci_policy.sh
kevinveenbirkenbachandClaude Opus 4.7 a561aa76b5 ci(push): skip push CI when an open PR already covers the branch
push_ci_policy.sh now hard-fails on missing GH_TOKEN/GITHUB_REPOSITORY
and, for non-main branches, calls gh pr list to detect whether the
push is already covered by an open pull request. If so, emits
should_run=false (skip_reason=pr-ci-covers-this-push) so the push
workflow exits cleanly and the pull_request workflow handles the
actual CI run.

To absorb the bot's push -> gh-pr-create latency on update/* and
alert-autofix-* branches, the PR check retries up to 10x with 2s
sleeps before falling through to a real CI run. Human branches
(feature/**, hotfix/**, fix/**, chore/**) check once with no delay.

entry-push-latest.yml's run-policy step now forwards GH_TOKEN so the
gh call works against the repo.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-28 21:06:34 +02:00

75 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
: "${GITHUB_EVENT_CREATED:?Missing GITHUB_EVENT_CREATED}"
: "${GITHUB_OUTPUT:?Missing GITHUB_OUTPUT}"
: "${GITHUB_SHA:?Missing GITHUB_SHA}"
: "${GITHUB_REF:?Missing GITHUB_REF}"
: "${GH_TOKEN:?Missing GH_TOKEN}"
: "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}"
if [[ "${GITHUB_REF}" == "refs/heads/main" && "${CI_RUN_ON_MAIN:-}" != "true" ]]; then
{
echo "should_run=false"
echo "skip_reason=main-push-gated-by-ci-run-on-main"
echo "commit_in_main=true"
} >>"${GITHUB_OUTPUT}"
echo "Push to main detected and repository variable CI_RUN_ON_MAIN is not 'true' -> CI will be skipped."
exit 0
fi
if [[ "${GITHUB_REF}" != "refs/heads/main" ]]; then
branch_name="${GITHUB_REF#refs/heads/}"
case "${branch_name}" in
update/* | alert-autofix-*) max_attempts=10 ;;
*) max_attempts=1 ;;
esac
attempt=0
pr_count=0
while ((attempt < max_attempts)); do
pr_count="$(gh pr list --repo "${GITHUB_REPOSITORY}" --head "${branch_name}" --state open --json number --jq 'length')"
if ((pr_count > 0)); then
break
fi
attempt=$((attempt + 1))
if ((attempt < max_attempts)); then
sleep 2
fi
done
if ((pr_count > 0)); then
{
echo "should_run=false"
echo "skip_reason=pr-ci-covers-this-push"
echo "commit_in_main=false"
} >>"${GITHUB_OUTPUT}"
echo "Open PR exists for branch '${branch_name}' -> CI will be skipped; pull_request workflow handles this push."
exit 0
fi
fi
if [[ "${GITHUB_EVENT_CREATED}" != "true" ]]; then
{
echo "should_run=true"
echo "skip_reason=not-a-branch-creation"
echo "commit_in_main=false"
} >>"${GITHUB_OUTPUT}"
echo "Regular push detected -> CI will run."
exit 0
fi
if git merge-base --is-ancestor "${GITHUB_SHA}" "origin/main"; then
{
echo "should_run=false"
echo "skip_reason=branch-created-from-commit-already-in-main"
echo "commit_in_main=true"
} >>"${GITHUB_OUTPUT}"
echo "Branch creation detected and commit ${GITHUB_SHA} is already contained in origin/main -> CI will be skipped."
else
{
echo "should_run=true"
echo "skip_reason=branch-created-from-commit-not-in-main"
echo "commit_in_main=false"
} >>"${GITHUB_OUTPUT}"
echo "Branch creation detected and commit ${GITHUB_SHA} is not contained in origin/main -> CI will run."
fi