ci: switch to native deployment using systemd and a virtual environment
Some checks failed
Deploy Beta (NATIVE) / deploy (push) Failing after 23s
Some checks failed
Deploy Beta (NATIVE) / deploy (push) Failing after 23s
This commit is contained in:
parent
7013ef40b3
commit
7788f90e5a
2 changed files with 69 additions and 58 deletions
|
|
@ -1,4 +1,4 @@
|
|||
name: Build and Deploy Beta
|
||||
name: Deploy Beta (NATIVE)
|
||||
|
||||
on:
|
||||
push:
|
||||
|
|
@ -6,28 +6,7 @@ on:
|
|||
- beta
|
||||
|
||||
jobs:
|
||||
build-and-push:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: https://github.com/actions/checkout@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
apk add --no-cache aws-cli docker-cli
|
||||
|
||||
- name: Log in to Amazon ECR
|
||||
run: |
|
||||
aws ecr get-login-password --region ap-south-2 | docker login --username AWS --password-stdin 764709663363.dkr.ecr.ap-south-2.amazonaws.com
|
||||
|
||||
- name: Build and Push Docker Image
|
||||
run: |
|
||||
docker build -t 764709663363.dkr.ecr.ap-south-2.amazonaws.com/seller-central-backend:beta -t 764709663363.dkr.ecr.ap-south-2.amazonaws.com/seller-central-backend:${{ github.sha }} .
|
||||
docker push 764709663363.dkr.ecr.ap-south-2.amazonaws.com/seller-central-backend:beta
|
||||
docker push 764709663363.dkr.ecr.ap-south-2.amazonaws.com/seller-central-backend:${{ github.sha }}
|
||||
|
||||
deploy:
|
||||
needs: build-and-push
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
|
|
@ -49,9 +28,11 @@ jobs:
|
|||
"logs_collected": {
|
||||
"files": {
|
||||
"collect_list": [
|
||||
{ "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/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 }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -59,6 +40,10 @@ jobs:
|
|||
}
|
||||
EOF
|
||||
|
||||
- name: Package Application
|
||||
run: |
|
||||
tar -czf app.tar.gz --exclude=.git --exclude=tmp .
|
||||
|
||||
- name: Deploy to Beta EC2
|
||||
run: |
|
||||
apk add --no-cache openssh-client aws-cli
|
||||
|
|
@ -66,52 +51,57 @@ jobs:
|
|||
echo "${{ secrets.BETA_EC2_SSH_KEY }}" > ~/.ssh/id_rsa
|
||||
chmod 600 ~/.ssh/id_rsa
|
||||
|
||||
# Copy CloudWatch config to server
|
||||
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa tmp/amazon-cloudwatch-agent.json ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/amazon-cloudwatch-agent.json
|
||||
# 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/
|
||||
|
||||
# SSH to execute deployment steps
|
||||
# Execute deployment commands on remote server
|
||||
ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }} << 'EOF'
|
||||
set -e
|
||||
|
||||
# ── 1. Ensure log directory exists ────────────────────────────
|
||||
mkdir -p /home/ubuntu/seller_central_backend_logs
|
||||
# ── 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
|
||||
|
||||
# ── 2. Move and apply CloudWatch Config ────────────────────────
|
||||
sudo mv /tmp/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
|
||||
# ── 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
|
||||
|
||||
# ── 3. Reload CloudWatch Agent with new config ─────────────────
|
||||
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
|
||||
# ── 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. Get DB Secret ARN from Secrets Manager ──────────────────
|
||||
# ── 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
|
||||
|
||||
# ── 5. Fetch Database Secrets and write to .env ────────────────
|
||||
DB_SECRET_ARN=$(aws secretsmanager describe-secret \
|
||||
--secret-id seller-central/db-credentials \
|
||||
--region ap-south-2 \
|
||||
--query ARN --output text)
|
||||
|
||||
# ── 5. Pull new image ──────────────────────────────────────────
|
||||
command -v aws &> /dev/null || sudo snap install aws-cli --classic || (sudo apt-get update && sudo apt-get install -y unzip && curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip" && unzip -q awscliv2.zip && sudo ./aws/install && rm -rf awscliv2.zip ./aws)
|
||||
aws ecr get-login-password --region ap-south-2 | sudo docker login --username AWS --password-stdin 764709663363.dkr.ecr.ap-south-2.amazonaws.com
|
||||
sudo docker pull 764709663363.dkr.ecr.ap-south-2.amazonaws.com/seller-central-backend:beta
|
||||
echo "DB_SECRET_ARN=$DB_SECRET_ARN" > .env
|
||||
echo "DEBUG=0" >> .env
|
||||
|
||||
# ── 6. Stop and remove old container ──────────────────────────
|
||||
sudo docker stop seller-central-backend-beta || true
|
||||
sudo docker rm seller-central-backend-beta || true
|
||||
# ── 6. Run Database Migrations ─────────────────────────────────
|
||||
./venv/bin/python manage.py migrate --noinput
|
||||
|
||||
# ── 7. Start container with logs volume + DB secret ───────────
|
||||
sudo docker run -d \
|
||||
--name seller-central-backend-beta \
|
||||
--restart always \
|
||||
-p 8080:8000 \
|
||||
-v /home/ubuntu/seller_central_backend_logs:/app/logs \
|
||||
-e DB_SECRET_ARN="$DB_SECRET_ARN" \
|
||||
764709663363.dkr.ecr.ap-south-2.amazonaws.com/seller-central-backend:beta
|
||||
# ── 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. Run DB migrations ───────────────────────────────────────
|
||||
sleep 5
|
||||
sudo docker exec seller-central-backend-beta python manage.py migrate --noinput
|
||||
# ── 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. Prune old images ────────────────────────────────────────
|
||||
sudo docker image prune -a -f
|
||||
# ── 9. Cleanup tmp files ───────────────────────────────────────
|
||||
rm -f /tmp/app.tar.gz
|
||||
EOF
|
||||
|
|
|
|||
21
seller-central-backend.service
Normal file
21
seller-central-backend.service
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
[Unit]
|
||||
Description=Seller Central Backend Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
User=ubuntu
|
||||
WorkingDirectory=/home/ubuntu/seller_central_backend
|
||||
EnvironmentFile=/home/ubuntu/seller_central_backend/.env
|
||||
ExecStart=/home/ubuntu/seller_central_backend/venv/bin/gunicorn config.wsgi:application \
|
||||
--bind 0.0.0.0:8080 \
|
||||
--workers 3 \
|
||||
--access-logfile /home/ubuntu/seller_central_backend/logs/gunicorn-access.log \
|
||||
--error-logfile /home/ubuntu/seller_central_backend/logs/gunicorn-error.log \
|
||||
--log-level info \
|
||||
--capture-output
|
||||
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Loading…
Reference in a new issue