Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/check-runner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Test Self-Hosted Runner

on:
workflow_dispatch:

permissions:
contents: read

jobs:
verify:
runs-on: [self-hosted, linux, x64, unsdg-runner]

steps:
- name: Verify runner
run: |
echo "Runner is working!"
echo "Hostname: $(hostname)"
echo "User: $(whoami)"
echo "Architecture: $(uname -m)"

- name: Verify Docker
run: |
docker ps
docker compose version

- name: Check disk
run: df -h
182 changes: 182 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
name: CI/CD

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

# One run per ref. PR runs get cancelled when a new commit arrives.
# Runs on main are queued, never cancelled, so a deploy is never interrupted halfway.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

permissions:
contents: read

jobs:
backend-test:
name: Backend lint and test
runs-on: ubuntu-latest
timeout-minutes: 30
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v7

- uses: actions/setup-python@v7
with:
python-version: "3.11"
cache: pip
cache-dependency-path: backend/requirements.txt

- name: Install dependencies
run: pip install -r requirements.txt ruff pytest pip-audit

# - name: Lint
# run: ruff check .

# - name: Test
# # Exit code 5 means no tests were collected. Remove the fallback once tests exist.
# run: pytest -q || [ $? -eq 5 ]

- name: Dependency audit
continue-on-error: true
run: pip-audit -r requirements.txt

frontend-test:
name: Frontend lint and test
runs-on: ubuntu-latest
timeout-minutes: 15
defaults:
run:
working-directory: frontend
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: frontend/package-lock.json

- run: npm ci
- run: npm run lint --if-present
- run: npm run typecheck --if-present
- run: npm test --if-present

- name: Dependency audit
continue-on-error: true
run: npm audit --audit-level=high

build:
name: Build and publish ${{ matrix.name }} image
needs: [backend-test, frontend-test]
runs-on: ubuntu-latest
timeout-minutes: 40

permissions:
contents: read
packages: write

strategy:
fail-fast: true
matrix:
include:
- name: backend
context: ./backend
file: ./backend/Dockerfile
build_args: ""
- name: frontend
context: .
file: ./frontend/Dockerfile
build_args: NEXT_PUBLIC_API_BASE_URL=/

steps:
- uses: actions/checkout@v4

- name: Set image prefix
run: echo "IMAGE_PREFIX=ghcr.io/${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_ENV"

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Check runner disk space
run: |
echo "=== Disk space ==="
df -h
echo "=== Docker disk usage ==="
docker system df
echo "=== Docker storage ==="
sudo du -sh /usr/local/lib/docker 2>/dev/null || true

- name: Log in to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and publish image
uses: docker/build-push-action@v6
with:
context: ${{ matrix.context }}
file: ${{ matrix.file }}
build-args: ${{ matrix.build_args }}

# Never import the image into local Docker.
load: false

# Build on PRs; publish on other events.
push: ${{ github.event_name != 'pull_request' }}

tags: |
${{ env.IMAGE_PREFIX }}/unsdg-${{ matrix.name }}:${{ github.sha }}
${{ env.IMAGE_PREFIX }}/unsdg-${{ matrix.name }}:latest

cache-from: type=gha,scope=${{ matrix.name }}
cache-to: type=gha,mode=max,scope=${{ matrix.name }}



deploy:
name: Deploy to AWS VM
needs: build
runs-on: [self-hosted, linux, x64, unsdg-runner]
timeout-minutes: 20
environment: production
permissions:
contents: read
packages: read
env:
APP_DIR: ${{ vars.APP_DIR || '/opt/unsdg' }}
steps:
- uses: actions/checkout@v4

- name: Set image prefix
run: echo "IMAGE_PREFIX=ghcr.io/${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_ENV"

- name: Prepare deployment files
run: |
set -euo pipefail

mkdir -p "$APP_DIR"

install -m 0644 \
docker-compose.prod.yml \
"$APP_DIR/docker-compose.prod.yml"

install -m 0755 \
deploy/deploy.sh \
"$APP_DIR/deploy.sh"

- name: Smoke test
if: vars.APP_URL != ''
run: curl --fail --silent --show-error --retry 5 --retry-delay 5 "${{ vars.APP_URL }}" > /dev/null

