GitHub Actions๋ฅผ ์ด์ฉํ React ์ฑ CI/CD ๋ฐฐํฌ ์๋ํ
์ด ํฌ์คํธ์์๋ GitHub Actions๋ฅผ ์ฌ์ฉํ์ฌ React ์ฑ์ GitHub Pages์ ์๋ ๋ฐฐํฌํ๋ CI/CD ํ์ดํ๋ผ์ธ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃน๋๋ค. GitHub Actions๋ ์ํํธ์จ์ด ๊ฐ๋ฐ ์ํฌํ๋ก์ฐ๋ฅผ ์๋ํํ ์ ์๋ ๋๊ตฌ๋ก, ๋ค์ํ ์์
์ ์๋์ผ๋ก ์คํํ ์ ์์ต๋๋ค. React ์ฑ์ GitHub Pages์ ๋ฐฐํฌํ๋ ํ๋ก์ธ์ค๋ฅผ ์๋ํํ์ฌ, ๋งค๋ฒ ์๋์ผ๋ก ๋ฐฐํฌํ ํ์ ์์ด, main
๋ธ๋์น์ ๋ณ๊ฒฝ ์ฌํญ์ด ํธ์๋ ๋๋ง๋ค ์๋์ผ๋ก ๋น๋๋๊ณ ๋ฐฐํฌ๋๋๋ก ์ค์ ํ ์ ์์ต๋๋ค.
1. GitHub Actions ์ํฌํ๋ก์ฐ ํ์ผ ์ค์
GitHub Actions ์ํฌํ๋ก์ฐ๋ .github/workflows
ํด๋ ๋ด์ YAML ํ์ผ๋ก ์ ์๋ฉ๋๋ค. ์๋๋ Deploy React App to GitHub Pages
๋ผ๋ ์ด๋ฆ์ ์ํฌํ๋ก์ฐ ํ์ผ์ ์์์
๋๋ค.
name: Deploy React App to GitHub Pages
on:
push:
branches:
- main # main ๋ธ๋์น์ ํธ์๋ ๋๋ง๋ค ๋ฐฐํฌ ์คํ
jobs:
deploy:
runs-on: ubuntu-latest # ์คํ ํ๊ฒฝ, ์ต์ ์ฐ๋ถํฌ ํ๊ฒฝ์์ ์คํ
steps:
# 1. ๋ฆฌํฌ์งํ ๋ฆฌ ์ฒดํฌ์์
- name: Checkout repository
uses: actions/checkout@v2
# 2. Node.js ์ค์
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "14"
# 3. ํ๋ก์ ํธ ์์กด์ฑ ์ค์น
- name: Install dependencies
run: npm install
# 4. ์ฑ ๋น๋
- name: Build the app
run: npm run build
# 5. Git ์ค์ (์ด๋ฉ์ผ๊ณผ ์ฌ์ฉ์ ์ด๋ฆ ์ค์ )
- name: Set Git user
run: |
git config --global user.email "leesengjie@naver.com"
git config --global user.name "SeungjaeLee00"
git config --global url."https://${GH_TOKEN}@github.com/".insteadOf "https://github.com/"
# 6. GitHub Pages์ ๋ฐฐํฌ
- name: Deploy to GitHub Pages
run: |
git remote set-url origin https://$GH_TOKEN@github.com/SeungjaeLee00/Netflix_cloneCoding.git
npm run deploy
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
2. ๊ฐ ๋จ๊ณ ์ค๋ช
name
:
- ์ํฌํ๋ก์ฐ ์ด๋ฆ: GitHub Pages์ React ์ฑ์ ๋ฐฐํฌํ๋ ์์ ์ ๋ํ ์ด๋ฆ์ ๋๋ค.
on
:
- ํธ์ ์ด๋ฒคํธ:
main
๋ธ๋์น์ ํธ์๋ ๋๋ง๋ค ์ด ์ํฌํ๋ก์ฐ๊ฐ ์คํ๋ฉ๋๋ค.
jobs
:
- deploy: ๋ฐฐํฌ ์์ ์ ์ ์ํ๋ Job์ ๋๋ค.
steps
:
- ๋ฆฌํฌ์งํ ๋ฆฌ ์ฒดํฌ์์: GitHub ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ์ฒดํฌ์์ํ์ฌ, ์ํฌํ๋ก์ฐ์์ ์ฌ์ฉํ ์ ์๊ฒ ํฉ๋๋ค.
- name: Checkout repository
uses: actions/checkout@v2
- Node.js ์ค์ : ํ๋ก์ ํธ์์ ์ฌ์ฉํ๊ณ ์๋ Node.js ๋ฒ์ ์ ์ค์ ํฉ๋๋ค. ์ฌ๊ธฐ์๋
14
๋ฒ์ ์ ์ฌ์ฉํฉ๋๋ค.
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "14"
- ํ๋ก์ ํธ ์์กด์ฑ ์ค์น:
npm install
์ ์คํํ์ฌ ํ๋ก์ ํธ ์์กด์ฑ๋ค์ ์ค์นํฉ๋๋ค.
- name: Install dependencies
run: npm install
- ์ฑ ๋น๋:
npm run build
๋ช ๋ น์ด๋ฅผ ์คํํ์ฌ, ๋ฐฐํฌ์ฉ์ผ๋ก ์ต์ ํ๋ React ์ฑ์ ๋น๋ํฉ๋๋ค.
- name: Build the app
run: npm run build
- Git ์ค์ : ๋ฐฐํฌ ์ ์ฌ์ฉํ Git ์ด๋ฉ์ผ๊ณผ ์ฌ์ฉ์ ์ด๋ฆ์ ์ค์ ํฉ๋๋ค. ๋ํ,
GH_TOKEN
์ ์ฌ์ฉํ์ฌ GitHub ๋ฆฌํฌ์งํ ๋ฆฌ์ ํธ์ํ ์ ์๋๋ก ์ค์ ํฉ๋๋ค.
- name: Set Git user
run: |
git config --global user.email "leesengjie@naver.com"
git config --global user.name "SeungjaeLee00"
git config --global url."https://${GH_TOKEN}@github.com/".insteadOf "https://github.com/"
- GitHub Pages์ ๋ฐฐํฌ:
npm run deploy
๋ช ๋ น์ด๋ฅผ ์คํํ์ฌ, GitHub Pages์ ๋ฐฐํฌํฉ๋๋ค. ์ด๋GH_TOKEN
์ GitHub์ Personal Access Token(PAT)์ผ๋ก ์ค์ ๋์ด์ผ ํ๋ฉฐ, ์ด ํ ํฐ์ GitHub์ Secrets์ ์ ์ฅํด์ผ ํฉ๋๋ค.
- name: Deploy to GitHub Pages
run: |
git remote set-url origin https://$GH_TOKEN@github.com/SeungjaeLee00/Netflix_cloneCoding.git
npm run deploy
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
3. GitHub Secrets ์ค์
- GitHub Token:
GH_TOKEN
์ GitHub ๋ฆฌํฌ์งํ ๋ฆฌ์ ํธ์ํ ์ ์๋ ๊ถํ์ ๊ฐ์ง Personal Access Token์ ๋๋ค. ์ด ํ ํฐ์ GitHub์ Secrets์ ์ถ๊ฐํด์ผ ํฉ๋๋ค.- GitHub ๋ฆฌํฌ์งํ ๋ฆฌ์ Settings > Secrets์์ ์๋ก์ด secret์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
- Secret Name:
GH_TOKEN
- Secret Value: GitHub์์ ์์ฑํ Personal Access Token
4. npm run deploy
๋ช
๋ น์ด ์ค์
๋ฐฐํฌ๋ฅผ ์ํด npm run deploy
๋ช
๋ น์ด๋ฅผ ์คํํด์ผ ํฉ๋๋ค. package.json
ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ ์คํฌ๋ฆฝํธ๊ฐ ์์ด์ผ ํฉ๋๋ค:
"scripts": {
"deploy": "gh-pages -d build"
}
gh-pages
ํจํค์ง๋ React ์ฑ์ GitHub Pages์ ์ฝ๊ฒ ๋ฐฐํฌํ ์ ์๋๋ก ๋์์ค๋๋ค. ์ด ํจํค์ง๋ฅผ ์ค์นํ๋ ค๋ฉด,npm install --save gh-pages
๋ช ๋ น์ด๋ฅผ ์คํํด์ผ ํฉ๋๋ค.
5. ๋ฐฐํฌ ์๋ํ
์ด์ main
๋ธ๋์น์ ์ฝ๋๊ฐ ํธ์๋ ๋๋ง๋ค GitHub Actions๊ฐ ์๋์ผ๋ก ์คํ๋๋ฉฐ, ๋น๋๋ React ์ฑ์ GitHub Pages์ ๋ฐฐํฌํฉ๋๋ค. ์ด๋ ๊ฒ ์ค์ ํ๋ฉด, ๋งค๋ฒ ์๋์ผ๋ก ๋ฐฐํฌํ ํ์ ์์ด ์๋์ผ๋ก ๋ฐฐํฌ๊ฐ ์ด๋ฃจ์ด์ง๋๋ค.
๐ ๊ฒฐ๊ณผ
๋ทํ๋ฆญ์ค ํด๋ก ์น์ฌ์ดํธ ์ฃผ์: https://seungjaelee00.github.io/Netflix_cloneCoding/
๐ก ์ ๋ฆฌ
GitHub Actions๋ฅผ ์ด์ฉํ CI/CD ์ค์ ์ ๊ฐ๋ฐ์์๊ฒ ๋งค์ฐ ์ ์ฉํ๋ฉฐ, ๋ฐฐํฌ ๊ณผ์ ์์ ๋ฐ์ํ ์ ์๋ ์ค์๋ฅผ ์ค์ด๊ณ , ๋์ฑ ํจ์จ์ ์ธ ๊ฐ๋ฐ ํ๊ฒฝ์ ์ ๊ณตํฉ๋๋ค. ์ด๋ฒ ํฌ์คํธ์์๋ GitHub Pages์ React ์ฑ์ ์๋์ผ๋ก ๋ฐฐํฌํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ์ผ๋ฉฐ, ์ด ์ค์ ์ ํตํด ๊ฐํธํ๊ฒ ๋ฐฐํฌ ์์ ์ ์๋ํํ ์ ์์ต๋๋ค.