Update django_CICD.yml #45
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Django CI/CD | |
on: | |
push: | |
branches: ["main"] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Python 3.10 | |
uses: actions/setup-python@v3 | |
with: | |
python-version: '3.10' | |
- name: Install Dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install django djangorestframework drf-yasg | |
pip install -r requirements.txt | |
working-directory: aiServer | |
upload: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Create secrets.json | |
uses: jsdaniell/[email protected] | |
with: | |
name: "secrets.json" | |
json: ${{ secrets.SECRETS }} | |
dir: 'aiServer' | |
- name: Create my_settings.py | |
run: | | |
echo "${{ secrets.DB_SETTINGS }}" > aiServer/aiServer/my_settings.py | |
- name: Upload Code to EC2 | |
env: | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
EC2_USER: ubuntu | |
EC2_KEY: ${{ secrets.EC2_KEY }} | |
run: | | |
echo "${{ secrets.EC2_KEY }}" > key.pem | |
chmod 400 key.pem | |
ssh -o StrictHostKeyChecking=no -i key.pem $EC2_USER@$EC2_HOST << EOF | |
mkdir -p /home/ubuntu/aiServer/logs | |
mkdir -p /home/ubuntu/aiServer/staticfiles | |
EOF | |
scp -o StrictHostKeyChecking=no -i key.pem -r ./aiServer $EC2_USER@$EC2_HOST:/home/ubuntu/ | |
rm key.pem | |
deploy: | |
runs-on: ubuntu-latest | |
needs: upload | |
steps: | |
- name: Deploy and Verify | |
env: | |
EC2_HOST: ${{ secrets.EC2_HOST }} | |
EC2_USER: ubuntu | |
EC2_KEY: ${{ secrets.EC2_KEY }} | |
run: | | |
echo "${{ secrets.EC2_KEY }}" > key.pem | |
chmod 400 key.pem | |
ssh -o StrictHostKeyChecking=no -i key.pem $EC2_USER@$EC2_HOST << EOF | |
pkill -9 gunicorn || true | |
sudo apt update | |
# 필수 패키지 설치 | |
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev | |
# 프로젝트 디렉토리로 이동 | |
cd /home/ubuntu/aiServer | |
# 가상환경 설정 및 실행 | |
python3 -m venv venv | |
source venv/bin/activate | |
# 필요한 패키지 재설치 | |
pip install --force-reinstall cffi cryptography pyopenssl | |
pip install --upgrade pip | |
pip install gunicorn django djangorestframework drf-yasg | |
pip install -r requirements.txt | |
python manage.py makemigrations | |
python manage.py migrate | |
python manage.py collectstatic --noinput | |
# Gunicorn 실행 | |
nohup gunicorn --bind 0.0.0.0:8000 aiServer.wsgi:application \ | |
--workers 3 \ | |
--timeout 120 \ | |
--access-logfile logs/gunicorn-access.log \ | |
--error-logfile logs/gunicorn-error.log & | |
sleep 10 | |
# Nginx 재시작 | |
sudo service nginx restart | |
# 상태 확인 | |
echo "Checking Gunicorn processes:" | |
ps aux | grep gunicorn | |
echo "Checking Gunicorn logs:" | |
tail -n 20 logs/gunicorn-access.log | |
tail -n 20 logs/gunicorn-error.log | |
echo "Testing service response:" | |
curl -I http://localhost:8000 | |
echo "Checking Nginx status:" | |
sudo systemctl status nginx | |
EOF | |
rm key.pem |