Some checks failed
Deploy Production Email Reports (Unified) / deploy (push) Failing after 18s
182 lines
6.5 KiB
YAML
182 lines
6.5 KiB
YAML
name: Deploy Production Email Reports (Unified)
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
- master
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
container:
|
|
image: catthehacker/ubuntu:act-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
shell: sh
|
|
run: |
|
|
git clone $GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git /workspace/repo
|
|
cd /workspace/repo
|
|
git checkout $GITHUB_REF_NAME
|
|
|
|
- name: Build Docker image
|
|
shell: sh
|
|
run: |
|
|
cd /workspace/repo
|
|
docker build -t email_reports_unified:latest .
|
|
docker save email_reports_unified:latest | gzip > email_reports_unified.tar.gz
|
|
|
|
- name: Setup SSH
|
|
shell: sh
|
|
env:
|
|
DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
chmod 700 ~/.ssh
|
|
echo "$DEPLOY_SSH_KEY" > ~/.ssh/id_ed25519
|
|
chmod 600 ~/.ssh/id_ed25519
|
|
ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts
|
|
|
|
- name: Upload image and compose
|
|
shell: sh
|
|
env:
|
|
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
run: |
|
|
scp -i ~/.ssh/id_ed25519 \
|
|
/workspace/repo/email_reports_unified.tar.gz \
|
|
/workspace/repo/docker-compose.yml \
|
|
${DEPLOY_USER}@${DEPLOY_HOST}:/tmp/
|
|
|
|
- name: Deploy on server
|
|
shell: sh
|
|
env:
|
|
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
run: |
|
|
ssh -i ~/.ssh/id_ed25519 $DEPLOY_USER@$DEPLOY_HOST << 'EOF'
|
|
set -e
|
|
|
|
DEPLOY_DIR="/var/www/apps/email_reports"
|
|
sudo mkdir -p "$DEPLOY_DIR"
|
|
sudo chown $USER:$USER "$DEPLOY_DIR"
|
|
|
|
echo "Loading image"
|
|
docker load < /tmp/email_reports_unified.tar.gz
|
|
|
|
echo "Removing old email_reports images"
|
|
docker images | grep email_reports_unified | grep -v "$(docker images email_reports_unified:latest -q)" | awk '{print $3}' | xargs -r docker rmi -f || true
|
|
|
|
echo "Updating compose file"
|
|
cp /tmp/docker-compose.yml "$DEPLOY_DIR/docker-compose.yml"
|
|
|
|
cd "$DEPLOY_DIR"
|
|
|
|
echo "Checking .env file"
|
|
if [ ! -f .env ]; then
|
|
echo ".env file not found at $DEPLOY_DIR/.env"
|
|
echo "Please create it first with required variables:"
|
|
echo " Crew: DB_HOST_CREW, DB_NAME_CREW, DB_USER_CREW, DB_PASS_CREW, SMTP_PASS_CREW"
|
|
echo " MerchBay: DB_HOST_MERCHBAY, DB_NAME_MERCHBAY, DB_USER_MERCHBAY, DB_PASS_MERCHBAY, SMTP_PASS_MERCHBAY"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Fixing .env permissions"
|
|
sudo chown $USER:$USER .env
|
|
sudo chmod 600 .env
|
|
|
|
echo "Ensure networks"
|
|
docker network inspect crew-app-net >/dev/null 2>&1 || \
|
|
docker network create crew-app-net
|
|
|
|
echo "Creating required directories"
|
|
mkdir -p daily_order_reports_crew daily_order_reports_merchbay
|
|
touch email.log
|
|
chmod 666 email.log
|
|
chmod 755 daily_order_reports_crew daily_order_reports_merchbay
|
|
|
|
echo "Stopping existing containers"
|
|
docker compose down || true
|
|
docker rm -f email_reports_unified email_reports_crew email_reports_merchbay || true
|
|
|
|
echo "Starting unified container (env vars from .env file)"
|
|
docker compose up -d
|
|
|
|
echo "Waiting for container to start (30 seconds)..."
|
|
sleep 5
|
|
|
|
# Check if container is running or restarting
|
|
CONTAINER_STATE=$(docker inspect --format='{{.State.Status}}' email_reports_unified 2>/dev/null || echo "not_found")
|
|
echo "Container state: $CONTAINER_STATE"
|
|
|
|
if [ "$CONTAINER_STATE" = "restarting" ]; then
|
|
echo "Container is restarting - showing logs:"
|
|
docker logs email_reports_unified
|
|
exit 1
|
|
fi
|
|
|
|
# Wait a bit more for initialization
|
|
sleep 25
|
|
|
|
if docker ps --format '{{.Names}}' | grep -q email_reports_unified; then
|
|
echo "✓ Container is running"
|
|
echo "Testing cron daemon"
|
|
docker exec email_reports_unified ps aux | grep -q crond && echo "✓ Cron is running"
|
|
|
|
echo "Cron schedule:"
|
|
docker exec email_reports_unified crontab -l
|
|
|
|
echo "Testing database connectivity (Crew)"
|
|
docker exec email_reports_unified ping -c 1 mysql && echo "✓ Can reach MySQL"
|
|
else
|
|
echo "✗ Container failed to start"
|
|
docker compose logs
|
|
exit 1
|
|
fi
|
|
|
|
echo "Cleanup"
|
|
rm -f /tmp/email_reports_unified.tar.gz /tmp/docker-compose.yml
|
|
|
|
echo "Docker cleanup"
|
|
docker image prune -af --filter "until=24h" || true
|
|
docker container prune -f || true
|
|
docker system df
|
|
|
|
echo "✓ Deployment completed!"
|
|
echo "Email reports container: email_reports_unified"
|
|
echo "Next scheduled runs:"
|
|
echo " - Crew: 23:55 CT"
|
|
echo " - MerchBay: 23:56 CT"
|
|
EOF
|
|
|
|
- name: Verify deployment
|
|
shell: sh
|
|
env:
|
|
DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
|
|
DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
|
|
run: |
|
|
ssh -i ~/.ssh/id_ed25519 $DEPLOY_USER@$DEPLOY_HOST << 'EOF'
|
|
echo "Container status:"
|
|
docker ps --filter "name=email_reports_unified" --format "table {{.Names}}\t{{.Status}}\t{{.State}}"
|
|
|
|
echo ""
|
|
echo "Recent logs:"
|
|
docker logs --tail 20 email_reports_unified
|
|
|
|
echo ""
|
|
echo "To test manually:"
|
|
echo " Crew: docker exec email_reports_unified BRAND=crew php /app/send_report.php"
|
|
echo " MerchBay: docker exec email_reports_unified BRAND=merchbay php /app/send_report.php"
|
|
echo ""
|
|
echo "To view logs:"
|
|
echo " docker logs -f email_reports_unified"
|
|
echo " docker exec email_reports_unified tail -f /app/email.log"
|
|
echo ""
|
|
echo "Filter by brand:"
|
|
echo " docker exec email_reports_unified grep '[CREW]' /app/email.log"
|
|
echo " docker exec email_reports_unified grep '[MERCHBAY]' /app/email.log"
|
|
EOF
|