forked from ThayseAda/api_pet
-
Notifications
You must be signed in to change notification settings - Fork 0
84 lines (67 loc) · 2.55 KB
/
deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
name: Deploy-to-EC2
on:
push:
branches:
- main
paths-ignore:
- 'docs/**'
- 'README.md'
jobs:
deploy-to-ec2:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set Up Python
uses: actions/setup-python@v3
with:
python-version: '3.11'
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Set Up SSH Key
run: |
echo "${{ secrets.EC2_PRIVATE_KEY }}" > /tmp/ec2-key.pem
chmod 600 /tmp/ec2-key.pem
- name: SSH into EC2 and Deploy
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-east-1'
run: |
ssh -i /tmp/ec2-key.pem -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_IP }} << EOF
# Remove the /home/ec2-user/app directory if it exists
if [ -d "/home/ec2-user/app" ]; then
echo "Removing existing /home/ec2-user/app directory..."
sudo rm -rf /home/ec2-user/app
fi
# Recreate the /home/ec2-user/app directory
mkdir -p /home/ec2-user/app
cd /home/ec2-user/app
# Install git if not already installed
sudo yum install -y git || sudo apt-get install -y git
# Clone the repository again
git clone https://github.com/LuizCampedelli/Projeto_Api_Pet_ADA_Final.git .
# Ensure virtual environment exists, create if not
if [ ! -d "/home/ec2-user/myenv" ]; then
python3 -m venv /home/ec2-user/myenv
fi
# Activate virtual environment
. /home/ec2-user/myenv/bin/activate
# Install requirements, including Flask
if [ -f requirements.txt ]; then
pip install --upgrade pip
pip install -r requirements.txt
else
echo "requirements.txt file not found!"
fi
# Install Flask explicitly if missing from requirements.txt
pip install flask
# Stop any existing Gunicorn process on port 5000
sudo fuser -k 5000/tcp || true
# Start Gunicorn to serve the Flask app
nohup gunicorn --bind 0.0.0.0:5000 app:app > gunicorn.log 2>&1 & disown
# Log message for successful deployment
echo "Deployment completed successfully!"
EOF