diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 899546ef..9cb5910a 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -63,8 +63,18 @@ jobs: echo "Found and syncing: $COMPOSE_FILE" + # Create a temporary directory for files to sync + mkdir -p /tmp/klz-deploy + cp "$COMPOSE_FILE" /tmp/klz-deploy/ + + # Check if varnish directory exists and copy it + if [ -d "varnish" ]; then + echo "Syncing varnish directory..." + cp -r varnish /tmp/klz-deploy/ + fi + # Use tar to bundle files and send them via SSH in a single connection - tar czf - "$COMPOSE_FILE" varnish 2>/dev/null || tar czf - "$COMPOSE_FILE" | \ + tar czf - -C /tmp/klz-deploy . | \ ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me \ "mkdir -p /home/deploy/sites/klz-cables.com/ && tar xzf - -C /home/deploy/sites/klz-cables.com/ && echo 'Files synced successfully' && ls -la /home/deploy/sites/klz-cables.com/" @@ -74,87 +84,117 @@ jobs: env: REGISTRY_USER: ${{ secrets.REGISTRY_USER }} REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }} + NEXT_PUBLIC_UMAMI_WEBSITE_ID: ${{ secrets.NEXT_PUBLIC_UMAMI_WEBSITE_ID }} + NEXT_PUBLIC_UMAMI_SCRIPT_URL: ${{ secrets.NEXT_PUBLIC_UMAMI_SCRIPT_URL }} + SENTRY_DSN: ${{ secrets.SENTRY_DSN }} + REDIS_URL: ${{ secrets.REDIS_URL }} + REDIS_KEY_PREFIX: ${{ secrets.REDIS_KEY_PREFIX }} run: | echo "Starting deployment on server..." - # Execute deployment commands directly with proper error handling + # Create deployment script on server + cat > /tmp/deploy.sh << 'DEPLOY_EOF' + #!/bin/bash + set -e + + echo '=== Starting deployment ===' + cd /home/deploy/sites/klz-cables.com + + echo '=== Creating .env ===' + cat > .env << EOF + NEXT_PUBLIC_UMAMI_WEBSITE_ID=${NEXT_PUBLIC_UMAMI_WEBSITE_ID} + NEXT_PUBLIC_UMAMI_SCRIPT_URL=${NEXT_PUBLIC_UMAMI_SCRIPT_URL} + SENTRY_DSN=${SENTRY_DSN} + REDIS_URL=${REDIS_URL} + REDIS_KEY_PREFIX=${REDIS_KEY_PREFIX} + EOF + + echo '=== Logging into Docker registry ===' + echo '${REGISTRY_PASS}' | docker login registry.infra.mintel.me -u '${REGISTRY_USER}' --password-stdin + if [ $? -eq 0 ]; then + echo '✓ Registry login successful' + else + echo '✗ Registry login failed' + exit 1 + fi + + echo '=== Checking if infra network exists ===' + if ! docker network inspect infra >/dev/null 2>&1; then + echo 'Creating infra network...' + docker network create infra || true + fi + + echo '=== Checking current containers ===' + docker compose ps + + echo '=== Checking current image ===' + docker images registry.infra.mintel.me/mintel/klz-cables.com:latest + + echo '=== Removing local image to force fresh pull ===' + docker rmi registry.infra.mintel.me/mintel/klz-cables.com:latest || true + + echo '=== Pulling latest image ===' + docker compose pull + echo '✓ Image pull command completed' + + echo '=== Verifying new image after pull ===' + docker images registry.infra.mintel.me/mintel/klz-cables.com:latest + + echo '=== Checking if image was actually updated ===' + if docker inspect registry.infra.mintel.me/mintel/klz-cables.com:latest >/dev/null 2>&1; then + echo '✓ Image exists locally' + else + echo '✗ Image pull failed - image not found locally' + exit 1 + fi + + echo '=== Stopping all containers ===' + docker compose down + echo '✓ Containers stopped' + + echo '=== Starting all containers ===' + docker compose up -d + echo '✓ Containers started' + + echo '=== Waiting for containers to be ready (30 seconds) ===' + sleep 30 + + echo '=== Verifying deployment status ===' + docker compose ps + + echo '=== Checking app container logs ===' + docker compose logs --tail=30 app + + echo '=== Checking varnish container logs ===' + docker compose logs --tail=20 varnish + + echo '=== Verifying application health ===' + # Wait a bit more for the app to be fully ready + sleep 15 + if curl -f -s http://localhost:80/health > /dev/null 2>&1; then + echo '✓ Application health check passed' + else + echo '✗ Application health check failed - checking app logs' + docker compose logs --tail=50 app + exit 1 + fi + + echo '=== Cleaning up old images ===' + docker image prune -f --filter 'until=24h' + + echo '=== Deployment completed successfully ===' + DEPLOY_EOF + + # Make script executable and copy to server + chmod +x /tmp/deploy.sh + + # Copy deployment script to server + scp -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 /tmp/deploy.sh deploy@alpha.mintel.me:/tmp/deploy.sh + + # Execute deployment script on server ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me " set -e - - echo '=== Starting deployment ===' - cd /home/deploy/sites/klz-cables.com - - echo '=== Creating .env ===' - echo 'SENTRY_DSN=${{ secrets.SENTRY_DSN }}' > .env - - echo '=== Logging into Docker registry ===' - echo '${{ secrets.REGISTRY_PASS }}' | docker login registry.infra.mintel.me -u '${{ secrets.REGISTRY_USER }}' --password-stdin - if [ $? -eq 0 ]; then - echo '✓ Registry login successful' - else - echo '✗ Registry login failed' - exit 1 - fi - - echo '=== Checking current containers ===' - docker compose ps - - echo '=== Checking current image ===' - docker images registry.infra.mintel.me/mintel/klz-cables.com:latest - - echo '=== Removing local image to force fresh pull ===' - docker rmi registry.infra.mintel.me/mintel/klz-cables.com:latest || true - - echo '=== Pulling latest image ===' - docker compose pull - echo '✓ Image pull command completed' - - echo '=== Verifying new image after pull ===' - docker images registry.infra.mintel.me/mintel/klz-cables.com:latest - - echo '=== Checking if image was actually updated ===' - if docker inspect registry.infra.mintel.me/mintel/klz-cables.com:latest >/dev/null 2>&1; then - echo '✓ Image exists locally' - else - echo '✗ Image pull failed - image not found locally' - exit 1 - fi - - echo '=== Stopping all containers ===' - docker compose down - echo '✓ Containers stopped' - - echo '=== Starting all containers ===' - docker compose up -d - echo '✓ Containers started' - - echo '=== Waiting for containers to be ready (20 seconds) ===' - sleep 20 - - echo '=== Verifying deployment status ===' - docker compose ps - - echo '=== Checking app container logs ===' - docker compose logs --tail=30 app - - echo '=== Checking varnish container logs ===' - docker compose logs --tail=20 varnish - - echo '=== Verifying application health ===' - # Wait a bit more for the app to be fully ready - sleep 10 - if curl -f -s http://localhost:80/health > /dev/null 2>&1; then - echo '✓ Application health check passed' - else - echo '✗ Application health check failed - checking app logs' - docker compose logs --tail=50 app - exit 1 - fi - - echo '=== Cleaning up old images ===' - docker image prune -f --filter 'until=24h' - - echo '=== Deployment completed successfully ===' + bash /tmp/deploy.sh " - + echo "Deployment completed" \ No newline at end of file