diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e8f8b65 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,7 @@ +node_modules +.next +.git +.github +*.md +.env* +.DS_Store \ No newline at end of file diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml new file mode 100644 index 0000000..7dfb766 --- /dev/null +++ b/.github/workflows/deploy.yaml @@ -0,0 +1,69 @@ +name: CI/CD Bennu Code + +on: + push: + branches: + - feat/ci-cd + +env: + GIT_USERNAME: ${{ github.actor }} + GIT_PASSWORD: ${{ secrets.GITHUB_TOKEN }} + IMAGE_TAG: latest + DOCKERFILE: ${{ secrets.DOCKERFILE || './Dockerfile' }} + IMAGE_NAME: ghcr.io/${{ github.repository }} + IMAGE_REGISTRY_URL: ${{ secrets.IMAGE_REGISTRY_URL || 'https://ghcr.io' }} + IMAGE_REGISTRY_USER: ${{ secrets.IMAGE_REGISTRY_USER || github.actor }} + IMAGE_REGISTRY_PASSWORD: ${{ secrets.IMAGE_REGISTRY_PASSWORD || secrets.GITHUB_TOKEN }} + +jobs: + build-and-push: + name: Build & Push Docker Image + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout código + uses: actions/checkout@v4.2.2 + + - name: Login a GHCR + uses: docker/login-action@v3.4.0 + with: + registry: ${{ env.IMAGE_REGISTRY_URL }} + username: ${{ env.IMAGE_REGISTRY_USER }} + password: ${{ env.IMAGE_REGISTRY_PASSWORD }} + + - name: Build & Push imagen + uses: docker/build-push-action@v6 + with: + context: . + file: ${{ env.DOCKERFILE }} + push: true + tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} + + deploy: + name: Deploy en EC2 + runs-on: ubuntu-latest + needs: build-and-push + + steps: + - name: Deploy via SSH + uses: appleboy/ssh-action@v1 + with: + host: ${{ secrets.EC2_HOST }} + username: ${{ secrets.EC2_USER }} + key: ${{ secrets.EC2_SSH_KEY }} + script: | + echo "${{ secrets.GITHUB_TOKEN }}" | docker login https://ghcr.io -u ${{ github.actor }} --password-stdin + + docker pull ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} + + docker stop bennu-code || true + docker rm bennu-code || true + + docker run -d \ + --name bennu-code \ + --restart always \ + -p 3000:3000 \ + ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fd70a4d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +FROM node:24-alpine3.22 AS deps + +RUN apk add --no-cache libc6-compat +WORKDIR /app + +RUN npm install -g pnpm + +COPY package.json pnpm-lock.yaml ./ +RUN pnpm install --frozen-lockfile + + +FROM node:24-alpine3.22 AS builder +WORKDIR /app + +RUN npm install -g pnpm + +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +RUN pnpm build +RUN cp -r .next/static .next/standalone/.next/static + + +FROM node:24-alpine3.22 AS runner +WORKDIR /app + +ENV NODE_ENV=production +ENV PORT=3000 + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 + +CMD ["node", "server.js"] \ No newline at end of file diff --git a/next.config.ts b/next.config.ts index 24e7a37..8919db6 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,7 +1,7 @@ import type { NextConfig } from "next" const nextConfig: NextConfig = { - output: "export", + output: "standalone", } export default nextConfig