#!/usr/bin/env bash # Bump the FROM line in TARGET_REPO and land it on main. Where branch # protection refuses a direct push, propose the same commit as a pull request. # Inputs arrive as environment variables; see action.yaml. set -euo pipefail TAG="${TAG_INPUT:-$RUN_NUMBER}" NAME="$(basename "$IMAGE")" MSG="${MESSAGE:-bump ${NAME} to #${TAG}}" BRANCH="cascade/${NAME}-${TAG}" WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT git clone --depth 1 "https://ci-bot:${TOKEN}@${HOST}/${TARGET_REPO}.git" "$WORK" cd "$WORK" git config user.name ci-bot git config user.email ci-bot@fritzlab.net if [ ! -f "$FILE" ]; then echo "FATAL: ${FILE} not found in ${TARGET_REPO}" exit 1 fi sed -i "s|^FROM ${IMAGE}:.*|FROM ${IMAGE}:${TAG}|" "$FILE" if ! grep -q "^FROM ${IMAGE}:${TAG}\b" "$FILE"; then echo "FATAL: sed did not match a FROM line for ${IMAGE} in ${FILE}" grep "^FROM" "$FILE" || true exit 1 fi if git diff --quiet; then echo "${TARGET_REPO} already on ${NAME}:${TAG}, skipping" exit 0 fi git add "$FILE" git commit -m "$MSG" # Protected main: push the commit to its own branch and open a pull request, # so the bump waits for review instead of failing the upstream release. propose() { if git ls-remote --exit-code origin "refs/heads/${BRANCH}" >/dev/null; then echo "${BRANCH} already exists in ${TARGET_REPO}" else git push origin "HEAD:refs/heads/${BRANCH}" fi local body status body="$(jq -n --arg title "$MSG" --arg head "$BRANCH" \ --arg body "Cascade from ${IMAGE}:${TAG}. ${TARGET_REPO} protects main, so this bump needs review." \ '{title: $title, head: $head, base: "main", body: $body}')" status="$(curl -sS -o "$WORK/pull.json" -w '%{http_code}' -X POST \ -H "Authorization: token ${TOKEN}" -H 'Content-Type: application/json' \ --data "$body" "https://${HOST}/api/v1/repos/${TARGET_REPO}/pulls")" case "$status" in 201) echo "proposed ${NAME}:${TAG} → $(jq -r '.html_url' "$WORK/pull.json")" ;; 409) echo "a pull request for ${BRANCH} is already open in ${TARGET_REPO}" ;; *) echo "FATAL: opening a pull request in ${TARGET_REPO} answered HTTP ${status}" cat "$WORK/pull.json" || true exit 1 ;; esac } ATTEMPTS=0 until git push origin main 2>"$WORK/push.err"; do cat "$WORK/push.err" >&2 if grep -q 'protected branch' "$WORK/push.err"; then propose exit 0 fi ATTEMPTS=$((ATTEMPTS + 1)) if [ "$ATTEMPTS" -ge 3 ]; then echo "FATAL: push to ${TARGET_REPO} failed after ${ATTEMPTS} attempts" exit 1 fi echo "push rejected, attempt ${ATTEMPTS}; rebasing and retrying" git pull --rebase origin main sleep $((ATTEMPTS * 2)) done echo "cascaded ${NAME}:${TAG} → ${TARGET_REPO}"