From 83b76b1667196224e42e39020ab17a44121a6896 Mon Sep 17 00:00:00 2001 From: rocknroll17 Date: Sat, 2 Nov 2024 23:10:24 +0900 Subject: [PATCH] feat:github action CI/CD added --- .github/workflows/deploy.yml | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..9272095 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,46 @@ +name: CI/CD for FastAPI + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.9' # Use the version you need + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + + - name: EC2로 SSH 접속하여 git pull + env: + SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} + EC2_USER: 'ubuntu' + EC2_HOST: 'ec2-3-38-23-48.ap-northeast-2.compute.amazonaws.com' + run: | + # 개인 키를 파일에 저장 + echo "${SSH_PRIVATE_KEY}" > private_key + chmod 600 private_key + + # EC2 인스턴스에 SSH 접속하여 git pull 실행 + ssh -i private_key -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST 'cd ~/mirisa_server && git pull origin main' + + - name: Restart the FastAPI application + env: + SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} + EC2_USER: 'ubuntu' + EC2_HOST: 'your_ec2_ip' + run: | + echo "${SSH_PRIVATE_KEY}" > private_key + chmod 600 private_key + ssh -i private_key $EC2_USER@$EC2_HOST 'sudo lsof -t -i :1500 | xargs sudo kill -9 && screen -S server -X stuff "source myenv/bin/activate\n" && screen -S server -X stuff "cd mirisa_server/\n" && screen -S server -X stuff "python3 main.py\n"'