Skip to content

Commit

Permalink
✨ feat: add Docker Next.js server setup and CI/CD pipeline
Browse files Browse the repository at this point in the history
- Dockerfile: Add Dockerfile with standalone output for Next.js server.
- next.config.js: Enable standalone output for efficient Docker builds.
- package.json: Update `postinstall` script to conditionally install Husky.
- ci.yml: Add new CI/CD pipeline for building and deploying to Amazon ECR.

Related issue: YJU-OKURA#36
  • Loading branch information
yuminn-k committed May 8, 2024
1 parent a1d62dd commit f2d749a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Deploy to ECR

on:
push:
branches: [main]

jobs:
build:
name: Build Image
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v2

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY}}
aws-region: ap-northeast-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
run: |
docker build -t minori-next .
docker tag minori-next:latest 233629834663.dkr.ecr.us-east-1.amazonaws.com/minori-next:latest
docker push 233629834663.dkr.ecr.us-east-1.amazonaws.com/minori-next:latest
45 changes: 45 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Base image
FROM node:20-alpine AS base

# Dependencies stage
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /src/app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Builder stage
FROM base AS builder
WORKDIR /src/app
COPY --from=deps /src/app/node_modules ./node_modules
COPY . .
RUN yarn build

# Final production image
FROM base AS runner
WORKDIR /src/app

ENV NODE_ENV=production

RUN addgroup --system --gid 1001 nodejs \
&& adduser --system --uid 1001 nextjs

# Copy Next.js build output
COPY --from=builder /src/app/public ./public
COPY --from=builder /src/app/.next/standalone ./
COPY --from=builder /src/app/.next/static ./.next/static

# Copy additional necessary files
COPY --from=builder /src/app/package.json ./package.json
COPY --from=builder /src/app/yarn.lock ./yarn.lock

# Install production-only dependencies while ignoring optional ones
RUN yarn install --frozen-lockfile --production --ignore-optional

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ module.exports = {
],
},
reactStrictMode: false,
output: 'standalone',
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next lint",
"test": "jest",
"test:watch": "jest --watch",
"postinstall": "husky install"
"postinstall": "node -e \"if (process.env.NODE_ENV !== 'production') require('husky').install()\""
},
"husky": {
"hooks": {
Expand Down

0 comments on commit f2d749a

Please sign in to comment.