Skip to content

๐Ÿชต 3. ์ธํ”„๋ผ ์‹ค์Šต(4) : ๋„์ปค๋กœ ๋ฐฐํฌํ•˜๊ธฐ CI CD

ssum1ra edited this page Dec 5, 2024 · 1 revision

์‚ฌ์ „ ์ž‘์—…

PM2 ํ”„๋กœ์„ธ์Šค ์ค‘์ง€

pm2 delete whiteboard-server

์ธ์Šคํ„ด์Šค์— Docker engine ์„ค์น˜

  • Docker engine
    • ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ, ์‹คํ–‰, ๊ด€๋ฆฌ์™€ ๊ฐ™์€ Docker์˜ ์ฃผ์š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ
  • ๊ณต์‹ ๋ฌธ์„œ ์ฐธ๊ณ 
  • Docker Documentation: Ubuntu

Docker file ์ƒ์„ฑ

  • Docker ์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์„ค๊ณ„๋„
# ๊ธฐ๋ณธ ์ด๋ฏธ์ง€๋กœ Node.js 20 Alpine ๋ฒ„์ „ ์‚ฌ์šฉ
# Alpine์€ ๋งค์šฐ ๊ฐ€๋ฒผ์šด Linux ๋ฐฐํฌํŒ
FROM node:20-alpine

# ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์˜ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ์„ค์ •
WORKDIR /app

# package.json ํŒŒ์ผ๋“ค๋งŒ ๋จผ์ € ๋ณต์‚ฌ
# ์บ์‹œ ํ™œ์šฉ์„ ์œ„ํ•ด ์†Œ์Šค์ฝ”๋“œ๋ณด๋‹ค ๋จผ์ € ๋ณต์‚ฌ
COPY package*.json ./
# npm ci๋Š” package-lock.json ๊ธฐ๋ฐ˜์œผ๋กœ ์ •ํ™•ํ•œ ๋ฒ„์ „ ์„ค์น˜
RUN npm ci

# ๋‚˜๋จธ์ง€ ์†Œ์Šค ํŒŒ์ผ ๋ณต์‚ฌ
COPY . .
# NestJS ์•ฑ ๋นŒ๋“œ
RUN npm run build

# 3000๋ฒˆ ํฌํŠธ ์‚ฌ์šฉ ๋ช…์‹œ
EXPOSE 3000

# ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰ ์‹œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด
CMD ["npm", "run", "start:prod"]

DockerHub

  • ํด๋ผ์šฐ๋“œ ๊ธฐ๋ฐ˜์˜ Docker ์ด๋ฏธ์ง€ ์ €์žฅ์†Œ
  • NCP์˜ Container Repository์™€ ์œ ์‚ฌ

DockerHub Access token ์ƒ์„ฑ

Repository secrets ์ถ”๊ฐ€

image (21)

๋™์ž‘ ๊ณผ์ •

GitHub Actions์—์„œ

  • server ํด๋”์˜ Dockerfile ๊ธฐ๋ฐ˜์œผ๋กœ ์ด๋ฏธ์ง€ ๋นŒ๋“œ
  • ๋นŒ๋“œ๋œ ์ด๋ฏธ์ง€๋ฅผ Docker Hub์— push

GitHub Actions๊ฐ€ ์„œ๋ฒ„์— SSH ์ ‘์†ํ•ด์„œ

  • Docker Hub ๋กœ๊ทธ์ธ
  • ์ƒˆ ์ด๋ฏธ์ง€ pull
  • ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ ์ •์ง€/์ œ๊ฑฐ
  • ์ƒˆ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰

์ˆ˜์ •๋œ .github/workflows/ci-cd.yml

