From d474881e4666245f00a0246c3f603708d9edc59d Mon Sep 17 00:00:00 2001 From: "shdhkims@naver.com" <49316394+shdhkim@users.noreply.github.com> Date: Mon, 25 Nov 2024 23:11:44 +0900 Subject: [PATCH] =?UTF-8?q?11/25=20=EB=B0=B0=ED=8F=AC=20=EC=84=A4=EC=A0=95?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deploy.yml | 82 ++++++++++++++++++++++++++++++++++++ DockerFile | 26 ++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 DockerFile diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..4d7e04d --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,82 @@ +name: Deploy Python App with Docker + +on: + push: + branches: + - main # main 브랜치에 푸시될 때 워크플로우 실행 + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + + steps: + # 1. 코드 체크아웃 + - name: Checkout code + uses: actions/checkout@v4 + + # 2. Python 설치 + - name: Set up Python 3.9 + uses: actions/setup-python@v4 + with: + python-version: '3.9' + + # 3. Python 의존성 설치 + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + # 4. Docker Hub 로그인 + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + # 5. Docker 이미지 빌드 및 푸시 + - name: Build and Push Docker Image + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ${{ secrets.DOCKER_USERNAME }}/python-app:latest + + # 6. EC2로 배포 + - name: Deploy to EC2 + env: + HOST: ${{ secrets.DEPLOY_HOST }} # EC2 퍼블릭 IP 또는 도메인 + USER: ${{ secrets.DEPLOY_USER }} # EC2 SSH 사용자 이름 + KEY: ${{ secrets.DEPLOY_KEY }} # SSH 개인 키 + IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/python-app:latest # Docker 이미지 이름 + run: | + # SSH 설정 + mkdir -p ~/.ssh + echo "$KEY" > deploy_key.pem + chmod 400 deploy_key.pem + echo "Adding $HOST to known_hosts" + if [ -z "$HOST" ]; then echo "HOST is not set"; exit 1; fi + ssh-keyscan -H $HOST >> ~/.ssh/known_hosts || exit 1 + + # EC2 접속 및 Docker 관련 작업 실행 + ssh -i deploy_key.pem $USER@$HOST <