From 6a6ab6bda3eb63e26b2ec9450dc0b6cbfac7c230 Mon Sep 17 00:00:00 2001 From: ybkang1108 Date: Wed, 31 Jul 2024 17:04:17 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20cd=20=EC=9B=8C=ED=81=AC=ED=94=8C?= =?UTF-8?q?=EB=A1=9C=EC=9A=B0=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 41 ++++++++++++++++++++++ Dockerfile | 66 ++++++++++++++++++++++++++++++++++++ config/docker/entrypoint.sh | 10 ++++++ config/nginx/Dockerfile | 4 +++ config/nginx/nginx.conf | 23 +++++++++++++ config/scripts/deploy.sh | 27 +++++++++++++++ docker-compose.yml | 38 +++++++++++++++++++++ 7 files changed, 209 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 Dockerfile create mode 100644 config/docker/entrypoint.sh create mode 100644 config/nginx/Dockerfile create mode 100644 config/nginx/nginx.conf create mode 100644 config/scripts/deploy.sh create mode 100644 docker-compose.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..71bc691 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,41 @@ +name: Deploy to EC2 +on: [push] +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@develop + + - name: create env file + run: | + touch .env + echo "${{ secrets.ENV_VARS }}" >> .env + + - name: create remote directory + uses: appleboy/ssh-action@develop + with: + host: ${{ secrets.HOST }} + username: ubuntu + key: ${{ secrets.KEY }} + script: mkdir -p /home/ubuntu/srv/ubuntu + + - name: copy source via ssh key + uses: burnett01/rsync-deployments@4.1 + with: + switches: -avzr --delete + remote_path: /home/ubuntu/srv/ubuntu/ + remote_host: ${{ secrets.HOST }} + remote_user: ubuntu + remote_key: ${{ secrets.KEY }} + + - name: executing remote ssh commands using password + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ubuntu + key: ${{ secrets.KEY }} + script: | + sh /home/ubuntu/srv/ubuntu/config/scripts/deploy.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..abf6728 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,66 @@ +# BUILDER # +########### + +# pull official base image +FROM python:3.12 as builder + +# set work directory +WORKDIR /usr/src/app + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +# install psycopg2 dependencies +RUN apt-get update && apt-get install -y python3-dev default-libmysqlclient-dev build-essential && pip install mysqlclient + +# install dependencies +COPY ./requirements.txt . +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt + +# FINAL # +######### + +# pull official base image +FROM python:3.12 + +# create directory for the app user +RUN mkdir -p /home/app + +# create the app user +RUN addgroup --system app && adduser --system --ingroup app app + +# create the appropriate directories +ENV HOME=/home/app +ENV APP_HOME=/home/app/web +RUN mkdir -p $APP_HOME/static $APP_HOME/media +WORKDIR $APP_HOME + +# install dependencies +RUN apt-get update && apt-get install -y libpq-dev +RUN apt-get update \ + && apt-get install -y --no-install-recommends gcc python3-dev musl-dev \ + && apt-get install -y default-libmysqlclient-dev +COPY --from=builder /usr/src/app/wheels /wheels +COPY --from=builder /usr/src/app/requirements.txt . +RUN pip install mysqlclient +RUN pip install --no-cache /wheels/* +RUN apt-get remove -y gcc python3-dev musl-dev && apt-get autoremove -y + +# copy entrypoint.sh +COPY ./config/docker/entrypoint.sh $APP_HOME + +# copy project +COPY . $APP_HOME + +# chown all the files to the app user +RUN chown -R app:app $APP_HOME + +# change to the app user +USER app + +# set entrypoint +ENTRYPOINT ["/home/app/web/entrypoint.sh"] + +# default command +CMD ["gunicorn", "myproject.wsgi:application", "--bind", "0.0.0.0:8000"] diff --git a/config/docker/entrypoint.sh b/config/docker/entrypoint.sh new file mode 100644 index 0000000..1560cfb --- /dev/null +++ b/config/docker/entrypoint.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +# Django 관련 초기화 작업 +python manage.py collectstatic --noinput +python manage.py migrate + +# Gunicorn을 사용하여 Django 애플리케이션 실행 +exec gunicorn mustgou.wsgi:application --bind 0.0.0.0:8000 diff --git a/config/nginx/Dockerfile b/config/nginx/Dockerfile new file mode 100644 index 0000000..805664c --- /dev/null +++ b/config/nginx/Dockerfile @@ -0,0 +1,4 @@ +FROM nginx:1.24.0 + +RUN rm /etc/nginx/conf.d/default.conf +COPY nginx.conf /etc/nginx/conf.d diff --git a/config/nginx/nginx.conf b/config/nginx/nginx.conf new file mode 100644 index 0000000..b573bb6 --- /dev/null +++ b/config/nginx/nginx.conf @@ -0,0 +1,23 @@ +upstream mustgou { + server web:8000; +} + +server { + + listen 80; + + location / { + proxy_pass http://mustgou; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $host; + proxy_redirect off; + } + + location /static/ { + alias /home/app/web/static/; + } + + location /media/ { + alias /home/app/web/media/; + } +} diff --git a/config/scripts/deploy.sh b/config/scripts/deploy.sh new file mode 100644 index 0000000..fe0febf --- /dev/null +++ b/config/scripts/deploy.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Installing docker engine if not exists +if ! type docker > /dev/null +then + echo "docker does not exist" + echo "Start installing docker" + sudo apt-get update + sudo apt install -y apt-transport-https ca-certificates curl software-properties-common + curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - + sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable" + sudo apt update + apt-cache policy docker-ce + sudo apt install -y docker-ce +fi + +# Installing docker-compose if not exists +if ! type docker-compose > /dev/null +then + echo "docker-compose does not exist" + echo "Start installing docker-compose" + sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + sudo chmod +x /usr/local/bin/docker-compose +fi + +echo "start docker-compose up: ubuntu" +sudo docker-compose -f /home/ubuntu/srv/ubuntu/docker-compose.yml up --build -d diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e88a6a5 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,38 @@ +version: '3.12' + +services: # 컨테이너 설정 + + web: # 컨테이너1 : web + container_name: web + build: + context: ./ + dockerfile: Dockerfile + command: gunicorn mustgou.wsgi:application --bind 0.0.0.0:8000 + environment: + DJANGO_SETTINGS_MODULE: mustgou.settings + env_file: + - .env + volumes: + - static:/home/app/web/static + - media:/home/app/web/media + expose: + - 8000 + entrypoint: + - sh + - config/docker/entrypoint.sh + + nginx: # 컨테이너2 : nginx + container_name: nginx + build: ./config/nginx # 여기에는 Dockerfile이 있으며 이 도커파일에서 nginx에 대한 상위 설정파일인 nginx.conf가 있음 + volumes: + - ./config/nginx/nginx.conf:/etc/nginx/conf.d/nginx.conf + - static:/home/app/web/static + - media:/home/app/web/media + ports: + - "80:80" + depends_on: + - web # 웹 컨테이너에 의존 (그래서 web 컨테이너를 먼저 띄운 후, nginx 컨테이너를 띄움) + +volumes: + static: + media: