Skip to content

Commit

Permalink
Add docker configurations (#11)
Browse files Browse the repository at this point in the history
* Setup sveltekit for node

* Add Dockerfile configs for ui, queue, and api

* Add workflow for pushing images

* Update variables and env vars

* Add prereleased and released triggers

* Remove env block
  • Loading branch information
sneakycrow authored Nov 9, 2024
1 parent 1998398 commit 6679e7c
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 4 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/build-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: build & push images

on:
release:
types:
- prereleased
- released
workflow_dispatch:

jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Get release version
id: version
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DO_REGISTRY_KEY }}

- name: Log in to DO Container Registry
run: doctl registry login --expiry-seconds 600

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

# Build and push API image
- name: Build and push API image
uses: docker/build-push-action@v5
with:
context: .
file: config/api.Dockerfile
push: true
platforms: linux/amd64
tags: |
${{ secrets.DO_REGISTRY }}/${{ env.API_IMAGE }}:latest
${{ secrets.DO_REGISTRY }}/${{ env.API_IMAGE }}:${{ steps.version.outputs.VERSION }}
cache-from: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ env.API_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ env.API_IMAGE }}:buildcache,mode=max

# Build and push Queue image
- name: Build and push Queue image
uses: docker/build-push-action@v5
with:
context: .
file: config/queue.Dockerfile
push: true
platforms: linux/amd64
tags: |
${{ secrets.DO_REGISTRY }}/${{ env.QUEUE_IMAGE }}:latest
${{ secrets.DO_REGISTRY }}/${{ env.QUEUE_IMAGE }}:${{ steps.version.outputs.VERSION }}
cache-from: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ env.QUEUE_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ env.QUEUE_IMAGE }}:buildcache,mode=max

# Build and push UI image
- name: Build and push UI image
uses: docker/build-push-action@v5
with:
context: .
file: config/ui.Dockerfile
push: true
platforms: linux/amd64
tags: |
${{ secrets.DO_REGISTRY }}/${{ env.UI_IMAGE }}:latest
${{ secrets.DO_REGISTRY }}/${{ env.UI_IMAGE }}:${{ steps.version.outputs.VERSION }}
cache-from: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ env.UI_IMAGE }}:buildcache
cache-to: type=registry,ref=${{ secrets.DO_REGISTRY }}/${{ env.UI_IMAGE }}:buildcache,mode=max

- name: Image digest
run: |
echo "API image digest: $(doctl registry repository digest-list ${{ env.API_IMAGE }} --format Tag,Digest --no-header | grep latest)"
echo "Queue image digest: $(doctl registry repository digest-list ${{ env.QUEUE_IMAGE }} --format Tag,Digest --no-header | grep latest)"
echo "UI image digest: $(doctl registry repository digest-list ${{ env.UI_IMAGE }} --format Tag,Digest --no-header | grep latest)"
53 changes: 53 additions & 0 deletions config/api.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Build stage
FROM --platform=$BUILDPLATFORM rust:1.82-slim-bullseye as builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
pkg-config \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*

# Create a new empty shell project
WORKDIR /app

# Copy workspace configuration
COPY Cargo.toml Cargo.lock ./

# Copy all workspace members
COPY packages/db ./packages/db
COPY packages/vod ./packages/vod
COPY services/forge-queue ./services/forge-queue
COPY services/silo-api ./services/silo-api

# Build the project for release
WORKDIR /app
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/app/target \
cargo build --release -p api && \
cp /app/target/release/api /usr/local/bin/api

