์ด๋ฒˆ์—๋Š” Github Actions CI/CD(1) - git submodule ํฌํ•จ์‹œํ‚คํ‚ค์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ NGINX๋ฅผ ์ ์šฉํ•ด๋ณด๊ฒ ๋‹ค.

๊ฐœ๋ฐœ ํ™˜๊ฒฝ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • Window 11
  • Spring Boot 2.7.7
  • java 11
  • Ubuntu Server.22.04 LTS
  • Intellij IDEA Ultimate

0. NGINX๋ž€?


NGINX๋Š” ํŠธ๋ž˜ํ”ฝ์ด ๋งŽ์€ ์›น์‚ฌ์ดํŠธ์˜ ์„œ๋ฒ„(WAS)๋ฅผ ๋„์™€์ฃผ๋Š” ๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ๊ตฌ์กฐ์˜ ์›น ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.

ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์„ ๋ฐ›์•˜์„ ๋•Œ ์š”์ฒญ์— ๋งž๋Š” ์ •์  ํŒŒ์ผ์„ ์‘๋‹ตํ•ด ์ฃผ๋Š” Web Server๋กœ ํ™œ์šฉ๋˜๊ธฐ๋„ ํ•˜๊ณ , ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ์„œ๋ฒ„๋กœ ํ™œ์šฉ๋˜์–ด ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ์˜ ์—ญํ• ์„ ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ๊ฐœ๋…์ด ์ค‘์š”ํ•œ๋ฐ, ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„ ์„œ๋ฒ„๋กœ ์š”์ฒญ์„ ๋Œ€์‹  ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ ์„œ๋ฒ„(port:80)๊ฐ€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋‹จ์ˆœํžˆ ์ „๋‹ฌ(ํฌ์›Œ๋”ฉ)ํ•˜๊ณ  ์‹ค์ œ ์š”์ฒญ์— ๋Œ€ํ•œ ์ž‘์—…์€ ๋‚ด๋ถ€์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„(ex.์Šคํ”„๋ง๋ถ€ํŠธ 8080)์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์š”์ฒญ์„ ๋ถ„์‚ฐ์‹œ์ผœ ์ฃผ์–ด ์œ„์—์„œ ๋งํ•œ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ์˜ ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๊ณ , ์บ์‹ฑ์ด ๊ฐ€๋Šฅํ•ด ์‘๋‹ต์„ฑ์ด ์ฆ๊ฐ€ํ•˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„๋ฅผ ๊ฐ์ถ”๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ๋„ ๊ฐ•ํ™”๋œ๋‹ค.

๊ทธ๋Ÿผ NGINX์— ๋Œ€ํ•œ ๊ฐœ๋…์€ ์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๊ณ , ์‹ค์ œ ์ ์šฉ์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.


1. NGINX ํŒŒ์ผ(app.conf) ์ž‘์„ฑ


๋‹ค์Œ๊ณผ ๊ฐ™์ด, ํ”„๋กœ์ ํŠธ root ๊ฒฝ๋กœ์— nginx/conf.d ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค๊ณ  app.conf ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

image

ํŒŒ์ผ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

proxy_pass http://myacademy:8080 ๋ถ€๋ถ„์—์„œ docker container ์ด๋ฆ„์„ ๋„ฃ์–ด์ค€๋‹ค.

๋‚˜์˜ ๊ฒฝ์šฐ์—” myacademy.

