2026-08-09 13:13:29 +00:00
|
|
|
name: Deploy Beta (NATIVE)
|
2026-08-09 03:29:16 +00:00
|
|
|
|
|
|
|
|
on:
|
|
|
|
|
push:
|
|
|
|
|
branches:
|
|
|
|
|
- beta
|
|
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
|
deploy:
|
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
|
steps:
|
2026-08-09 12:58:46 +00:00
|
|
|
- name: Checkout Code
|
|
|
|
|
uses: https://github.com/actions/checkout@v4
|
|
|
|
|
|
|
|
|
|
- name: Generate CloudWatch Config Locally
|
|
|
|
|
run: |
|
|
|
|
|
mkdir -p tmp
|
|
|
|
|
cat << 'EOF' > tmp/amazon-cloudwatch-agent.json
|
|
|
|
|
{
|
|
|
|
|
"agent": { "metrics_collection_interval": 60, "run_as_user": "root" },
|
|
|
|
|
"metrics": {
|
|
|
|
|
"metrics_collected": {
|
|
|
|
|
"disk": { "measurement": ["used_percent"], "metrics_collection_interval": 60, "resources": ["/"] },
|
|
|
|
|
"mem": { "measurement": ["mem_used_percent"], "metrics_collection_interval": 60 }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"logs": {
|
|
|
|
|
"logs_collected": {
|
|
|
|
|
"files": {
|
|
|
|
|
"collect_list": [
|
2026-08-09 13:13:29 +00:00
|
|
|
{ "file_path": "/home/ubuntu/seller_central_backend/logs/app.log", "log_group_name": "/seller-central-backend/app", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
|
|
|
{ "file_path": "/home/ubuntu/seller_central_backend/logs/error.log", "log_group_name": "/seller-central-backend/error", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
|
|
|
{ "file_path": "/home/ubuntu/seller_central_backend/logs/requests.log", "log_group_name": "/seller-central-backend/requests", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
|
|
|
{ "file_path": "/home/ubuntu/seller_central_backend/logs/gunicorn-access.log", "log_group_name": "/seller-central-backend/gunicorn-access", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
|
|
|
{ "file_path": "/home/ubuntu/seller_central_backend/logs/gunicorn-error.log", "log_group_name": "/seller-central-backend/gunicorn-error", "log_stream_name": "{instance_id}", "retention_in_days": 30 }
|
2026-08-09 12:58:46 +00:00
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
EOF
|
|
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
- name: Package Application
|
|
|
|
|
run: |
|
|
|
|
|
tar -czf app.tar.gz --exclude=.git --exclude=tmp .
|
|
|
|
|
|
2026-08-09 03:29:16 +00:00
|
|
|
- name: Deploy to Beta EC2
|
2026-08-09 03:32:07 +00:00
|
|
|
run: |
|
2026-08-09 12:58:46 +00:00
|
|
|
apk add --no-cache openssh-client aws-cli
|
2026-08-09 03:32:07 +00:00
|
|
|
mkdir -p ~/.ssh
|
|
|
|
|
echo "${{ secrets.BETA_EC2_SSH_KEY }}" > ~/.ssh/id_rsa
|
|
|
|
|
chmod 600 ~/.ssh/id_rsa
|
2026-08-09 12:58:46 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# Copy app archive and configuration files
|
|
|
|
|
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json seller-central-backend.service ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/
|
2026-08-09 12:58:46 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# Execute deployment commands on remote server
|
2026-08-09 03:32:07 +00:00
|
|
|
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }} << 'EOF'
|
2026-08-09 12:47:58 +00:00
|
|
|
set -e
|
|
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# ── 1. Stop Docker Container (Clean up old docker setup) ───────
|
|
|
|
|
sudo docker stop seller-central-backend-beta || true
|
|
|
|
|
sudo docker rm seller-central-backend-beta || true
|
2026-08-09 12:47:58 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# ── 2. Install Host Dependencies ──────────────────────────────
|
|
|
|
|
sudo apt-get update
|
|
|
|
|
sudo apt-get install -y python3-pip python3-venv python3-dev default-libmysqlclient-dev pkg-config libpq-dev build-essential aws-cli
|
2026-08-09 12:47:58 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# ── 3. Extract codebase to target directory ───────────────────
|
|
|
|
|
sudo mkdir -p /home/ubuntu/seller_central_backend
|
|
|
|
|
sudo chown -R ubuntu:ubuntu /home/ubuntu/seller_central_backend
|
|
|
|
|
tar -xzf /tmp/app.tar.gz -C /home/ubuntu/seller_central_backend
|
|
|
|
|
|
|
|
|
|
# ── 4. Set up python virtual environment & dependencies ────────
|
|
|
|
|
cd /home/ubuntu/seller_central_backend
|
|
|
|
|
mkdir -p logs
|
|
|
|
|
python3 -m venv venv
|
|
|
|
|
./venv/bin/pip install --upgrade pip
|
|
|
|
|
./venv/bin/pip install -r requirements.txt
|
2026-08-09 12:47:58 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# ── 5. Fetch Database Secrets and write to .env ────────────────
|
2026-08-09 12:47:58 +00:00
|
|
|
DB_SECRET_ARN=$(aws secretsmanager describe-secret \
|
|
|
|
|
--secret-id seller-central/db-credentials \
|
|
|
|
|
--region ap-south-2 \
|
|
|
|
|
--query ARN --output text)
|
2026-08-09 13:13:29 +00:00
|
|
|
|
|
|
|
|
echo "DB_SECRET_ARN=$DB_SECRET_ARN" > .env
|
|
|
|
|
echo "DEBUG=0" >> .env
|
2026-08-09 12:47:58 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# ── 6. Run Database Migrations ─────────────────────────────────
|
|
|
|
|
./venv/bin/python manage.py migrate --noinput
|
2026-08-09 12:47:58 +00:00
|
|
|
|
2026-08-09 13:13:29 +00:00
|
|
|
# ── 7. Configure and restart Systemd service ───────────────────
|
|
|
|
|
sudo mv /tmp/seller-central-backend.service /etc/systemd/system/seller-central-backend.service
|
|
|
|
|
sudo systemctl daemon-reload
|
|
|
|
|
sudo systemctl enable seller-central-backend
|
|
|
|
|
sudo systemctl restart seller-central-backend
|
|
|
|
|
|
|
|
|
|
# ── 8. Configure CloudWatch Agent ──────────────────────────────
|
|
|
|
|
sudo mv /tmp/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
|
|
|
|
|
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
|
|
|
|
|
-a fetch-config -m ec2 -s \
|
|
|
|
|
-c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
|
|
|
|
|
|
|
|
|
|
# ── 9. Cleanup tmp files ───────────────────────────────────────
|
|
|
|
|
rm -f /tmp/app.tar.gz
|
2026-08-09 03:32:07 +00:00
|
|
|
EOF
|