diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3c1eda2 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +node_modules +.next +npm-debug.log +Dockerfile +docker-compose.yml +.git \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..85798af --- /dev/null +++ b/.env.example @@ -0,0 +1,12 @@ +# Environnement production pour docker-compose (service clock-prod) +NODE_ENV=production +NEXT_TELEMETRY_DISABLED=1 +HOSTNAME=0.0.0.0 +PORT=3000 +TZ=Europe/Paris +APP_PORT=3000 + +# Informations du projet +SITE_URL=https://clock.arthurp.fr +CONTACT_URL=https://contact.arthurp.fr +CONTACT_EMAIL=contact@arthurp.fr diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..db756a4 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,27 @@ +name: Deploy Docker + +on: + push: + branches: ["main"] + +jobs: + deploy: + runs-on: self-hosted + + steps: + - name: Deploy on server + run: | + set -e + + echo "Go to app folder" + cd /opt/apps/clock + + echo "Pull latest code" + git pull origin main + + echo "Build & restart containers" + docker compose down || true + docker compose build + docker compose up -d + + echo "Deploy finished" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1f3f329..0c7f516 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ yarn-error.log* # env files (can opt-in for committing if needed) .env* +!.env.example # vercel .vercel diff --git a/Dockerfile b/Dockerfile index 818b55e..79184ea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,32 +1,28 @@ -FROM node:22-alpine AS base -WORKDIR /app -ARG SITE_URL=https://clock.arthurp.fr -ARG CONTACT_URL=https://contact.arthurp.fr -ARG CONTACT_EMAIL=contact@arthurp.fr -ENV SITE_URL=${SITE_URL} -ENV CONTACT_URL=${CONTACT_URL} -ENV CONTACT_EMAIL=${CONTACT_EMAIL} -ENV NEXT_TELEMETRY_DISABLED=1 +# ===== BUILD ===== +FROM node:20-alpine AS builder -FROM base AS deps -COPY package.json package-lock.json ./ +WORKDIR /app + +COPY package*.json ./ RUN npm ci -FROM deps AS dev COPY . . -EXPOSE 3000 -CMD ["npm", "run", "dev", "--", "--hostname", "0.0.0.0", "--port", "3000"] -FROM deps AS builder -COPY . . RUN npm run build -FROM base AS runner + +# ===== RUN ===== +FROM node:20-alpine AS runner + +WORKDIR /app + ENV NODE_ENV=production -ENV HOSTNAME=0.0.0.0 -ENV PORT=3000 + +# Standalone output +COPY --from=builder /app/public ./public COPY --from=builder /app/.next/standalone ./ COPY --from=builder /app/.next/static ./.next/static -COPY --from=builder /app/public ./public + EXPOSE 3000 + CMD ["node", "server.js"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index de0842e..244db1b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,41 +1,22 @@ services: - clock-dev: - profiles: ["dev"] + clock: build: context: . - target: dev - args: - SITE_URL: ${SITE_URL} - CONTACT_URL: ${CONTACT_URL} - CONTACT_EMAIL: ${CONTACT_EMAIL} - container_name: clock-dev - ports: - - "3000:3000" - volumes: - - .:/app - - /app/node_modules - - /app/.next - env_file: - - .env - environment: - NEXT_TELEMETRY_DISABLED: "1" - NODE_ENV: development + dockerfile: Dockerfile + + container_name: clock - clock-prod: - profiles: ["prod"] - build: - context: . - target: runner - args: - SITE_URL: ${SITE_URL} - CONTACT_URL: ${CONTACT_URL} - CONTACT_EMAIL: ${CONTACT_EMAIL} - container_name: clock-prod - restart: unless-stopped ports: - - "3007:3000" + - "${APP_PORT}:3000" + env_file: - .env - environment: - NEXT_TELEMETRY_DISABLED: "1" - NODE_ENV: production \ No newline at end of file + + restart: unless-stopped + + healthcheck: + test: ["CMD", "wget", "-q", "--spider", "http://localhost:3000/"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 20s \ No newline at end of file diff --git a/package.json b/package.json index 1c77d7c..8991817 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "scripts": { "dev": "next dev", "build": "next build", - "start": "next start", + "start": "node .next/standalone/server.js", + "docker": "docker compose up --build", "lint": "eslint" }, "dependencies": {