server {
    listen 80;
    listen [::]:80;

    location / {
        proxy_pass http://myacademy:8080;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}


2. Nignx DockerFile ์ž‘์„ฑ


๋‹ค์Œ์œผ๋กœ, Github Actions์—์„œ CI/CD ํ•˜๊ธฐ ์œ„ํ•œ NGINX DockerFile์„ ์ž‘์„ฑ ํ•ด๋ณด๊ฒ ๋‹ค.

๊ฒฝ๋กœ๋Š” ์œ„์—์„œ ๋งŒ๋“  /nginx ๊ฒฝ๋กœ์— ์ƒ์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

image

๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๋งˆ์ง€๋ง‰์˜ COPY ๋ถ€๋ถ„์€ ํ”„๋กœ์ ํŠธ ./nginx/conf.d/ ๊ฒฝ๋กœ์˜ app.conf ํŒŒ์ผ์„ docker์— ์„ค์น˜๋˜์–ด ์žˆ๋Š” NGINX ๊ฒฝ๋กœ์ธ /etc/nginx/conf.d/ ๊ฒฝ๋กœ๋กœ ๋ณต์‚ฌํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

FROM nginx:latest
RUN rm -rf /etc/nginx/conf.d/default.conf
COPY ./nginx/conf.d/app.conf  /etc/nginx/conf.d/app.conf


3. docker-compose.yml ์ˆ˜์ •


๊ธฐ์กด์— NGINX ์ ์šฉ ์ „์—๋Š” docker-compose.yml ํŒŒ์ผ์ด ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

version: '3'
services:
  web:
    container_name: myacademy
    image: percyfrank/myacademy
    ports:
      - '8080:8080'

NGINX๋ฅผ ์ ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •๋œ๋‹ค.

ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ํฌํŠธ์ธ 80:80๋กœ NGINX ports ์˜ต์…˜์„ ์„ค์ •ํ•ด ์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์˜ ๊ฒฝ์šฐ NGINX๋ฅผ ๊ฑฐ์ณ์„œ ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ปจํ…Œ์ด๋„ˆ ๋‚ด๋ถ€์—์„œ ์—ฐ๊ฒฐํ•ด ์ค„ 8080 ํฌํŠธ๋กœ ์ง€์ •ํ•ด ์ค€๋‹ค.

์—ฌ๊ธฐ์„œ expose ์˜ต์…˜์€ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๊ณต๊ฐœํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋„์ปค ๋‚ด์˜ ์ปจํ…Œ์ด๋„ˆ์—๊ฒŒ๋งŒ ํฌํŠธ๋ฅผ ๊ณต๊ฐœํ•œ๋‹ค.

version: '3'
services:
  web:
    container_name: myacademy
    image: percyfrank/myacademy
    expose:
      - 8080
  nginx:
    container_name: myacademy-nginx
    image: percyfrank/nginx
    restart: always
    ports:
      - '80:80'
    depends_on:
      - web


4. Github Actions CI/CD ์Šคํฌ๋ฆฝํŠธ ์ˆ˜์ •


๋งˆ์ง€๋ง‰์œผ๋กœ, ๊ธฐ์กด์˜ CI/CD ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.

์ˆ˜์ •ํ•  ๋ถ€๋ถ„๋งŒ ๊ฐ€์ ธ์™€์„œ ๋ณด์—ฌ์ฃผ๊ฒ ๋‹ค.

4-1. docker image ์ƒ์„ฑ ๋ฐ docker hub์— push

Before

- name: build and release to DockerHub
  env:
    NAME: $
    APP: myacademy
  run: |
    docker build -t $NAME/$APP -f ./DockerFile .
    docker push $NAME/$APP:latest

After

- name: build and release to DockerHub
  env:
    NAME: $
    APP: myacademy
    NGINX: nginx
  run: |
    docker build -t $NAME/$APP -f ./DockerFile .
    docker build -t $NAME/$NGINX -f ./nginx/DockerFile .
    docker push $NAME/$APP:latest
    docker push $NAME/$NGINX:latest

4-2. docker-compose๋ฅผ ํ†ตํ•ด EC2 ์ธ์Šคํ„ด์Šค์— ์ปจํ…Œ์ด๋„ˆ ์˜ฌ๋ฆฌ๊ธฐ

Before

- name: EC2 Docker Run
  uses: appleboy/ssh-action@master
  env:
    APP: "myacademy"
    COMPOSE: "/home/ubuntu/compose/docker-compose.yml"
  with:
    username: ubuntu
    host: $
    key: $
    envs: APP, COMPOSE
    script_stop: true
    script: |
      sudo docker-compose -f $COMPOSE down --rmi all
      sudo docker pull $/$APP:latest
      sudo docker-compose -f $COMPOSE up -d

After

- name: EC2 Docker Run
  uses: appleboy/ssh-action@master
  env:
    APP: "myacademy"
    NGINX: "nginx"
    COMPOSE: "/home/ubuntu/compose/docker-compose.yml"
  with:
    username: ubuntu
    host: $
    key: $
    envs: APP, NGINX, COMPOSE
    script_stop: true
    script: |
      sudo docker-compose -f $COMPOSE down --rmi all
      sudo docker pull $/$APP:latest
      sudo docker pull $/$NGINX:latest
      sudo docker-compose -f $COMPOSE up -d


5. ๊ฒฐ๊ณผ ํ™•์ธ


NGINX๋ฅผ ์œ„ํ•œ ๋ชจ๋“  ์ž‘์—…์ด ๋๋‚ฌ๋‹ค.

์ฝ”๋“œ PUSH ํ›„, Github Actions์—์„œ CI/CD๊ฐ€ ์„ฑ๊ณตํ•˜๋ฉด ์ปจํ…Œ์ด๋„ˆ๊ฐ€ 2๊ฐœ ์˜ฌ๋ผ์˜ฌ ๊ฒƒ์ด๋‹ค.

ํ™•์ธํ•ด๋ณด์ž.

์ œ๋Œ€๋กœ ์˜ฌ๋ผ์™”๋‹ค. ์ด์ œ ๋ฐฐํฌ ์ฃผ์†Œ ๋’ค์— :8080์„ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

image


References

Leave a comment