Skip to content

Commit

Permalink
chore(deploy) add cotainers redis and kafka e adjust dockerfile and g…
Browse files Browse the repository at this point in the history
…ithub action
  • Loading branch information
DiegoPorfirio01 committed Jan 3, 2025
1 parent c447a27 commit d0eb45a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 89 deletions.
108 changes: 86 additions & 22 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ name: Chat Lucy Node API Production

on:
push:
branches: [master]
paths:
- 'apps/api/**'
- 'packages/**'
- 'config/**'
- 'package.json'
- '.github/workflows/**'
- 'pnpm-workspace.yaml'
- 'pnpm-lock.yaml'

branches: [main]
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -23,30 +14,103 @@ jobs:
run: docker login -u ${{secrets.DOCKER_USERNAME}} -p ${{secrets.DOCKER_PASSWORD}}

- name: Build docker image
run: docker build --build-arg DATABASE_URL="${{secrets.DATABASE_URL}}" -t ${{secrets.DOCKER_USERNAME}}/chat_lucy_node_api:latest -f apps/api/Dockerfile .
run: |
docker build --build-arg DATABASE_URL="${{secrets.DATABASE_URL}}" \
-t ${{secrets.DOCKER_USERNAME}}/chat_lucy_api:${{ github.sha }} -f apps/api/Dockerfile .
- name: Publish image
run: docker push ${{secrets.DOCKER_USERNAME}}/chat_lucy_node_api:latest
- name: Publish images
run: |
docker push ${{secrets.DOCKER_USERNAME}}/chat_lucy_api:${{ github.sha }}
deploy:
needs: build
runs-on: [self-hosted,main,prod]
steps:
- name: Remove container
run: docker rm -f chat-lucy-node-api || true
- name: Clean up old images
run: docker system prune -af

- name: Create Docker network
run: docker network create chat-lucy-network || true

- name: Create Docker volumes
run: |
docker volume create chat-lucy-redis-data || true
docker volume create chat-lucy-kafka-data || true
docker volume create chat-lucy-zookeeper-data || true
- name: Deploy Redis with password
run: |
docker rm -f chat-lucy-redis || true
docker run -d \
--name chat-lucy-redis \
--network chat-lucy-network \
--restart unless-stopped \
-v chat-lucy-redis-data:/data \
-p 127.0.0.1:6379:6379 \
redis:alpine redis-server --requirepass ${{secrets.REDIS_PASSWORD}} --appendonly yes
- name: Deploy Kafka and Zookeeper
run: |
docker rm -f chat-lucy-zookeeper || true
docker rm -f chat-lucy-kafka || true
docker run -d \
--name chat-lucy-zookeeper \
--network chat-lucy-network \
--restart unless-stopped \
-v chat-lucy-zookeeper-data:/data \
-p 2181:2181 \
wurstmeister/zookeeper
docker run -d \
--name chat-lucy-kafka \
--network chat-lucy-network \
--restart unless-stopped \
-v chat-lucy-kafka-data:/kafka \
-p 127.0.0.1:9092:9092 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://chat-lucy-kafka:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
-e KAFKA_ZOOKEEPER_CONNECT=chat-lucy-zookeeper:2181 \
-e KAFKA_CREATE_TOPICS="chats:1:1" \
-e KAFKA_CONNECTIONS_MAX_IDLE_MS=300000 \
-e KAFKA_BROKER_ID=1 \
wurstmeister/kafka
- name: Deploy Kafdrop
run: |
docker rm -f chat-lucy-kafdrop || true
docker run -d \
--name chat-lucy-kafdrop \
--network chat-lucy-network \
--restart unless-stopped \
-p 127.0.0.1:9000:9000 \
-e KAFKA_BROKERCONNECT=chat-lucy-kafka:9092 \
-e JVM_OPTS="-Xms32M -Xmx64M" \
-e SERVER_SERVLET_CONTEXTPATH="/" \
-e BASIC_AUTH_USER="${{secrets.KAFDROP_USER}}" \
-e BASIC_AUTH_PASSWORD="${{secrets.KAFDROP_PASSWORD}}" \
obsidiandynamics/kafdrop
- name: Remove old API container
run: docker rm -f chat-lucy-node-api || true

