diff --git a/.github/workflows/cd-aws.yml b/.github/workflows/cd-aws.yml index 3e56e601..b4ca8509 100644 --- a/.github/workflows/cd-aws.yml +++ b/.github/workflows/cd-aws.yml @@ -12,16 +12,6 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v3 - - - name: Set up Node.js environment - uses: actions/setup-node@v2 - with: - node-version: '18' - - - run: | - jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' <<< "$SECRETS_CONTEXT" > .env.local - env: - SECRETS_CONTEXT: ${{ toJson(secrets) }} - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v3 @@ -34,17 +24,24 @@ jobs: id: login-ecr uses: aws-actions/amazon-ecr-login@v2 + - name: Create .env file + run: | + touch .env.local + echo "${{ secrets.ENV_PRODUCTION }}" > .env.production + - name: Build, tag, and push docker image to Amazon ECR env: REGISTRY: ${{ steps.login-ecr.outputs.registry }} REPOSITORY: washfit-client IMAGE_TAG: latest run: | - docker build -t $REGISTRY/$REPOSITORY:$IMAGE_TAG . + docker build --no-cache -t $REGISTRY/$REPOSITORY:$IMAGE_TAG . docker push $REGISTRY/$REPOSITORY:$IMAGE_TAG - name: Application Run uses: appleboy/ssh-action@v1.0.3 + env: + PORT: 8000 with: host: ${{ secrets.AWS_EC2_HOST }} username: ${{ secrets.AWS_USERNAME }} diff --git a/Dockerfile b/Dockerfile index d90312e8..25e83813 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,50 +1,56 @@ -FROM node:18-alpine AS base - -FROM base AS deps +# Multi-stage build +# 1단계: 환경 설정 및 dependancy 설치 +FROM node:18-alpine AS deps RUN apk add --no-cache libc6-compat + +# 명령어를 실행할 디렉터리 지정 WORKDIR /app -COPY package.json yarn.lock* ./ -RUN \ - if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ - else echo "Lockfile not found." && exit 1; \ - fi +# Dependancy install을 위해 package.json, package-lock.json, yarn.lock 복사 +COPY package.json yarn.lock ./ + +# Dependancy 설치 (새로운 lock 파일 수정 또는 생성 방지) +RUN yarn --frozen-lockfile -FROM base AS builder +########################################################### +# 2단계: next.js 빌드 단계 +FROM node:18-alpine AS builder + +# 명령어를 실행할 디렉터리 지정 WORKDIR /app +# node_modules 등의 dependancy를 복사함. COPY --from=deps /app/node_modules ./node_modules COPY . . -RUN \ - if [ -f yarn.lock ]; then yarn run build; \ - else echo "Lockfile not found." && exit 1; \ - fi +RUN yarn build -FROM base AS runner -WORKDIR /app +########################################################### -ENV NODE_ENV production +# 3단계: next.js 실행 단계 +FROM node:18-alpine AS runner +# 명령어를 실행할 디렉터리 지정 +WORKDIR /app + +# container 환경에 시스템 사용자를 추가함 RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs +# next.config.js에서 output을 standalone으로 설정하면 +# 빌드에 필요한 최소한의 파일만 ./next/standalone로 출력이 된다. +# standalone 결과물에는 public 폴더와 static 폴더 내용은 포함되지 않으므로, 따로 복사를 해준다. COPY --from=builder /app/public ./public - -RUN mkdir .next -RUN chown nextjs:nodejs .next - COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static -USER nextjs - +# 컨테이너의 수신 대기 포트를 3000으로 설정 EXPOSE 3000 -ENV PORT 3000 - -ENV HOSTNAME "0.0.0.0" +# node로 애플리케이션 실행 +CMD ["node", "server.js"] -CMD ["node", "server.js"] \ No newline at end of file +# standalone으로 나온 결과값은 node 자체적으로만 실행 가능 +# CMD ["npm", "start"] \ No newline at end of file