CI (Continuous Integration)
: ๊ฐ๋ฐ ๊ณผ์ ์์ ์ฝ๋ ์ถฉ๋์ ์๋ฐฉํ๊ธฐ ์ํด ์ฃผ๊ธฐ์ ์ผ๋ก ์ ์ฅ์๋ฅผ ์๋ ๋น๋ ๋ฐ ํ ์คํธCD (Continuous Delivery)
: ์ ์ฅ์์ ์ฝ๋ ๋ณ๊ฒฝ ์ ์๋์ผ๋ก ์๋ฒ์ ๋ฐฐํฌ
ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก์ scripts/deploy.sh ํ์ผ์ ์์ฑํ๋ค.
์ด ์คํฌ๋ฆฝํธ๋ GitHub Actions
๋ก main ๋ธ๋์น ๋ณ๊ฒฝ ์ ์ต์ ์ฝ๋๋ฅผ ์๋์ผ๋ก ์๋ฒ์ ๋ฐฐํฌ, ๋น๋, ์คํํ๋ค.
scripts/deploy.sh
#!/bin/bash
npm -g install yarn
source ~/.bash_profile
cd ~/git/react-api-cicd/
git pull origin main
# front-end
cd frontend/
yarn install
yarn build
cp -rf dist/* ../backend/public
# back-end
cd ../backend/
yarn install
# pm2
pm2 stop react-api-cicd
pm2 start bin/www --name react-api-cicd --update-env
sleep 2
pm2 list
์คํ ๊ถํ ๋ถ์ฌ
chmod ./scripts/deploy.sh
์ฝ๋๋ฅผ Github์ Pushํ ํ, SSH๋ก ์ ์ํ ์๋ฒ์์ git pull origin main ๋ช ๋ น์ด๋ฅผ ์คํํ์ฌ ๋ณ๊ฒฝ ์ฌํญ์ ๋ฐ์ํ๋ค.
CI/CD
๋๊ตฌ๋ก Jenkins๊ฐ ๊ฐ์ฅ ์ ๋ช
ํ์ง๋ง, ์ด๋ฒ์๋ GitHub Actions๋ฅผ ํตํด์ ๊ตฌ์ฑํด ๋ณธ๋ค.
ํ๋ก์ ํธ ๋ฃจํธ ๊ฒฝ๋ก์ .github/workflows/deploy-main.yml ํ์ผ์ ๋ง๋ค๊ณ ์๋ ๋ด์ฉ์ ์ถ๊ฐํ๋ค.
name: Deploy on push to main
on:
push:
branches: [main]
jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
# ๋ ํฌ์งํ ๋ฆฌ ์ฒดํฌ์์
- name: Checkout repository
uses: actions/checkout@v2
# SSH๋ฅผ ์ฌ์ฉํ์ฌ ์คํฌ๋ฆฝํธ ์คํ
- name: executing remote ssh commands using key
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.KEY }}
port: ${{ secrets.PORT }}
script: |
./git/react-api-cicd/scripts/deploy.sh
GitHub์์ ํ๋ก์ ํธ ๋ ํฌ์งํ ๋ฆฌ์ Settings > Secrets > Actions๋ก ์ด๋ํ์ฌ ๋ค์ ํญ๋ชฉ์ ์ถ๊ฐํ๋ค.
HOST
: ์๋ฒ ๋๋ฉ์ธUSERNAME
: ec2-userKEY
: react-api-cicd-kp.pem ํ์ผ ๋ด์ฉPORT
: 22