testirovanie-robot/.gitea/peppa-check.sh
Peppa 7942bc96ed
All checks were successful
ci / checks (push) Successful in 3s
deploy / deploy (push) Successful in 2s
Bootstrap тестирование робот from Peppa template
2026-11-16 09:05:05 +05:00

48 lines
2.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Шаг CI Пеппы: bash .gitea/peppa-check.sh <проверка>
# Команда — .peppa/project.json → checks.<проверка>; нет команды — шаг пропущен.
# Результат: статус коммита «peppa/<проверка>» в Gitea и отчёт в Пеппу с хвостом лога
# (подпись HMAC-SHA256 секретом PEPPA_CI_SECRET). Шаг «install» сообщает только о провале.
# Локально (без CI): bash .gitea/peppa-check.sh test — просто выполнит команду.
set -uo pipefail
check="$1"
cfg=.peppa/project.json
cmd=$(jq -r --arg c "$check" '.checks[$c] // empty' "$cfg" 2>/dev/null)
if [[ -z "$cmd" ]]; then
echo "peppa: проверка «$check» не настроена — пропуск"
exit 0
fi
log=$(mktemp)
started=$(date +%s)
bash -o pipefail -c "$cmd" 2>&1 | tee "$log"
code=${PIPESTATUS[0]}
took=$(($(date +%s) - started))
[[ -n "${GITHUB_ACTIONS:-}" ]] || exit "$code"
state=success
[[ $code -eq 0 ]] || state=failure
[[ $check == install && $state == success ]] && exit 0
sha="${PEPPA_SHA:-$GITHUB_SHA}"
# Относительная ссылка, как у статусов самой Gitea (GITHUB_SERVER_URL здесь внутренний).
run_url="/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_NUMBER"
if [[ -n "${GITEA_TOKEN:-}" ]]; then
jq -n --arg s "$state" --arg c "peppa/$check" --arg u "$run_url" --arg d "$check: $state за ${took}с" \
'{state:$s, context:$c, target_url:$u, description:$d}' |
curl -fsS -o /dev/null -X POST -H "Authorization: token $GITEA_TOKEN" -H "Content-Type: application/json" \
--data-binary @- "$GITHUB_SERVER_URL/api/v1/repos/$GITHUB_REPOSITORY/statuses/$sha" ||
echo "peppa: не удалось записать статус коммита"
fi
peppa_url=$(jq -r '.peppaUrl // empty' "$cfg")
if [[ -n "$peppa_url" && -n "${PEPPA_CI_SECRET:-}" ]]; then
body=$(tail -n 200 "$log" | jq -Rs --arg repo "$GITHUB_REPOSITORY" --arg sha "$sha" --arg branch "${PEPPA_BRANCH:-}" \
--arg pr "${PEPPA_PR:-}" --arg check "$check" --arg state "$state" --arg url "$run_url" --argjson took "$took" \
'{repository:$repo, sha:$sha, branch:$branch, pr:($pr|tonumber? // null), check:$check, state:$state, run_url:$url, duration_sec:$took, log_tail:.}')
sig=$(printf '%s' "$body" | openssl dgst -sha256 -hmac "$PEPPA_CI_SECRET" | awk '{print $NF}')
curl -fsS -o /dev/null -m 20 -X POST "$peppa_url/api/hooks/gitea" -H "Content-Type: application/json" \
-H "X-Peppa-Event: check" -H "X-Gitea-Signature: $sig" --data-binary "$body" ||
echo "peppa: Пеппа недоступна — статус заберёт опрос"
fi
exit "$code"