#!/usr/bin/env bash set -euo pipefail # Query GitHub Actions for an exact SHA via gh. Output STATUS= keys. sha= while (($#)); do case "$1" in --sha) sha=$2; shift 2 ;; *) echo "Usage: ci-status.sh --sha SHA" >&2; exit 2 ;; esac done [[ -n "$sha" ]] || { echo "Usage: ci-status.sh --sha SHA" >&2; exit 2; } if ! command -v gh >/dev/null 2>&1; then echo "Error: gh is required" >&2 exit 3 fi root=${BUILDER_PROJECT_ROOT:-$(git rev-parse --show-toplevel)} origin=$(git -C "$root" remote get-url origin 2>/dev/null || true) [[ -n "$origin" ]] || { echo "Error: no origin remote" >&2; exit 3; } repo=$(WAIT_CI_ORIGIN=$origin python3 -c ' import os, re url = os.environ["WAIT_CI_ORIGIN"].strip() url = re.sub(r"^git@([^:]+):", r"ssh://git@\1/", url) url = re.sub(r"^ssh://git@([^/]+):", r"ssh://git@\1/", url) if re.match(r"^https?://", url): m = re.match(r"^https?://[^/]+(?:/(.*))?$", url) path = m.group(1) if m else "" else: m = re.match(r"^ssh://git@[^/]+/(.+)$", url) path = m.group(1) if m else "" print(re.sub(r"\.git$", "", path or "")) ') set +e raw=$(gh run list --repo "$repo" --commit "$sha" --limit 20 --json databaseId,status,conclusion,url,headSha) rc=$? set -e if [[ $rc -ne 0 ]]; then echo "Error: gh cannot list runs for $repo" >&2 exit 3 fi python3 -c ' import json, sys try: data = json.loads(sys.stdin.read()) except json.JSONDecodeError: print("STATUS=error") raise SystemExit(0) if not data: print("STATUS=empty") raise SystemExit(0) r = data[0] status = str(r.get("status") or "").lower() conc = str(r.get("conclusion") or "").lower() if status == "completed" and conc == "success": state = "success" elif status == "completed": state = "failed" else: state = "running" print("STATUS=%s ID=%s PIPELINE=%s" % (state, r.get("databaseId") or "-", r.get("url") or "-")) ' <<<"$raw"