# Runtime stage
FROM --platform=$TARGETPLATFORM debian:bullseye-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
ca-certificates \
libssl1.1 \
libpq5 \
&& rm -rf /var/lib/apt/lists/*

# Create uploads directory
RUN mkdir -p /app/uploads && \
chmod 777 /app/uploads

# Set working directory
WORKDIR /app

# Copy the binary from builder
COPY --from=builder /usr/local/bin/api /usr/local/bin/api

# Expose the default port
EXPOSE 3000

# Set the entrypoint
ENTRYPOINT ["/usr/local/bin/api"]
43 changes: 43 additions & 0 deletions config/queue.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Build stage
FROM --platform=$BUILDPLATFORM rust:1.82-slim-bullseye as builder

# Install build dependencies
RUN apt-get update && apt-get install -y \
pkg-config \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*

# Create a new empty shell project
WORKDIR /app

# Copy workspace configuration
COPY Cargo.toml Cargo.lock ./

# Copy all workspace members
COPY packages/db ./packages/db
COPY packages/vod ./packages/vod
COPY services/forge-queue ./services/forge-queue
COPY services/silo-api ./services/silo-api

# Build the project for release
WORKDIR /app/services/forge-queue
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/app/target \
cargo build --release && \
cp /app/target/release/queue /usr/local/bin/queue

# Runtime stage
FROM --platform=$TARGETPLATFORM debian:bullseye-slim

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
ca-certificates \
ffmpeg \
libpq5 \
&& rm -rf /var/lib/apt/lists/*

# Copy the binary from builder
COPY --from=builder /usr/local/bin/queue /usr/local/bin/queue

# Set the entrypoint
ENTRYPOINT ["/usr/local/bin/queue"]
38 changes: 38 additions & 0 deletions config/ui.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Build stage
FROM --platform=$BUILDPLATFORM node:20-slim as builder

# Set working directory
WORKDIR /app

# Copy package files
COPY services/barn-ui/package.json services/barn-ui/yarn.lock ./

# Install dependencies
RUN yarn install --frozen-lockfile

# Copy source files
COPY services/barn-ui .

# Build the application
RUN yarn build

# Runtime stage
FROM --platform=$TARGETPLATFORM node:20-slim

# Set working directory
WORKDIR /app

# Copy package files for production dependencies
COPY services/barn-ui/package.json services/barn-ui/yarn.lock ./

# Install production dependencies only
RUN yarn install --production --frozen-lockfile

# Copy built application from builder
COPY --from=builder /app/build ./build

# Expose default SvelteKit port
EXPOSE 3000

# Start the application
CMD ["node", "build"]
90 changes: 88 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set dotenv-load := true

# Service startup commands
start-api:
cargo run -p api

Expand All @@ -9,11 +10,15 @@ start-web:
start-queue:
cargo run -p queue

# Development commands
dev-api: start-api

dev-web:
yarn dev

dev-queue: start-queue

# Database commands
init-db: create-db migrate

create-db:
Expand All @@ -34,15 +39,96 @@ mig-add mig_name:
revert:
sqlx migrate run --source {{ mig_source }}

# Build commands
build-web:
yarn build

build-api:
cargo build -p api
cargo build -p api --release

build-queue:
cargo build -p queue
cargo build -p queue --release

# Docker build commands - Queue Service (forge-queue)
biq: build-image-queue # Shorthand for building queue image
build-image-queue: (build-image-queue-local) # Default to local build for queue

# Build queue image for local development (M1/ARM64)
build-image-queue-local:
docker build --platform linux/arm64 -t forge-queue -f config/queue.Dockerfile .

# Build queue image for production (AMD64)
build-image-queue-prod:
docker build --platform linux/amd64 -t forge-queue -f config/queue.Dockerfile .

# Run queue container with local build
run-queue: build-image-queue
docker run -e DATABASE_URL=${DATABASE_URL} forge-queue

# Run queue container with production build
run-queue-prod: build-image-queue-prod
docker run -e DATABASE_URL=${DATABASE_URL} forge-queue

# Docker build commands - API Service (silo-api)
bia: build-image-api # Shorthand for building API image
build-image-api: (build-image-api-local) # Default to local build for API

# Build API image for local development (M1/ARM64)
build-image-api-local:
docker build --platform linux/arm64 -t silo-api -f config/api.Dockerfile .

# Build API image for production (AMD64)
build-image-api-prod:
docker build --platform linux/amd64 -t silo-api -f config/api.Dockerfile .

# Run API container with local build
run-api: build-image-api
docker run -p 3000:3000 -e DATABASE_URL=${DATABASE_URL} -e JWT_SECRET=${JWT_SECRET} silo-api

# Run API container with production build
run-api-prod: build-image-api-prod
docker run -p 3000:3000 -e DATABASE_URL=${DATABASE_URL} -e JWT_SECRET=${JWT_SECRET} silo-api

# Docker build commands - UI Service (barn-ui)
biu: build-image-ui # Shorthand for building UI image
build-image-ui: (build-image-ui-local) # Default to local build for UI

# Build UI image for local development (M1/ARM64)
build-image-ui-local:
docker build --platform linux/arm64 -t barn-ui -f config/ui.Dockerfile .

# Build UI image for production (AMD64)
build-image-ui-prod:
docker build --platform linux/amd64 -t barn-ui -f config/ui.Dockerfile .

# Run UI container with local build
run-ui: build-image-ui
docker run -p 3000:3000 barn-ui

# Run UI container with production build
run-ui-prod: build-image-ui-prod
docker run -p 3000:3000 barn-ui

# Utility commands
sync: sync-web
sync-web:
yarn sync

# Clean commands
clean:
cargo clean
rm -rf node_modules
docker rmi forge-queue || true

# Verification commands
verify:
cargo check
cargo test
cargo clippy -- -D warnings
cargo fmt --all -- --check

# Run all services
dev: dev-web dev-api dev-queue

# Build all services
build: build-web build-api build-queue
2 changes: 1 addition & 1 deletion services/barn-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"sync": "svelte-kit sync"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^3.0.0",
"@sveltejs/adapter-node": "^2.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
"@tailwindcss/aspect-ratio": "^0.4.2",
Expand Down
2 changes: 1 addition & 1 deletion services/barn-ui/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import adapter from '@sveltejs/adapter-auto';
import adapter from '@sveltejs/adapter-node';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
Expand Down

0 comments on commit 6679e7c

Please sign in to comment.