fix(deploy): use repo deploy script + expose git_sha in health
Webhook now runs /opt/forwarder/deploy/deploy-pull.sh from git (not stale image copy). Rolling rebuild api then web; health reports FORWARDER_GIT_SHA.
This commit is contained in:
1 parent
210bc7e324
commit
df016a1e34
6 files changed
+68
-16
No files matched your search
@@ -1,5 +1,7 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
ARG FORWARDER_GIT_SHA=unknown
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
@@ -17,6 +19,7 @@ RUN pip install --no-cache-dir -e .
|
||||
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
ENV PYTHONPATH=/app/src
|
||||
ENV FORWARDER_GIT_SHA=$FORWARDER_GIT_SHA
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
|
||||
+2
-1
@@ -1,9 +1,10 @@
|
||||
FROM node:20-alpine AS build
|
||||
ARG GIT_SHA=unknown
|
||||
WORKDIR /app
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
RUN echo "admin-ui build ${GIT_SHA}" && npm run build
|
||||
|
||||
FROM nginx:alpine
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
|
||||
+31
-12
@@ -1,14 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
# BizDocs Forwarder — pull from Gitea + docker compose rebuild (CT 243 / /opt/forwarder).
|
||||
# Invoked by deploy-hook webhook or manually after SSH setup.
|
||||
set -euo pipefail
|
||||
|
||||
REPO_DIR="${FORWARDER_REPO_DIR:-/opt/forwarder}"
|
||||
DEPLOY_BRANCH="${DEPLOY_BRANCH:-main}"
|
||||
COMPOSE="docker compose"
|
||||
STATUS_FILE="$REPO_DIR/storage/deploy-logs/last-deploy.status"
|
||||
|
||||
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] [forwarder-deploy] $*"; }
|
||||
|
||||
write_status() {
|
||||
mkdir -p "$(dirname "$STATUS_FILE")"
|
||||
echo "$1" > "$STATUS_FILE"
|
||||
}
|
||||
|
||||
on_error() {
|
||||
write_status "failed $(date -Iseconds) — see latest deploy-*.log"
|
||||
}
|
||||
trap on_error ERR
|
||||
|
||||
if [ ! -d "$REPO_DIR/.git" ]; then
|
||||
log "FATAL: git repo missing at $REPO_DIR"
|
||||
exit 1
|
||||
@@ -19,7 +29,8 @@ cd "$REPO_DIR"
|
||||
log "Fetching origin/$DEPLOY_BRANCH"
|
||||
git fetch origin "$DEPLOY_BRANCH"
|
||||
git reset --hard "origin/$DEPLOY_BRANCH"
|
||||
log "HEAD $(git rev-parse --short HEAD) — $(git log -1 --format=%s)"
|
||||
GIT_SHA=$(git rev-parse --short HEAD)
|
||||
log "HEAD $GIT_SHA — $(git log -1 --format=%s)"
|
||||
|
||||
if [ ! -f .env ] && [ -f /etc/forwarder/forwarder.env ]; then
|
||||
log "Linking .env from /etc/forwarder/forwarder.env"
|
||||
@@ -27,20 +38,28 @@ if [ ! -f .env ] && [ -f /etc/forwarder/forwarder.env ]; then
|
||||
fi
|
||||
|
||||
if [ ! -f .env ]; then
|
||||
log "FATAL: .env missing (copy .env.example or /etc/forwarder/forwarder.env)"
|
||||
log "FATAL: .env missing"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
log "Building containers (web forced rebuild for admin-ui)"
|
||||
$COMPOSE build api worker deploy-hook
|
||||
$COMPOSE build --no-cache web
|
||||
export FORWARDER_GIT_SHA="$GIT_SHA"
|
||||
|
||||
LOG_FILE="$REPO_DIR/storage/deploy-logs/deploy-$(date +%Y%m%d-%H%M%S).log"
|
||||
mkdir -p "$(dirname "$LOG_FILE")"
|
||||
|
||||
log "Building API + worker ($GIT_SHA)"
|
||||
{
|
||||
$COMPOSE build --build-arg FORWARDER_GIT_SHA="$GIT_SHA" api worker
|
||||
$COMPOSE up -d --force-recreate api worker
|
||||
log "Running migrations"
|
||||
$COMPOSE run --rm migrate
|
||||
|
||||
log "Starting stack"
|
||||
$COMPOSE up -d --force-recreate api web worker deploy-hook
|
||||
|
||||
log "Health check"
|
||||
log "Building admin UI ($GIT_SHA)"
|
||||
$COMPOSE build --build-arg GIT_SHA="$GIT_SHA" web
|
||||
$COMPOSE up -d --force-recreate web
|
||||
log "Refreshing deploy-hook"
|
||||
$COMPOSE build deploy-hook
|
||||
$COMPOSE up -d --force-recreate deploy-hook
|
||||
curl -sf --max-time 10 http://127.0.0.1:8000/api/health >/dev/null
|
||||
log "Deploy OK"
|
||||
write_status "ok $GIT_SHA $(date -Iseconds)"
|
||||
log "Deploy OK ($GIT_SHA)"
|
||||
} >> "$LOG_FILE" 2>&1
|
||||
@@ -14,11 +14,23 @@ $deployBranch = getenv('DEPLOY_BRANCH') ?: 'main';
|
||||
$allowedRepo = 'bitreport.pt/forwarder';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
||||
$statusFile = $repoDir . '/storage/deploy-logs/last-deploy.status';
|
||||
$status = null;
|
||||
if (is_file($statusFile)) {
|
||||
$status = trim((string) file_get_contents($statusFile));
|
||||
}
|
||||
$gitHead = null;
|
||||
$headFile = $repoDir . '/.git/refs/heads/' . $deployBranch;
|
||||
if (is_file($headFile)) {
|
||||
$gitHead = substr(trim((string) file_get_contents($headFile)), 0, 7);
|
||||
}
|
||||
http_response_code(200);
|
||||
echo json_encode([
|
||||
'status' => 'ok',
|
||||
'message' => 'Forwarder deploy webhook. POST Gitea push events.',
|
||||
'repo_dir' => $repoDir,
|
||||
'git_head' => $gitHead,
|
||||
'last_deploy' => $status,
|
||||
], JSON_UNESCAPED_SLASHES);
|
||||
exit;
|
||||
}
|
||||
@@ -81,7 +93,10 @@ if ($ref !== 'refs/heads/' . $deployBranch) {
|
||||
exit;
|
||||
}
|
||||
|
||||
$deployScript = $repoDir . '/deploy/deploy-pull.sh';
|
||||
if (!is_file($deployScript)) {
|
||||
$deployScript = __DIR__ . '/deploy-pull.sh';
|
||||
}
|
||||
if (!is_file($deployScript)) {
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'deploy-pull.sh missing']);
|
||||
|
||||
+10
-2
@@ -29,7 +29,10 @@ services:
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
build: .
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
FORWARDER_GIT_SHA: ${FORWARDER_GIT_SHA:-unknown}
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "8000:8000"
|
||||
@@ -41,7 +44,10 @@ services:
|
||||
command: ["uvicorn", "forwarder.api.app:app", "--host", "0.0.0.0", "--port", "8000", "--app-dir", "src"]
|
||||
|
||||
worker:
|
||||
build: .
|
||||
build:
|
||||
context: .
|
||||
args:
|
||||
FORWARDER_GIT_SHA: ${FORWARDER_GIT_SHA:-unknown}
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
- .env
|
||||
@@ -54,6 +60,8 @@ services:
|
||||
build:
|
||||
context: ./admin-ui
|
||||
dockerfile: Dockerfile
|
||||
args:
|
||||
GIT_SHA: ${FORWARDER_GIT_SHA:-unknown}
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "8080:80"
|
||||
|
||||
@@ -9,4 +9,10 @@ router = APIRouter()
|
||||
|
||||
@router.get("/health", response_model=dict)
|
||||
def health() -> dict:
|
||||
return {"status": "ok", "version": __version__}
|
||||
import os
|
||||
|
||||
return {
|
||||
"status": "ok",
|
||||
"version": __version__,
|
||||
"git_sha": os.getenv("FORWARDER_GIT_SHA", "unknown"),
|
||||
}
|
||||
Reference in new issue
Block a user