93 lines
3.1 KiB
YAML
93 lines
3.1 KiB
YAML
name: Test & Deploy API
|
|
run-name: ${{ gitea.ref_name }} → Docker LXC
|
|
|
|
on:
|
|
push:
|
|
tags:
|
|
- 'v*'
|
|
|
|
jobs:
|
|
# ── 1. Run tests in an isolated Python container ──────────────────
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
container:
|
|
image: python:3.12-slim
|
|
|
|
steps:
|
|
- name: Install git
|
|
run: apt-get update && apt-get install -y --no-install-recommends git
|
|
|
|
- name: Checkout Code
|
|
run: |
|
|
git clone --depth 1 --branch "${GITHUB_REF_NAME}" \
|
|
"http://10.0.0.119:3000/${GITHUB_REPOSITORY}.git" . || \
|
|
git clone --depth 1 "http://10.0.0.119:3000/${GITHUB_REPOSITORY}.git" . && \
|
|
git checkout "${GITHUB_SHA}"
|
|
|
|
- name: Install Dependencies
|
|
run: pip install --no-cache-dir -r requirements.txt
|
|
|
|
- name: Run Linter
|
|
run: ruff check app/ tests/
|
|
|
|
- name: Run Tests
|
|
run: pytest tests/ -v --tb=short
|
|
|
|
# ── 2. Deploy to Docker LXC via SSH ─────────────────────────────────
|
|
deploy:
|
|
needs: test
|
|
runs-on: ubuntu-latest
|
|
if: gitea.event_name == 'push'
|
|
|
|
steps:
|
|
- name: Deploy via SSH
|
|
uses: appleboy/ssh-action@v1.0.0
|
|
with:
|
|
host: ${{ secrets.SSH_HOST }}
|
|
username: ${{ secrets.SSH_USER }}
|
|
key: ${{ secrets.SSH_KEY }}
|
|
script: |
|
|
set -e
|
|
DEPLOY_DIR="/opt/adiuva-api"
|
|
REPO_URL="http://10.0.0.119:3000/${{ gitea.repository }}.git"
|
|
TAG="${{ gitea.ref_name }}"
|
|
|
|
# ── Pull latest code ──
|
|
cd /tmp && rm -rf adiuva-api-deploy
|
|
git clone --depth 1 --branch "${TAG}" "${REPO_URL}" adiuva-api-deploy
|
|
|
|
# ── Sync source (preserve .env) ──
|
|
cp -rf /tmp/adiuva-api-deploy/app/ \
|
|
/tmp/adiuva-api-deploy/alembic/ \
|
|
/tmp/adiuva-api-deploy/alembic.ini \
|
|
/tmp/adiuva-api-deploy/Dockerfile \
|
|
/tmp/adiuva-api-deploy/docker-compose.yml \
|
|
/tmp/adiuva-api-deploy/requirements.txt \
|
|
"$DEPLOY_DIR/"
|
|
rm -rf /tmp/adiuva-api-deploy
|
|
|
|
# ── Verify .env ──
|
|
if [ ! -f "$DEPLOY_DIR/.env" ]; then
|
|
echo "❌ $DEPLOY_DIR/.env not found. Create it before deploying."
|
|
exit 1
|
|
fi
|
|
|
|
# ── Build & restart ──
|
|
cd "$DEPLOY_DIR"
|
|
docker compose down --remove-orphans || true
|
|
docker compose up -d --build
|
|
|
|
# ── Migrations ──
|
|
docker compose exec -T app alembic upgrade head
|
|
|
|
# ── Health check ──
|
|
echo "Waiting for app..."
|
|
sleep 5
|
|
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/v1/health)
|
|
if [ "$HTTP_CODE" -eq 200 ]; then
|
|
echo "✅ API is healthy (HTTP ${HTTP_CODE})"
|
|
else
|
|
echo "❌ Health check failed (HTTP ${HTTP_CODE})"
|
|
docker compose logs app --tail=50
|
|
exit 1
|
|
fi |