#!/usr/bin/env bash # Example promote script for Gitea webhook → LXC (BIT-465). Adjust branches, users, and # restart policy for your host. Run as the user that owns the repo (often root on first setup). set -euo pipefail _ENV_FILE="/etc/error-reporting/env" if [[ ! -f "${_ENV_FILE}" ]]; then echo "webhook-deploy: missing ${_ENV_FILE}" >&2 exit 1 fi set -a # shellcheck disable=SC1090 source "${_ENV_FILE}" set +a _ROOT="${ER_REPO_ROOT:?ER_REPO_ROOT must be set in ${_ENV_FILE}}" cd "${_ROOT}" # Avoid `fatal: detected dubious ownership` when root runs git on a tree owned by error-reporting. git config --global --add safe.directory "${_ROOT}" 2>/dev/null || true # Match your deploy branch (repository uses master today). _DEPLOY_BRANCH="${ER_DEPLOY_BRANCH:-master}" git fetch origin "${_DEPLOY_BRANCH}" git checkout "${_DEPLOY_BRANCH}" git merge --ff-only "origin/${_DEPLOY_BRANCH}" echo "webhook-deploy: npm ci + build (ingest-api, admin-ui, collector-worker, api)" (cd "${_ROOT}/ingest-api" && npm ci --include=dev && npm run build) (cd "${_ROOT}/admin-ui" && npm ci --include=dev && npm run build) (cd "${_ROOT}/collector-worker" && npm ci --include=dev && npm run build) (cd "${_ROOT}/api" && npm ci --include=dev && npm run build) if getent passwd error-reporting >/dev/null 2>&1; then chown -R error-reporting:error-reporting "${_ROOT}" fi "${_ROOT}/deploy/lxc/write-version-json.sh" "${_ROOT}/deploy/lxc/verify.sh" if [[ "${ER_WEBHOOK_RESTART_SERVICES:-1}" == "1" ]]; then systemctl restart bitreport-error-reporting-ingest-api.service systemctl restart bitreport-error-reporting-admin-ui.service systemctl restart bitreport-error-reporting-collector-worker.service systemctl restart bitreport-error-reporting-api.service # Auto-apply resolve-fixed-patterns after every deploy so code fixes clear stale incidents. if [[ -n "${ERROR_RESOLVE_API_KEY:-}" ]]; then _api_port="${ER_API_PORT:-3200}" _ready=0 for _i in $(seq 1 20); do if curl -sf "http://localhost:${_api_port}/health" >/dev/null 2>&1; then _ready=1; break fi sleep 3 done if [[ "${_ready}" == "1" ]]; then curl -sf -X POST "http://localhost:${_api_port}/api/error-monitoring/resolve-fixed-patterns" \ -H "Content-Type: application/json" \ -d "{\"api_key\":\"${ERROR_RESOLVE_API_KEY}\"}" \ >/dev/null 2>&1 \ && echo "webhook-deploy: resolve-fixed-patterns applied" \ || echo "webhook-deploy: WARN resolve-fixed-patterns failed (non-fatal)" else echo "webhook-deploy: WARN API not ready after 60s, skipping resolve-fixed-patterns" fi fi fi echo "webhook-deploy: done"