# - name: Clean up SSH material
# if: always()
# run: rm -f ~/.ssh/deploy_key
75 changes: 75 additions & 0 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash
# Usage: printf '%s' "$GHCR_TOKEN" | bash deploy.sh <image_tag> <ghcr_user> <image_prefix>
set -euo pipefail

TAG="${1:?image tag is required}"
GHCR_USER="${2:?ghcr user is required}"
IMAGE_PREFIX="${3:?image prefix is required}"

cd "$(dirname "$0")"
COMPOSE="docker compose -f docker-compose.prod.yml"
STATE_FILE=".last_good_tag"
HEALTH_TIMEOUT=600

export IMAGE_PREFIX

# Token arrives on stdin.
docker login ghcr.io -u "$GHCR_USER" --password-stdin

PREVIOUS=""
if [ -f "$STATE_FILE" ]; then
PREVIOUS="$(cat "$STATE_FILE")"
fi

wait_healthy() {
local deadline=$((SECONDS + HEALTH_TIMEOUT))
while [ "$SECONDS" -lt "$deadline" ]; do
local all_ok=1
for name in unsdg-backend unsdg-frontend; do
local status
status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$name" 2>/dev/null || echo missing)"
if [ "$status" != "healthy" ]; then
all_ok=0
fi
done
if [ "$all_ok" -eq 1 ]; then
return 0
fi
sleep 5
done
return 1
}

deploy_tag() {
export IMAGE_TAG="$1"
echo "Deploying tag $IMAGE_TAG"
$COMPOSE pull
$COMPOSE up -d --remove-orphans
}

deploy_tag "$TAG"

if wait_healthy; then
echo "$TAG" > "$STATE_FILE"
# Keep a week of images so a manual rollback stays fast.
docker image prune -af --filter "until=168h" > /dev/null
echo "Deploy succeeded: $TAG"
exit 0
fi

echo "Deploy failed health checks for $TAG" >&2
$COMPOSE logs --tail=100 >&2 || true

if [ -n "$PREVIOUS" ] && [ "$PREVIOUS" != "$TAG" ]; then
echo "Rolling back to $PREVIOUS" >&2
deploy_tag "$PREVIOUS"
if wait_healthy; then
echo "Rollback succeeded: $PREVIOUS" >&2
else
echo "Rollback also failed. Manual intervention required." >&2
fi
else
echo "No previous good tag recorded, nothing to roll back to." >&2
fi

exit 1
46 changes: 46 additions & 0 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
services:
backend:
image: ${IMAGE_PREFIX:?IMAGE_PREFIX is required}/unsdg-backend:${IMAGE_TAG:?IMAGE_TAG is required}
container_name: unsdg-backend
restart: unless-stopped
env_file:
- ./backend/.env
volumes:
- hf-cache:/root/.cache/huggingface
expose:
- "8010"
healthcheck:
test: ["CMD", "python", "-c", "import socket; socket.create_connection(('127.0.0.1', 8010), 3)"]
interval: 30s
timeout: 5s
retries: 5
start_period: 120s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"

frontend:
image: ${IMAGE_PREFIX:?IMAGE_PREFIX is required}/unsdg-frontend:${IMAGE_TAG:?IMAGE_TAG is required}
container_name: unsdg-frontend
restart: unless-stopped
depends_on:
backend:
condition: service_healthy
ports:
- "80:80"
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://127.0.0.1/"]
interval: 30s
timeout: 5s
retries: 3
start_period: 10s
logging:
driver: json-file
options:
max-size: "10m"
max-file: "3"

volumes:
hf-cache:
15 changes: 15 additions & 0 deletions frontend/services/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import { describe, it, expect, vi } from "vitest";

Check warning on line 2 in frontend/services/api.test.ts

View workflow job for this annotation

GitHub Actions / Frontend lint and test

'vi' is defined but never used



import { sdgApi } from "./api";

describe("sdgApi", () => {
it("is available", () => {
expect(sdgApi).toBeDefined();
});
});



1 change: 1 addition & 0 deletions frontend/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "@testing-library/jest-dom/vitest";
17 changes: 17 additions & 0 deletions frontend/types/images.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare module "*.png" {
import type { StaticImageData } from "next/image";
const src: StaticImageData;
export default src;
}

declare module "*.jpg" {
import type { StaticImageData } from "next/image";
const src: StaticImageData;
export default src;
}

declare module "*.jpeg" {
import type { StaticImageData } from "next/image";
const src: StaticImageData;
export default src;
}
Loading