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
|
# --- Build Frontend ---
|
||||||
FROM node:20-slim AS frontend-build
|
FROM node:20-alpine AS frontend-builder
|
||||||
WORKDIR /app/frontend
|
WORKDIR /app/frontend
|
||||||
COPY frontend/package*.json ./
|
COPY frontend/package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
COPY frontend/ ./
|
COPY frontend/ ./
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# Stage 2: Build Backend
|
# --- Build Backend ---
|
||||||
FROM node:20-slim AS backend-build
|
FROM node:20-alpine AS backend-builder
|
||||||
WORKDIR /app/backend
|
WORKDIR /app/backend
|
||||||
COPY backend/package*.json ./
|
COPY backend/package*.json ./
|
||||||
RUN npm install
|
RUN npm install
|
||||||
COPY backend/ ./
|
COPY backend/ ./
|
||||||
RUN npx prisma generate
|
RUN npx prisma generate
|
||||||
RUN npm run build || (mkdir -p dist && npx tsc -p tsconfig.json)
|
RUN npm run build
|
||||||
|
|
||||||
# Stage 3: Final Image
|
# --- Final Image ---
|
||||||
FROM node:20-slim
|
FROM node:20-alpine
|
||||||
WORKDIR /app
|
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 backend
|
||||||
COPY --from=backend-build /app/backend/dist ./backend/dist
|
COPY --from=backend-builder /app/backend/package*.json ./
|
||||||
COPY --from=backend-build /app/backend/node_modules ./backend/node_modules
|
COPY --from=backend-builder /app/backend/dist ./dist
|
||||||
COPY --from=backend-build /app/backend/prisma ./backend/prisma
|
COPY --from=backend-builder /app/backend/prisma ./prisma
|
||||||
COPY --from=backend-build /app/backend/package*.json ./backend/
|
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 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
|
EXPOSE 3000
|
||||||
|
|
||||||
# Script to run migrations and start server
|
# Start script
|
||||||
CMD ["sh", "-c", "npx prisma migrate deploy && node dist/server.js"]
|
CMD ["sh", "-c", "npx prisma db push && node dist/server.js"]
|
||||||
@@ -2,6 +2,7 @@ import express from 'express';
|
|||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
const app = express();
|
const app = express();
|
||||||
@@ -186,7 +187,10 @@ app.post('/api/reset', async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Serve Static Files in Production
|
// 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.use(express.static(frontendPath));
|
||||||
|
|
||||||
app.get(/^\/(?!api).*/, (req, res) => {
|
app.get(/^\/(?!api).*/, (req, res) => {
|
||||||
|
|||||||
Reference in new issue
Block a user