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
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Dockerfile
.dockerignore
node_modules
npm-debug.log
README.md
.next
.git
78 changes: 78 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: CI/CD - Giftizy

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

permissions:
contents: read
packages: write
id-token: write

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

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

- name: Log in to GHCR
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GHCR_PAT }}

- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: ghcr.io/jordan-182/giftizy:latest
platforms: linux/amd64

deploy-on-vps:
needs: build-and-push
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Deploy to VPS via SSH
uses: appleboy/[email protected]
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USER }}
key: ${{ secrets.VPS_SSH_PRIVATE_KEY }}
port: ${{ secrets.VPS_SSH_PORT }}
script: |
# Option A : create/update secrets (only if passed as GH secrets)
# Note: Be careful: creating secrets overwrites if same name exists; we remove before create for simplicity
# echo "${DB_PASSWORD}" | docker secret rm DB_PASSWORD || true
# echo "${DB_PASSWORD}" | docker secret create DB_PASSWORD -
# For security, prefer manually creating secrets on the VPS or use 'docker secret inspect' to check before replace.

# Pull latest image (optional)
docker pull ghcr.io/jordan-182/giftizy:latest || true

# Deploy stack (file should exist on VPS at /srv/apps/giftizy/docker-stack.yml)
docker stack deploy -c /srv/apps/giftizy/docker-stack.yml giftizy

# Wait for service to be ready
echo "Waiting for service to be ready..."
sleep 30

# Run migrations
echo "Running database migrations..."
SERVICE_ID=$(docker service ps giftizy_app --format "{{.ID}}" --filter "desired-state=running" | head -1)
if [ ! -z "$SERVICE_ID" ]; then
CONTAINER_ID=$(docker inspect --format="{{.Status.ContainerStatus.ContainerID}}" $SERVICE_ID)
if [ ! -z "$CONTAINER_ID" ]; then
docker exec $CONTAINER_ID npx prisma migrate deploy || echo "Migration failed or no migrations to run"
fi
fi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ yarn-error.log*

# env files (can opt-in for committing if needed)
.env
.env.prod
docker-stack.yml

# vercel
.vercel
Expand Down
40 changes: 40 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Dockerfile (multi-stage) - optimized for Next.js w/ Prisma
# Build stage
FROM node:22-alpine AS builder
WORKDIR /app

# Install pnpm and build deps
RUN corepack enable pnpm
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile

# Copy sources
COPY . .

# Generate prisma client & build
RUN pnpm exec prisma generate
RUN pnpm run build

# Production image
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production

# Create a non-root user
RUN addgroup -S appgroup && adduser -S appuser -G appgroup

# Copy built artifacts + node_modules (only prod)
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/prisma ./prisma

# Copy entrypoint script
COPY ./docker/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

USER appuser
EXPOSE 3000
ENV PORT=3000
CMD ["/entrypoint.sh"]
38 changes: 38 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/sh
set -e

echo "🔧 Generating environment variables from Docker Swarm secrets..."

# Function to read a secret if it exists
read_secret() {
local secret_name="$1"
if [ -f "/run/secrets/$secret_name" ]; then
cat "/run/secrets/$secret_name"
else
echo ""
fi
}

# Generate env file
cat <<EOF > .env.production
NEXT_PUBLIC_API_URL="$(read_secret giftizy_next_public_api_url)"
APP_NAME="$(read_secret giftizy_app_name)"

BETTER_AUTH_SECRET="$(read_secret giftizy_better_auth_secret)"
BETTER_AUTH_URL="$(read_secret giftizy_better_auth_url)"

DATABASE_URL="$(read_secret giftizy_database_url)"

ADMIN_EMAILS="$(read_secret giftizy_admin_emails)"

GOOGLE_CLIENT_ID="$(read_secret giftizy_google_client_id)"
GOOGLE_CLIENT_SECRET="$(read_secret giftizy_google_client_secret)"

NODEMAILER_USER="$(read_secret giftizy_nodemailer_user)"
NODEMAILER_APP_PASSWORD="$(read_secret giftizy_nodemailer_app_password)"
EOF

echo "✅ .env.production generated successfully"

echo "🚀 Starting application..."
exec "$@"
1 change: 1 addition & 0 deletions next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const nextConfig: NextConfig = {
},
],
},
output: "standalone",
};

export default nextConfig;
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "prisma generate && next dev --turbopack",
"build": "prisma generate && next build --turbopack",
"start": "next start",
"lint": "eslint"
"lint": "eslint",
"migrate": "prisma migrate deploy"
},
"dependencies": {
"@node-rs/argon2": "^2.0.2",
Expand All @@ -27,12 +28,12 @@
"clsx": "^2.1.1",
"date-fns": "^4.1.0",
"lucide-react": "^0.544.0",
"next": "15.5.4",
"next": "15.5.7",
"next-themes": "^0.4.6",
"nodemailer": "^7.0.6",
"react": "19.1.0",
"react": "19.1.2",
"react-day-picker": "^9.11.1",
"react-dom": "19.1.0",
"react-dom": "19.1.2",
"sonner": "^2.0.7",
"tailwind-merge": "^3.3.1",
"vaul": "^1.1.2",
Expand All @@ -46,7 +47,7 @@
"@types/react": "^19",
"@types/react-dom": "^19",
"eslint": "^9",
"eslint-config-next": "15.5.4",
"eslint-config-next": "15.5.7",
"prisma": "^6.16.2",
"tailwindcss": "^4",
"tw-animate-css": "^1.4.0",
Expand Down
Loading