| 1 | #!/usr/bin/env bash |
| 2 | # Start de self-update en wacht tot GET /api/v1/admin/self-update |
| 3 | # status ok meldt voor CI_COMMIT_SHA. publish_aur heeft needs: [self_update], |
| 4 | # dus die job start pas hierna. Een mislukking of timeout faalt deze job. |
| 5 | # SELF_UPDATE_TIMEOUT is seconden (standaard 300). SELF_UPDATE_INTERVAL |
| 6 | # is de pauze tussen pogingen (standaard 5). |
| 7 | set -euo pipefail |
| 8 | |
| 9 | : "${CI_API_URL:?CI_API_URL ontbreekt}" |
| 10 | : "${BLUEGIT_UPDATE_TOKEN:?BLUEGIT_UPDATE_TOKEN ontbreekt}" |
| 11 | : "${CI_COMMIT_SHA:?CI_COMMIT_SHA ontbreekt}" |
| 12 | |
| 13 | timeout="${SELF_UPDATE_TIMEOUT:-300}" |
| 14 | interval="${SELF_UPDATE_INTERVAL:-5}" |
| 15 | [[ "$timeout" =~ ^[0-9]+$ ]] || timeout=300 |
| 16 | [[ "$interval" =~ ^[0-9]+$ ]] || interval=5 |
| 17 | |
| 18 | if ! command -v jq >/dev/null 2>&1; then |
| 19 | apk add --no-cache jq >&2 |
| 20 | fi |
| 21 | |
| 22 | api="${CI_API_URL%/}/admin/self-update" |
| 23 | auth=(-H "Authorization: Bearer ${BLUEGIT_UPDATE_TOKEN}") |
| 24 | |
| 25 | curl -fsS --connect-timeout 5 --max-time 20 -X POST "$api" "${auth[@]}" \ |
| 26 | -H "Content-Type: application/json" \ |
| 27 | -d "{\"ref\":\"${CI_COMMIT_SHA}\"}" |
| 28 | |
| 29 | start="$(date +%s)" |
| 30 | while true; do |
| 31 | # Een herstart van de service tijdens pacman -U mag de poll niet stoppen. |
| 32 | # log staat live als eerste key en bevat de tekst "status" en "sha". |
| 33 | body="$(curl -fsS --connect-timeout 5 --max-time 20 "$api" "${auth[@]}" || true)" |
| 34 | status="" |
| 35 | sha="" |
| 36 | if parsed="$(printf '%s' "$body" | jq -er '[.status // "", .sha // ""] | @tsv' 2>/dev/null)"; then |
| 37 | status="${parsed%%$'\t'*}" |
| 38 | sha="${parsed#*$'\t'}" |
| 39 | fi |
| 40 | if [[ "$sha" == "$CI_COMMIT_SHA" && "$status" == "ok" ]]; then |
| 41 | echo "Self-update ok: $sha" |
| 42 | exit 0 |
| 43 | fi |
| 44 | if [[ "$sha" == "$CI_COMMIT_SHA" && "$status" == "failed" ]]; then |
| 45 | echo "Self-update mislukt: $sha" >&2 |
| 46 | exit 1 |
| 47 | fi |
| 48 | now="$(date +%s)" |
| 49 | if (( now - start >= timeout )); then |
| 50 | echo "Self-update timeout na ${timeout}s (status ${status:-onbekend}, sha ${sha:-onbekend})" >&2 |
| 51 | exit 1 |
| 52 | fi |
| 53 | echo "Self-update status ${status:-onbekend} voor ${sha:-onbekend}" |
| 54 | sleep "$interval" |
| 55 | done |