Files
EventHubFrontAdmin/scripts/publish-ci-report.sh
T
aleksey 0219ebace1 fix(ci-reports): chmod a+r on report.tar.gz for nginx read
mktemp leaves 600; nginx worker got 403 Forbidden on download.

[skip ci]
2026-07-28 19:42:21 +03:00

167 lines
5.0 KiB
Bash

#!/usr/bin/env bash
# Publish HTML CI report tree to local runner host and print HTTPS URL.
# Usage: publish-ci-report.sh <repo> <run_id> <kind> <src_dir>
# repo: EventHubBack | EventHubFront | …
# run_id: usually GITHUB_RUN_NUMBER or sha
# kind: eunit | ct | allure | e2e-ift | e2e-stage | …
# src_dir: directory with HTML to copy
set -euo pipefail
REPO="${1:?repo}"
RUN_ID="${2:?run_id}"
KIND="${3:?kind}"
SRC="${4:?src_dir}"
REPORTS_DIR="${CI_REPORTS_DIR:-/var/eventhub/ci-reports}"
STAND="${RUNNER_STAND:-}"
detect_stand() {
if [[ -n "${RUNNER_STAND:-}" ]]; then
echo "${RUNNER_STAND}"
return 0
fi
if [[ -f /opt/eventhub-ift/.env ]] || [[ -d /opt/eventhub-ift ]]; then
echo "ift"
return 0
fi
if [[ -f /opt/eventhub-stage/.env ]] || [[ -d /opt/eventhub-stage ]]; then
echo "stage"
return 0
fi
if command -v docker >/dev/null 2>&1 \
&& docker service ls --format '{{.Name}}' 2>/dev/null | grep -qx 'eventhub-ift-core_ci-reports'; then
echo "ift"
return 0
fi
if command -v docker >/dev/null 2>&1 \
&& docker service ls --format '{{.Name}}' 2>/dev/null | grep -qx 'eventhub_ci-reports'; then
echo "dev"
return 0
fi
echo "dev"
}
if [[ -n "${CI_REPORTS_BASE_URL:-}" ]]; then
BASE_URL="${CI_REPORTS_BASE_URL%/}"
STAND="${RUNNER_STAND:-custom}"
else
STAND="$(detect_stand)"
BASE_URL="https://ci-reports.${STAND}.eventhub.local"
fi
if [[ ! -d "${SRC}" ]]; then
echo "publish-ci-report: src missing: ${SRC}" >&2
exit 1
fi
# act runner: workflow may create REPORTS_DIR via sudo (root-owned); ensure CI user can write.
ensure_ci_reports_dir() {
if [[ -d "${REPORTS_DIR}" ]] && [[ -w "${REPORTS_DIR}" ]]; then
return 0
fi
if mkdir -p "${REPORTS_DIR}" 2>/dev/null && [[ -w "${REPORTS_DIR}" ]]; then
return 0
fi
if command -v sudo >/dev/null 2>&1; then
sudo mkdir -p "${REPORTS_DIR}"
sudo chown -R "$(id -u):$(id -g)" "${REPORTS_DIR}"
return 0
fi
mkdir -p "${REPORTS_DIR}"
}
ensure_ci_reports_dir
ensure_ci_reports_nginx() {
command -v docker >/dev/null 2>&1 || return 0
local svc replicas
for svc in eventhub-ift-core_ci-reports eventhub_ci-reports; do
docker service ls --format '{{.Name}}' 2>/dev/null | grep -qx "${svc}" || continue
replicas=$(docker service ls --format '{{.Name}} {{.Replicas}}' 2>/dev/null | awk -v s="${svc}" '$1==s{print $2; exit}')
if [[ "${replicas}" == 0/* ]] || [[ -z "${replicas}" ]]; then
echo "Starting ${svc}=1 (ci-reports nginx)..."
docker service scale "${svc}=1" 2>/dev/null || true
fi
return 0
done
}
ensure_ci_reports_nginx
DEST="${REPORTS_DIR}/${REPO}/${RUN_ID}/${KIND}"
mkdir -p "${REPORTS_DIR}/${REPO}/${RUN_ID}"
rm -rf "${DEST}"
mkdir -p "${DEST}"
cp -a "${SRC}/." "${DEST}/"
create_report_archive() {
local dest="$1"
local archive="${dest}/report.tar.gz"
local tmp
tmp="$(mktemp)"
tar -czf "${tmp}" -C "${dest}" .
mv -f "${tmp}" "${archive}"
chmod a+r "${archive}"
}
create_report_archive "${DEST}"
# Convenience: CT puts index under ct_run.*; expose DEST/index.html
if [[ ! -f "${DEST}/index.html" ]]; then
CT_INDEX="$(find "${DEST}" -type f -name index.html 2>/dev/null | head -n 1 || true)"
if [[ -n "${CT_INDEX}" ]]; then
REL="${CT_INDEX#"${DEST}/"}"
cat > "${DEST}/index.html" <<EOF
<!DOCTYPE html>
<html lang="ru"><head>
<meta charset="utf-8"/>
<meta http-equiv="refresh" content="0; url=${REL}"/>
<title>CT report</title>
</head><body>
<p><a href="${REL}">Open Common Test report</a></p>
</body></html>
EOF
fi
fi
# Refresh run index
INDEX="${REPORTS_DIR}/${REPO}/${RUN_ID}/index.html"
{
echo "<!DOCTYPE html>"
echo "<html lang=\"ru\"><head><meta charset=\"utf-8\"/><title>${REPO} #${RUN_ID}</title>"
echo "<style>body{font-family:system-ui,sans-serif;margin:2rem}a{display:block;margin:.4rem 0}</style>"
echo "</head><body>"
echo "<h1>${REPO} — run ${RUN_ID}</h1>"
echo "<ul>"
for d in "${REPORTS_DIR}/${REPO}/${RUN_ID}"/*/; do
[[ -d "${d}" ]] || continue
name="$(basename "${d}")"
if [[ -f "${d}index.html" ]]; then
if [[ -f "${d}report.tar.gz" ]]; then
echo "<li><a href=\"./${name}/index.html\">${name}</a> — <a href=\"./${name}/report.tar.gz\">report.tar.gz</a></li>"
else
echo "<li><a href=\"./${name}/index.html\">${name}</a></li>"
fi
elif [[ -f "${d}report.tar.gz" ]]; then
echo "<li><a href=\"./${name}/\">${name}/</a> — <a href=\"./${name}/report.tar.gz\">report.tar.gz</a></li>"
else
echo "<li><a href=\"./${name}/\">${name}/</a></li>"
fi
done
echo "</ul></body></html>"
} > "${INDEX}"
URL="${BASE_URL}/${REPO}/${RUN_ID}/${KIND}/"
ARCHIVE_URL="${BASE_URL}/${REPO}/${RUN_ID}/${KIND}/report.tar.gz"
INDEX_URL="${BASE_URL}/${REPO}/${RUN_ID}/"
echo "Report stand: ${STAND}"
echo "Report: ${URL}"
echo "Report archive: ${ARCHIVE_URL}"
echo "Report index: ${INDEX_URL}"
echo "Report path: ${DEST}"
if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then
{
echo "### Test report"
echo "- [${KIND}](${URL})"
echo "- [${KIND} archive](${ARCHIVE_URL})"
echo "- [index](${INDEX_URL})"
} >> "${GITHUB_STEP_SUMMARY}"
fi