generated from vignesh/searchengine
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 34s
140 lines
6.8 KiB
YAML
140 lines
6.8 KiB
YAML
name: Deploy Beta (NATIVE)
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- beta
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
container:
|
|
image: node:22-alpine
|
|
steps:
|
|
- name: Install git
|
|
run: apk add --no-cache git
|
|
|
|
- name: Checkout Code
|
|
uses: https://github.com/actions/checkout@v4
|
|
|
|
- name: Generate CloudWatch and Nginx Configs 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": [
|
|
{ "file_path": "/home/ubuntu/paymentservice/logs/app.log", "log_group_name": "/paymentservice/app", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
{ "file_path": "/home/ubuntu/paymentservice/logs/error.log", "log_group_name": "/paymentservice/error", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
{ "file_path": "/home/ubuntu/paymentservice/logs/requests.log", "log_group_name": "/paymentservice/requests", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
{ "file_path": "/home/ubuntu/paymentservice/logs/gunicorn-access.log", "log_group_name": "/paymentservice/gunicorn-access", "log_stream_name": "{instance_id}", "retention_in_days": 30 },
|
|
{ "file_path": "/home/ubuntu/paymentservice/logs/gunicorn-error.log", "log_group_name": "/paymentservice/gunicorn-error", "log_stream_name": "{instance_id}", "retention_in_days": 30 }
|
|
]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
EOF
|
|
|
|
cat << 'EOF' > tmp/nginx-proxy.conf
|
|
server {
|
|
server_name betapayments.tradhox.com;
|
|
|
|
location / {
|
|
proxy_pass http://127.0.0.1:8088;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
}
|
|
|
|
listen 443 ssl;
|
|
ssl_certificate /etc/letsencrypt/live/betapayments.tradhox.com/fullchain.pem;
|
|
ssl_certificate_key /etc/letsencrypt/live/betapayments.tradhox.com/privkey.pem;
|
|
include /etc/letsencrypt/options-ssl-nginx.conf;
|
|
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
|
|
}
|
|
|
|
server {
|
|
listen 80;
|
|
server_name betapayments.tradhox.com;
|
|
return 301 https://$host$request_uri;
|
|
}
|
|
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
|
|
mkdir -p ~/.ssh
|
|
echo "${{ secrets.BETA_EC2_SSH_KEY }}" > ~/.ssh/id_rsa
|
|
chmod 600 ~/.ssh/id_rsa
|
|
|
|
# Copy app archive and configuration files
|
|
scp -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa app.tar.gz tmp/amazon-cloudwatch-agent.json tmp/nginx-proxy.conf paymentservice.service ${{ secrets.BETA_EC2_USERNAME }}@${{ secrets.BETA_EC2_HOST }}:/tmp/
|
|
|
|
# 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. Install Host Dependencies ──────────────────────────────
|
|
sudo apt-get update
|
|
sudo apt-get install -y python3-pip python3-venv python3-dev pkg-config libpq-dev build-essential nginx certbot python3-certbot-nginx
|
|
|
|
# ── 2. Extract codebase to target directory ───────────────────
|
|
sudo mkdir -p /home/ubuntu/paymentservice
|
|
sudo chown -R ubuntu:ubuntu /home/ubuntu/paymentservice
|
|
tar -xzf /tmp/app.tar.gz -C /home/ubuntu/paymentservice
|
|
mkdir -p /home/ubuntu/paymentservice/logs
|
|
sudo chown -R ubuntu:ubuntu /home/ubuntu/paymentservice/logs
|
|
|
|
# ── 3. Set up python virtual environment & dependencies ────────
|
|
cd /home/ubuntu/paymentservice
|
|
python3 -m venv venv
|
|
source venv/bin/activate
|
|
pip install --upgrade pip
|
|
pip install -r requirements.txt
|
|
python manage.py migrate --noinput
|
|
|
|
# ── 4. Systemd Service Deployment ──────────────────────────────
|
|
sudo cp /tmp/paymentservice.service /etc/systemd/system/paymentservice.service
|
|
sudo systemctl daemon-reload
|
|
sudo systemctl enable paymentservice.service
|
|
sudo systemctl restart paymentservice.service || {
|
|
echo "paymentservice.service failed to restart! Checking journalctl:"
|
|
sudo journalctl -u paymentservice.service -n 50 --no-pager
|
|
exit 1
|
|
}
|
|
|
|
# ── 5. Ensure SSL Certificate Exists for betapayments.tradhox.com ─
|
|
if [ ! -f "/etc/letsencrypt/live/betapayments.tradhox.com/fullchain.pem" ]; then
|
|
echo "Requesting SSL certificate for betapayments.tradhox.com..."
|
|
sudo systemctl stop nginx || true
|
|
sudo certbot certonly --standalone -d betapayments.tradhox.com --non-interactive --agree-tos -m devops@tradhox.com || true
|
|
sudo systemctl start nginx || true
|
|
fi
|
|
|
|
# ── 6. Nginx Config ───────────────────────────────────────────
|
|
sudo cp /tmp/nginx-proxy.conf /etc/nginx/sites-available/paymentservice
|
|
sudo ln -sf /etc/nginx/sites-available/paymentservice /etc/nginx/sites-enabled/paymentservice
|
|
sudo nginx -t && sudo systemctl reload nginx
|
|
|
|
# ── 7. CloudWatch Agent Config ────────────────────────────────
|
|
sudo mkdir -p /opt/aws/amazon-cloudwatch-agent/etc/
|
|
sudo cp /tmp/amazon-cloudwatch-agent.json /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json
|
|
sudo systemctl restart amazon-cloudwatch-agent || true
|
|
|
|
echo "paymentservice microservice successfully deployed for betapayments.tradhox.com!"
|
|
EOF
|