suvera-dev πŸ₯¦

AWS λ°°ν¬μžλ™ν™” ꡬ좕 _ 6. React ν”„λ‘œμ νŠΈ 배포 λ³Έλ¬Έ

Infra/CI CD

AWS λ°°ν¬μžλ™ν™” ꡬ좕 _ 6. React ν”„λ‘œμ νŠΈ 배포

suvera 2023. 7. 14. 14:39

이전에 index.html νŒŒμΌν•˜λ‚˜λ‘œ 배포 ν…ŒμŠ€νŠΈλ₯Ό ν–ˆμ—ˆλŠ”λ°,

μ‹€μ œλ‘œ λ°°ν¬ν•΄μ•Όν•˜λŠ” ν”„λ‘œμ νŠΈλŠ” React ν”„λ‘œμ νŠΈλ‘œ ꡬ성할 것이기 λ•Œλ¬Έμ— React ν”„λ‘œμ νŠΈλ₯Ό λ§Œλ“€κ³  잘 λœ¨λŠ”μ§€ ν™•μΈν•΄λ³΄μž.

 

1. React ν”„λ‘œμ νŠΈ 생성

- λ¦¬μ•‘νŠΈ ν”„λ‘œμ νŠΈλ₯Ό μƒμ„±ν•˜λŠ” 방법은 κ°„λ‹¨ν•˜λ‹€.

- 이전에 μ§„ν–‰ν–ˆλ˜ ν΄λ”μ—μ„œ κ·ΈλŒ€λ‘œ React ν”„λ‘œμ νŠΈλ₯Ό λ§Œλ“€μ–΄μ£Όμ—ˆλ‹€.

  1. Node.js μ„€μΉ˜
  2. μž‘μ—…μš© 폴더 λ§Œλ“€κΈ°
  3. ν„°λ―Έλ„μ—μ„œ npx create-react-app ν”„λ‘œμ νŠΈλͺ…

μ΄λ ‡κ²Œ ν•˜λ©΄ μžλ™μ μœΌλ‘œ ν•„μš”ν•œ 폴더듀이 μ„ΈνŒ…λœλ‹€.

 

2. Docker 파일 μˆ˜μ •

μ•„λž˜μ— 각 μ½”λ“œ μ„€λͺ…을 μ μ–΄λ‘μ—ˆλ‹€.

# base image μ„€μ •(as build 둜 μ™„λ£Œλœ νŒŒμΌμ„ λ°‘μ—μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.)
FROM node:14-alpine as build

# μ»¨ν…Œμ΄λ„ˆ λ‚΄λΆ€ μž‘μ—… 디렉토리 μ„€μ •
WORKDIR /app

# app dependencies
# μ»¨ν…Œμ΄λ„ˆ λ‚΄λΆ€λ‘œ package.json νŒŒμΌλ“€μ„ 볡사
COPY package*.json ./

# package.json 및 package-lock.json νŒŒμΌμ— λͺ…μ‹œλœ μ˜μ‘΄μ„± νŒ¨ν‚€μ§€λ“€μ„ μ„€μΉ˜
RUN npm install

# 호슀트 λ¨Έμ‹ μ˜ ν˜„μž¬ 디렉토리 νŒŒμΌλ“€μ„ μ»¨ν…Œμ΄λ„ˆ λ‚΄λΆ€λ‘œ μ „λΆ€ 볡사
COPY . .

# npm build
RUN npm run build

# prod environment
FROM nginx:stable-alpine

# 이전 λΉŒλ“œ λ‹¨κ³„μ—μ„œ λΉŒλ“œν•œ 결과물을 /usr/share/nginx/html 으둜 λ³΅μ‚¬ν•œλ‹€.
COPY --from=build /app/build /usr/share/nginx/html

# κΈ°λ³Έ nginx μ„€μ • νŒŒμΌμ„ μ‚­μ œν•œλ‹€. (custom μ„€μ •κ³Ό 좩돌 방지)
RUN rm /etc/nginx/conf.d/default.conf

# custom μ„€μ •νŒŒμΌμ„ μ»¨ν…Œμ΄λ„ˆ λ‚΄λΆ€λ‘œ λ³΅μ‚¬ν•œλ‹€.
COPY nginx/nginx.conf /etc/nginx/conf.d

# μ»¨ν…Œμ΄λ„ˆμ˜ 80번 포트λ₯Ό μ—΄μ–΄μ€€λ‹€.
EXPOSE 80

# nginx μ„œλ²„λ₯Ό μ‹€ν–‰ν•˜κ³  λ°±κ·ΈλΌμš΄λ“œλ‘œ λ™μž‘ν•˜λ„λ‘ ν•œλ‹€.
CMD ["nginx", "-g", "daemon off;"]

3. nginx μ„€μ • 파일 λ§Œλ“€κΈ° 

- ν˜„μž¬ 디렉토리에 nginx 폴더λ₯Ό λ§Œλ“€κ³ , κ·Έ μ•ˆμ— nginx.conf νŒŒμΌμ„ λ§Œλ“ λ‹€.

그리고 μ•„λž˜μ™€ 같이 μ„€μ •ν•΄μ€€λ‹€ !

server {
    listen 80;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    location / {
        # rootλ₯Ό /usr/share/nginx/html 을 λ°”λΌλ³΄κ²Œ ν–ˆμœΌλ―€λ‘œ(Dockerfile μ°Έκ³ )
        # ν•΄λ‹Ή 경둜 μ•„λž˜μ— 배포해주면 됨
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
}

4. μ½”λ“œ μˆ˜μ • ν›„ build, 배포 확인해보기

App.js νŒŒμΌμ— μ›ν•˜λŠ” λŒ€λ‘œ μ½”λ“œλ₯Ό μˆ˜μ •ν•œλ‹€.

- 이후 λ„μ»€νŒŒμΌμ—μ„œ μžλ™μ μœΌλ‘œ npm run build λͺ…λ Ήμ–΄λ₯Ό 톡해 React ν”„λ‘œμ νŠΈλ₯Ό build ν•˜κ³  결과물을 κ°€μ Έμ˜¨λ‹€.

 

μ½”λ“œ μˆ˜μ • ν›„ , 깃헙에 pushν•˜λ©΄ 이전에 μ„€μ •ν•΄λ‘” workflowκ°€ λŒμ•„κ°„λ‹€.

 

μ•žμ—μ„œ μ„€μ •ν•΄λ‘” λ„λ©”μΈμœΌλ‘œ μ ‘μ†ν•˜λ©΄ React ν”„λ‘œμ νŠΈλ‘œ κ΅¬μ„±ν•œ 화면이 λœ¬λ‹€ !! 

 

Comments