Skip to content

Deployment and Operations

Rajesh Mani edited this page Sep 13, 2026 · 1 revision

StudentAppBackend Banner

Deployment & Operations Guide


1. Overview

StudentAppBackend is engineered for continuous deployment across modern containerized infrastructure (Render, Kubernetes, AWS ECS, GCP Cloud Run) and standalone Linux virtual machines.


2. Multi-Stage Docker Architecture

The project features an optimized multi-stage Dockerfile:

graph LR
    subgraph BuildStage["Stage 1: Build (swift:6.0-noble)"]
        Source[Swift Source + Package.swift] --> Resolve[Resolve Dependencies Cache Layer]
        Resolve --> Compile[swift build -c release --static-swift-stdlib -Xlinker -ljemalloc]
        Compile --> StagingArea[/staging: Binary + Resources]
    end

    subgraph RuntimeStage["Stage 2: Run (ubuntu:noble)"]
        Base[Minimal Ubuntu Noble] --> Deps[Install libjemalloc2 & ca-certificates]
        Deps --> User[Create unprivileged user: vapor:vapor]
        User --> Copy[Copy /staging -> /app]
        Copy --> Run[ENTRYPOINT ./StudentAppBackend serve --port 8080]
    end

    StagingArea --> Copy
Loading

Key Production Enhancements

  • Memory Allocation (jemalloc): Replaces standard glibc malloc with jemalloc to mitigate fragmentation during long-running async event loops.
  • Static Linking: Builds with --static-swift-stdlib to minimize runtime shared library dependencies.
  • Least-Privilege Security: Runs as an unprivileged system user (vapor:vapor, UID 1000) rather than root.
  • Crash Reporting: Injects SWIFT_BACKTRACE configuration for detailed symbolic crash traces.

3. GitHub Actions CI/CD Pipeline

The workflow defined in .github/workflows/swift.yml automates quality checks and container distribution:

graph TD
    Push[Push to main / tags / PR] --> TestJob[Job: Build & Test (postgres)]
    
    subgraph TestJobPipeline["Test Environment"]
        PostgresContainer[postgres:16-alpine Service Container]
        HealthCheck[Wait for pg_isready Healthcheck]
        SwiftTest[swift test -v]
        PostgresContainer --> HealthCheck --> SwiftTest
    end

    TestJob --> DockerBuildJob[Job: Build Docker Image (Dry Run)]
    DockerBuildJob -->|PR Only| PRComplete[PR Verification Passed]
    DockerBuildJob -->|Push to main or tag v*| DockerPublishJob[Job: Publish to GHCR]
    DockerPublishJob --> GHCRRegistry[(ghcr.io/rajeshm20/studentappbackend)]
Loading

Container Registry Images

Published container images are stored in GitHub Packages:

  • ghcr.io/rajeshm20/studentappbackend:latest (tracks main)
  • ghcr.io/rajeshm20/studentappbackend:v2.0.0 (version release)
  • ghcr.io/rajeshm20/studentappbackend:sha-<commit> (commit SHA tag)

4. Standalone Linux Binary Compilation

For environments requiring direct host execution (systemd, bare metal, EC2) without Docker virtualization:

Option A: Native Linux Compilation (Ubuntu / Debian / WSL)

# 1. Install jemalloc performance headers
sudo apt-get update && sudo apt-get install -y libjemalloc-dev

# 2. Build release binary
swift build -c release \
  --product StudentAppBackend \
  --static-swift-stdlib \
  -Xlinker -ljemalloc

# Executable is generated at: .build/release/StudentAppBackend

Option B: Build Linux Binary from macOS via Docker

# Compile inside official Swift 6.0 Linux container
docker run --rm \
  -v "$PWD":/workspace \
  -w /workspace \
  swift:6.0-noble \
  swift build -c release --product StudentAppBackend --static-swift-stdlib

# Or extract the optimized binary from the multi-stage Docker build:
docker build --target build -t studentapp-builder .
docker run --rm studentapp-builder cat /staging/StudentAppBackend > ./StudentAppBackend-linux
chmod +x ./StudentAppBackend-linux

5. Health Probes & Observability

StudentAppBackend exposes dedicated probe endpoints:

  • GET /health/live: Fast liveness probe returning {"status":"ok"}. Verifies that the HTTP server event loop is actively accepting sockets.
  • GET /health/ready: Readiness probe executing SELECT 1 against PostgreSQL. Returns 200 OK ({"status":"ready"}) if the connection pool is healthy, or 503 Service Unavailable if database connectivity is lost.

Kubernetes Probe Example

livenessProbe:
  httpGet:
    path: /health/live
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10
readinessProbe:
  httpGet:
    path: /health/ready
    port: 8080
  initialDelaySeconds: 10
  periodSeconds: 15