name: Whiteboard CI/CD

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      # ์บ์‹œ ์„ค์ •(npm ๊ธฐ์ค€)
      - name: Cache dependencies
        uses: actions/cache@v3
        with:
          path: |
            **/node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      # ํ”„๋ก ํŠธ์—”๋“œ ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ
      - name: Install & Build Frontend
        working-directory: ./client
        env:
          VITE_SOCKET_URL: ${{ secrets.VITE_SOCKET_URL }}
        run: |
          npm ci
          npm run build

      # Object Storage ์—…๋กœ๋“œ
      - name: Deploy to Object Storage
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.NCP_ACCESS_KEY }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.NCP_SECRET_KEY }}
        run: |
          # S3 ์—…๋กœ๋“œ
          aws s3 sync ./client/dist s3://test-web30 \
            --endpoint-url https://kr.object.ncloudstorage.com \
            --region kr-standard
      # ๋„์ปค ๋นŒ๋“œ & ๋ฐฐํฌ
      - name: Docker Setup
        uses: docker/setup-buildx-action@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v5
        with:
          context: ./server
          push: true
          tags: ${{ secrets.DOCKERHUB_USERNAME }}/whiteboard:latest

      # ์„œ๋ฒ„ ๋ฐฐํฌ
      - name: Deploy to Server
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ncloud
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            # Docker ๋กœ๊ทธ์ธ
            echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin

            # ์ƒˆ ์ด๋ฏธ์ง€ ํ’€
            docker pull ${{ secrets.DOCKERHUB_USERNAME }}/whiteboard:latest

            # ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ ์ •์ง€ & ์ œ๊ฑฐ
            docker stop whiteboard || true
            docker rm whiteboard || true

            # ์ƒˆ ์ปจํ…Œ์ด๋„ˆ ์‹คํ–‰
            docker run -d --name whiteboard -p 3000:3000 --restart unless-stopped ${{ secrets.DOCKERHUB_USERNAME }}/whiteboard:latest

์ž๋™ ๋นŒ๋“œ & ๋ฐฐํฌ ์„ฑ๊ณต

image (22)

์ฐธ๊ณ 

  • Alpine ์ด๋ฏธ์ง€์™€ mediasoup ์ถฉ๋Œ
FROM node:20-alpine   # ์ตœ์†Œํ™”๋œ ๋ฆฌ๋ˆ…์Šค๋กœ mediasoup ๊ฐ™์€ ๋„ค์ดํ‹ฐ๋ธŒ ๋ชจ๋“ˆ ๋นŒ๋“œ ์–ด๋ ค์›€

# 1. ๊ธฐ๋ณธ node ์ด๋ฏธ์ง€ ์‚ฌ์šฉ
FROM node:20

# 2. Alpine์— ๋นŒ๋“œ ๋„๊ตฌ ์ถ”๊ฐ€
FROM node:20-alpine
RUN apk add --no-cache python3 make g++ gcc
  • Docker ๊ถŒํ•œ ๋ฌธ์ œ
# ncloud ์‚ฌ์šฉ์ž๋ฅผ docker ๊ทธ๋ฃน์— ์ถ”๊ฐ€
sudo usermod -aG docker ncloud

# ๋ณ€๊ฒฝ์‚ฌํ•ญ ์ ์šฉ์„ ์œ„ํ•ด ์žฌ๋กœ๊ทธ์ธ
exit

docker ps  # ๊ถŒํ•œ ๋ฌธ์ œ ์—†์ด ์‹คํ–‰๋˜์–ด์•ผ ํ•จ

๐Ÿ˜Ž ์›จ๋ฒ ๋ฒ ๋ฒ ๋ฒฑ

๐Ÿ‘ฎ๐Ÿป ํŒ€ ๊ทœ์น™

๐Ÿ’ป ํ”„๋กœ์ ํŠธ

๐Ÿชต ์›จ๋ฒ ๋ฒฑ ๊ธฐ์ˆ ๋กœ๊ทธ

๐Ÿช„ ๋ฐ๋ชจ ๊ณต์œ 

๐Ÿ”„ ์Šคํ”„๋ฆฐํŠธ ๊ธฐ๋ก

๐Ÿ“— ํšŒ์˜๋ก

Clone this wiki locally