- name: Run docker container chat-lucy-node-api
run: |
docker run -d -p 3333:3333 \
-e NEXT_PUBLIC_APP_URL="${{secrets.NEXT_PUBLIC_APP_URL}}" \
-e NEXT_PUBLIC_API_URL="${{secrets.NEXT_PUBLIC_API_URL}}" \
-e APP_URL="${{secrets.NEXT_PUBLIC_API_URL}}" \
-e API_URL="${{secrets.NEXT_PUBLIC_API_URL}}" \
-e DATABASE_URL="${{secrets.DATABASE_URL}}" \
-e REDIS_URL="${{secrets.REDIS_URL}}" \
-e REDIS_URL="redis://:${{secrets.REDIS_PASSWORD}}@chat-lucy-redis:6379" \
-e JWT_SECRET="${{secrets.JWT_SECRET}}" \
-e KAFKA_BROKER="${{secrets.KAFKA_BROKER}}" \
-e KAFKA_BROKER="chat-lucy-kafka:9092" \
-e KAFKA_USERNAME="${{secrets.KAFKA_USERNAME}}" \
-e KAFKA_PASSWORD="${{secrets.KAFKA_PASSWORD}}" \
-e KAFKA_TOPIC="${{secrets.KAFKA_TOPIC}}" \
--name chat-lucy-node-api \
${{secrets.DOCKER_USERNAME}}/chat_lucy_node_api:latest
-e KAFKA_TOPIC="chats" \
-e NODE_ENV="production" \
--network chat-lucy-network \
--restart unless-stopped \
--name chat-lucy-node-api-monorepo \
${{secrets.DOCKER_USERNAME}}/chat_lucy_api:${{ github.sha }}
92 changes: 29 additions & 63 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,76 +1,42 @@
FROM node:20-alpine AS builder
# Stage 1: Base image
FROM node:20-alpine AS base
WORKDIR /app
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN apk add --no-cache openssl
RUN corepack enable

# Define build argument
# Adicionando DATABASE_URL como argumento e variável de ambiente
ARG DATABASE_URL
ENV DATABASE_URL=${DATABASE_URL}

WORKDIR /app

RUN corepack enable && corepack prepare pnpm@latest --activate

# Copy root package.json and workspace files
COPY package.json pnpm-workspace.yaml pnpm-lock.yaml ./

# Copy API package.json
COPY apps/api/package.json ./apps/api/
# Stage 2: Development dependencies
FROM base AS dependencies
COPY apps/api/package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY packages ./packages
COPY config ./config

# Install dependencies
RUN pnpm install --frozen-lockfile

# Copy source code
COPY apps/api ./apps/api

# Build the application
WORKDIR /app/apps/api
COPY apps/api/prisma ./prisma
COPY apps/api/tsup.config.ts ./
RUN pnpm install
RUN pnpm prisma generate

# Stage 3: Build
FROM base AS builder
COPY --from=dependencies /app/node_modules ./node_modules
COPY --from=dependencies /app/node_modules/prisma ./node_modules/prisma
COPY . .
RUN pnpm build

# Generate Prisma Client in builder stage
RUN pnpm prisma generate --schema=./prisma/schema.prisma

FROM node:20-alpine

ARG DATABASE_URL
ENV DATABASE_URL=${DATABASE_URL}
ENV NODE_ENV=production

RUN addgroup -S appgroup && adduser -S appuser -G appgroup
RUN apk add --no-cache openssl

WORKDIR /app

RUN corepack enable && corepack prepare pnpm@latest --activate

# Copy necessary files
COPY --from=builder /app/package.json ./
COPY --from=builder /app/pnpm-workspace.yaml ./
COPY --from=builder /app/pnpm-lock.yaml ./
COPY --from=builder /app/apps/api/package.json ./apps/api/
COPY --from=builder /app/apps/api/dist ./apps/api/dist
COPY --from=builder /app/packages ./packages
COPY --from=builder /app/config ./config
# Stage 4: Production
FROM base AS runner
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/apps/api/node_modules ./apps/api/node_modules
COPY --from=builder /app/apps/api/prisma ./apps/api/prisma

# Important: Copy the generated Prisma Client
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma

# Install production dependencies
RUN pnpm install --prod --frozen-lockfile

# Set correct permissions
RUN chown -R appuser:appgroup /app

USER appuser

WORKDIR /app/apps/api
COPY apps/api/package.json ./
COPY apps/api/prisma ./prisma/

# Generate Prisma Client again in production stage
RUN pnpm prisma generate --schema=./prisma/schema.prisma
# Gera o Prisma Client novamente no ambiente final
RUN npx prisma generate && npx prisma db push

EXPOSE 3333

CMD ["pnpm", "start"]
CMD ["pnpm", "start"]
8 changes: 4 additions & 4 deletions apps/api/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model User {
id String @id @default(uuid())
name String
Expand Down

0 comments on commit d0eb45a

Please sign in to comment.