프론트엔드 개발에 도움을 주고자 서버 repo의 develop
브랜치를 EC2 인스턴스에 배포할 때 사용했던 Github Actions 스크립트이다.
name: Deploy dev server
on:
push:
branches: [develop]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Build Docker image
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_REPO }} .
docker tag ${{ secrets.DOCKER_REPO }} ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:dev
docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:dev
- name: Run Docker in EC2
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:dev
docker tag ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:dev ${{ secrets.DOCKER_REPO }}
docker stop server
docker run -d --rm --name server \
-e NODE_ENV=dev \
-e 기타=환경변수 \
-p 3000:3000 ${{ secrets.DOCKER_REPO }}
NestJS 앱을 도커 이미지로 컨테이너화하는 Dockerfile 스크립트이다.
###################
# BUILD
###################
# Base image
FROM node:20.4.0-alpine3.18 As build
# Create app directory
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./
# Install app dependencies
RUN npm ci
# Bundle app source
COPY --chown=node:node . .
# Generate Prisma client
RUN npx prisma generate
# Creates a "dist" folder with the production build
RUN npm run build
# Running `npm ci` removes the existing node_modules directory and passing in --only=production ensures that only the production dependencies are installed.
RUN npm ci --only=production && npm cache clean --force
###################
# PRODUCTION
###################
FROM node:20.4.0-alpine3.18 As production
# Copy the bundled code from the build stage to the production image
COPY --chown=node:node --from=build /usr/src/app/node_modules ./node_modules
COPY --chown=node:node --from=build /usr/src/app/dist ./dist
# Use the node user from the image (instead of the root user)
USER node
# Start the server using the production build
CMD [ "node", "dist/main.js" ]