Skip to content

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

ssum1ra edited this page Dec 5, 2024 · 1 revision

.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: Deploy Backend
        uses: appleboy/[email protected]
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ncloud
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd ~/whiteboard/server
            git pull
            npm ci
            npm run build

            # ์„œ๋ฒ„์— ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํŒŒ์ผ ์ƒ์„ฑ
            cat << EOF > .env
            # DATABASE_URL=${{ secrets.DATABASE_URL }}
            EOF

            # PM2 ์žฌ์‹œ์ž‘
            pm2 restart whiteboard-server || pm2 start dist/main.js --name "whiteboard-server"
  • npm ๊ธฐ์ค€

Repository secrets ์ถ”๊ฐ€

image (19)

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

image (20)

  • Actions ํƒญ์—์„œ ๋กœ๊ทธ ํ™•์ธ ๊ฐ€๋Šฅ

์ฐธ๊ณ 

AWS S3 CLI๋กœ NCP Object Storage ์ œ์–ด

  • NCP Object Storage๋Š” AWS S3 CLI์™€ ํ˜ธํ™˜๋˜๋„๋ก ์„ค๊ณ„๋จ.
  • Object Storage CLI

AWS CLI ์ค‘๋ณต ์„ค์น˜ ์ด์Šˆ

- name: Deploy to Object Storage
  env:
    AWS_ACCESS_KEY_ID: ${{ secrets.NCP_ACCESS_KEY }}
    AWS_SECRET_ACCESS_KEY: ${{ secrets.NCP_SECRET_KEY }}
  run: |
    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    unzip awscliv2.zip
    sudo ./aws/install # ์ด๋ฏธ ์„ค์น˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ ์‚ญ์ œํ•ด์•ผ ํ•จ.

    aws s3 sync ./client/dist s3://test-web30 \
      --endpoint-url https://kr.object.ncloudstorage.com \
      --region kr-standard

ubuntu-latest์— ๊ธฐ๋ณธ ์„ค์น˜๋œ ๊ฒƒ๋“ค

  • โœ… AWS CLI
  • โœ… curl
  • โœ… git
  • โœ… npm
  • โœ… python

๋ฐฑ์—”๋“œ ์ฝ”๋“œ๋งŒ .env ํŒŒ์ผ ๋”ฐ๋กœ ์ƒ์„ฑ ํ•ด์ค˜์•ผํ•˜๋Š” ์ด์œ 

  • ํ”„๋ก ํŠธ์—”๋“œ (env: ์‚ฌ์šฉ ๊ฐ€๋Šฅ)
    • ๋นŒ๋“œ ์‹œ์ ์—๋งŒ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํ•„์š”
    • ๋นŒ๋“œ ํ›„์—๋Š” ์ •์  ํŒŒ์ผ๋กœ ๋ณ€ํ™˜
  • ๋ฐฑ์—”๋“œ (ํŒŒ์ผ ํ•„์š”)
    • ๋Ÿฐํƒ€์ž„์— ์ง€์†์ ์œผ๋กœ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ฐธ์กฐ
    • ์„œ๋ฒ„ ๋™์ž‘ ์ค‘์—๋„ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ํ•„์š”
  • ์ฆ‰, GitHub Actions ์‹คํ–‰์ด ์ข…๋ฃŒ๋˜๋ฉด ํ™˜๊ฒฝ ๋ณ€์ˆ˜๊ฐ€ ์‚ญ์ œ๋˜๋ฏ€๋กœ, ์„œ๋ฒ„์—์„œ๋Š” ๋…๋ฆฝ์ ์ธ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํŒŒ์ผ์ด ํ•„์š”ํ•˜๋‹ค.

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

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

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

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

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

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

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

Clone this wiki locally