Deployment: Add Dockerfile and GitHub Actions CI/CD workflow
This commit is contained in:
1 parent
71e6727a45
commit
68771a24ef
3 files changed
+74
-17
No files matched your search
@@ -0,0 +1,45 @@
|
||||
name: Build and Publish Docker Image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
env:
|
||||
REGISTRY: ghcr.io
|
||||
IMAGE_NAME: ${{ github.repository }}
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Log in to the Container registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ env.REGISTRY }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Extract metadata (tags, labels) for Docker
|
||||
id: meta
|
||||
uses: docker/metadata-action@v5
|
||||
with:
|
||||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
||||
tags: |
|
||||
type=raw,value=latest
|
||||
type=sha,value=${{ github.sha }}
|
||||
|
||||
- name: Build and push Docker image
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
push: true
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
+24
-16
@@ -1,34 +1,42 @@
|
||||
# Stage 1: Build Frontend
|
||||
FROM node:20-slim AS frontend-build
|
||||
# --- Build Frontend ---
|
||||
FROM node:20-alpine AS frontend-builder
|
||||
WORKDIR /app/frontend
|
||||
COPY frontend/package*.json ./
|
||||
RUN npm install
|
||||
COPY frontend/ ./
|
||||
RUN npm run build
|
||||
|
||||
# Stage 2: Build Backend
|
||||
FROM node:20-slim AS backend-build
|
||||
# --- Build Backend ---
|
||||
FROM node:20-alpine AS backend-builder
|
||||
WORKDIR /app/backend
|
||||
COPY backend/package*.json ./
|
||||
RUN npm install
|
||||
COPY backend/ ./
|
||||
RUN npx prisma generate
|
||||
RUN npm run build || (mkdir -p dist && npx tsc -p tsconfig.json)
|
||||
RUN npm run build
|
||||
|
||||
# Stage 3: Final Image
|
||||
FROM node:20-slim
|
||||
# --- Final Image ---
|
||||
FROM node:20-alpine
|
||||
WORKDIR /app
|
||||
RUN apt-get update && apt-get install -y openssl && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=frontend-build /app/frontend/dist ./frontend/dist
|
||||
COPY --from=backend-build /app/backend/dist ./backend/dist
|
||||
COPY --from=backend-build /app/backend/node_modules ./backend/node_modules
|
||||
COPY --from=backend-build /app/backend/prisma ./backend/prisma
|
||||
COPY --from=backend-build /app/backend/package*.json ./backend/
|
||||
# Copy backend
|
||||
COPY --from=backend-builder /app/backend/package*.json ./
|
||||
COPY --from=backend-builder /app/backend/dist ./dist
|
||||
COPY --from=backend-builder /app/backend/prisma ./prisma
|
||||
COPY --from=backend-builder /app/backend/node_modules ./node_modules
|
||||
|
||||
WORKDIR /app/backend
|
||||
# Copy frontend build to be served by backend
|
||||
COPY --from=frontend-builder /app/frontend/dist ./frontend-dist
|
||||
|
||||
# Env vars
|
||||
ENV NODE_ENV=production
|
||||
ENV PORT=3000
|
||||
ENV DATABASE_URL="file:/app/data/dev.db"
|
||||
|
||||
# Create data directory for SQLite
|
||||
RUN mkdir -p /app/data
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
# Script to run migrations and start server
|
||||
CMD ["sh", "-c", "npx prisma migrate deploy && node dist/server.js"]
|
||||
# Start script
|
||||
CMD ["sh", "-c", "npx prisma db push && node dist/server.js"]
|
||||
@@ -2,6 +2,7 @@ import express from 'express';
|
||||
import cors from 'cors';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
const app = express();
|
||||
@@ -186,7 +187,10 @@ app.post('/api/reset', async (req, res) => {
|
||||
});
|
||||
|
||||
// Serve Static Files in Production
|
||||
const frontendPath = path.join(__dirname, '../../frontend/dist');
|
||||
const frontendPath = fs.existsSync(path.join(__dirname, '../frontend-dist'))
|
||||
? path.join(__dirname, '../frontend-dist')
|
||||
: path.join(__dirname, '../../frontend/dist');
|
||||
|
||||
app.use(express.static(frontendPath));
|
||||
|
||||
app.get(/^\/(?!api).*/, (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user