Files
e-tib.com/scripts/priority-gate.sh
Marc Mintel b64b2ccf25
All checks were successful
Build & Deploy / 🔍 Prepare (push) Successful in 1m33s
Build & Deploy / 🧪 QA (push) Successful in 1m40s
Build & Deploy / 🏗️ Build (push) Successful in 3m19s
Build & Deploy / 🚀 Deploy (push) Successful in 33s
Build & Deploy / 🧪 Post-Deploy Verification (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 3s
fix: pipeline execution, deployment priority, and bundle optimizations
2026-06-25 22:08:26 +02:00

92 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
set -e
if [ -z "$GITEA_PAT" ]; then
echo "No GITEA_PAT provided, skipping priority gate."
exit 0
fi
if [ -z "$CURRENT_RUN_ID" ] || [ -z "$CURRENT_TARGET" ] || [ -z "$GITHUB_REPOSITORY" ]; then
echo "Missing required environment variables. CURRENT_RUN_ID=$CURRENT_RUN_ID, CURRENT_TARGET=$CURRENT_TARGET, GITHUB_REPOSITORY=$GITHUB_REPOSITORY"
# Don't fail the build just because target is empty (e.g. skip targets) or env vars are missing during PRs
echo "Bypassing priority gate gracefully."
exit 0
fi
declare -A PRIO=( ["production"]=40 ["staging"]=30 ["testing"]=20 ["branch"]=10 )
CURRENT_PRIO=${PRIO[$CURRENT_TARGET]:-0}
echo "Current Run: $CURRENT_RUN_ID, Target: $CURRENT_TARGET, Priority: $CURRENT_PRIO"
API_BASE="https://git.infra.mintel.me/api/v1/repos/$GITHUB_REPOSITORY/actions/runs"
# Fetch running and waiting workflows
LIMIT=50
RESPONSE=$(curl -s -H "Authorization: token $GITEA_PAT" "$API_BASE?limit=$LIMIT")
# Verify response is a valid JSON array
if ! echo "$RESPONSE" | jq -e 'type == "array"' >/dev/null 2>&1; then
echo "Failed to parse API response from Gitea (not an array) or API error."
echo "$RESPONSE"
echo "Bypassing priority gate gracefully."
exit 0
fi
# Use jq to extract running or waiting jobs that are NOT the current one
RUNS=$(echo "$RESPONSE" | jq -c '.[] | select(.id != '"$CURRENT_RUN_ID"') | select(.status == "running" or .status == "waiting")')
if [ -z "$RUNS" ]; then
echo "No other running/waiting workflows found."
exit 0
fi
while IFS= read -r run; do
if [ -z "$run" ]; then continue; fi
RUN_ID=$(echo "$run" | jq -r '.id')
REF=$(echo "$run" | jq -r '.ref')
REF_NAME=${REF#refs/tags/}
REF_NAME=${REF_NAME#refs/heads/}
# Determine target of the running workflow
TARGET="branch"
if [ "$REF" == "refs/heads/main" ] || [ "$REF_NAME" == "main" ]; then
TARGET="testing"
elif [[ "$REF" == refs/tags/* ]] || [[ "$REF" == refs/heads/v* ]]; then
if [[ "$REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
TARGET="production"
else
TARGET="staging"
fi
fi
RUN_PRIO=${PRIO[$TARGET]:-0}
echo "Found running workflow ID: $RUN_ID (Target: $TARGET, Priority: $RUN_PRIO)"
if [ "$CURRENT_PRIO" -gt "$RUN_PRIO" ]; then
echo "Current workflow has HIGHER priority ($CURRENT_PRIO > $RUN_PRIO). Canceling run $RUN_ID..."
curl -s -X POST -H "Authorization: token $GITEA_PAT" "$API_BASE/$RUN_ID/cancel"
elif [ "$CURRENT_PRIO" -lt "$RUN_PRIO" ]; then
echo "Current workflow has LOWER priority ($CURRENT_PRIO < $RUN_PRIO). Aborting self."
curl -s -X POST -H "Authorization: token $GITEA_PAT" "$API_BASE/$CURRENT_RUN_ID/cancel"
echo "Waiting for Gitea to process cancellation..."
sleep 15
exit 0
else
# Equal priority: Newer wins
if [ "$CURRENT_RUN_ID" -gt "$RUN_ID" ]; then
echo "Equal priority, but current is newer. Canceling run $RUN_ID..."
curl -s -X POST -H "Authorization: token $GITEA_PAT" "$API_BASE/$RUN_ID/cancel"
else
echo "Equal priority, but current is older. Aborting self."
curl -s -X POST -H "Authorization: token $GITEA_PAT" "$API_BASE/$CURRENT_RUN_ID/cancel"
echo "Waiting for Gitea to process cancellation..."
sleep 15
exit 0
fi
fi
done <<< "$RUNS"
echo "Priority gate cleared. Continuing deployment."