new deploy

This commit is contained in:
2026-01-27 09:13:06 +01:00
parent abfe74c083
commit 2bbc5b03d8

View File

@@ -1,123 +1,217 @@
name: Build & Deploy name: Build & Deploy MB Grid Solutions
on: on:
push: push:
branches: branches: [main]
- main
jobs: jobs:
deploy: build-and-deploy:
# ────────────────────────────────────────────────
# WICHTIG: Kein "docker" mehr sondern eines der neuen Labels
runs-on: docker runs-on: docker
steps: steps:
# --- Checkout --- # ═══════════════════════════════════════════════════════════════════════════════
- name: Checkout repo # LOGGING: Workflow Start - Full Transparency
uses: actions/checkout@v3 # ═══════════════════════════════════════════════════════════════════════════════
- name: 📋 Log Workflow Start
# --- Tools ---
- name: Install tools
run: | run: |
apt-get update echo "╔══════════════════════════════════════════════════════════════════════════════╗"
apt-get install -y \ echo "║ MB Grid Solutions Deployment Workflow Started ║"
docker.io \ echo "╚══════════════════════════════════════════════════════════════════════════════╝"
openssh-client \ echo ""
rsync echo "📋 Workflow Information:"
echo " • Repository: ${{ github.repository }}"
echo " • Branch: ${{ github.ref }}"
echo " • Commit: ${{ github.sha }}"
echo " • Actor: ${{ github.actor }}"
echo " • Run ID: ${{ github.run_id }}"
echo " • Timestamp: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
echo ""
echo "🔍 Environment Details:"
echo " • Runner OS: ${{ runner.os }}"
echo " • Workspace: ${{ github.workspace }}"
echo ""
# --- Docker registry login --- - name: Checkout repository
- name: Login to registry uses: actions/checkout@v4
env:
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }}
run: |
echo "$REGISTRY_PASS" | docker login registry.infra.mintel.me \
-u "$REGISTRY_USER" \
--password-stdin
# --- Build image --- # ═══════════════════════════════════════════════════════════════════════════════
- name: Build image # LOGGING: Registry Login Phase
# ═══════════════════════════════════════════════════════════════════════════════
- name: 🔐 Login to private registry
run: | run: |
echo "Starting Docker build..." echo "╔══════════════════════════════════════════════════════════════════════════════╗"
docker build \ echo "║ Step: Registry Login ║"
echo "╚══════════════════════════════════════════════════════════════════════════════╝"
echo ""
echo "🔐 Authenticating with private registry..."
echo " Registry: registry.infra.mintel.me"
echo " User: ${{ secrets.REGISTRY_USER != '' && '***' || 'NOT SET' }}"
echo ""
# Execute login with error handling
if echo "${{ secrets.REGISTRY_PASS }}" | docker login registry.infra.mintel.me -u "${{ secrets.REGISTRY_USER }}" --password-stdin 2>&1; then
echo "✅ Registry login successful"
else
echo "❌ Registry login failed"
exit 1
fi
echo ""
# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING: Build Phase
# ═══════════════════════════════════════════════════════════════════════════════
- name: 🏗️ Build Docker image
run: |
echo "╔══════════════════════════════════════════════════════════════════════════════╗"
echo "║ Step: Build Docker Image ║"
echo "╚══════════════════════════════════════════════════════════════════════════════╝"
echo ""
echo "🏗️ Building Docker image with buildx..."
echo " Platform: linux/arm64"
echo " Target: registry.infra.mintel.me/mintel/mb-grid-solutions:latest"
echo ""
echo "⏱️ Build started at: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
echo ""
# Execute build with detailed logging
set -e
docker buildx build \
--pull \ --pull \
-t registry.infra.mintel.me/mintel/mb-grid-solutions:latest . --platform linux/arm64 \
echo "Docker build completed successfully" -t registry.infra.mintel.me/mintel/mb-grid-solutions:latest \
echo "Built image:" --push .
docker images registry.infra.mintel.me/mintel/mb-grid-solutions:latest --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedSince}}"
# --- Push image --- BUILD_EXIT_CODE=$?
- name: Push image if [ $BUILD_EXIT_CODE -eq 0 ]; then
run: | echo ""
echo "Starting Docker push..." echo "✅ Build completed successfully at: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
docker push registry.infra.mintel.me/mintel/mb-grid-solutions:latest echo ""
echo "Docker push completed successfully" echo "📊 Image Details:"
IMAGE_SIZE=$(docker inspect registry.infra.mintel.me/mintel/mb-grid-solutions:latest --format='{{.Size}}')
IMAGE_SIZE_MB=$((IMAGE_SIZE / 1024 / 1024))
echo " • Size: ${IMAGE_SIZE_MB}MB"
docker inspect registry.infra.mintel.me/mintel/mb-grid-solutions:latest --format=' • Created: {{.Created}}'
docker inspect registry.infra.mintel.me/mintel/mb-grid-solutions:latest --format=' • Architecture: {{.Architecture}}'
else
echo ""
echo "❌ Build failed with exit code: $BUILD_EXIT_CODE"
exit $BUILD_EXIT_CODE
fi
echo ""
# --- SSH setup --- # ═══════════════════════════════════════════════════════════════════════════════
- name: Setup SSH # LOGGING: Deployment Phase
env: # ═══════════════════════════════════════════════════════════════════════════════
SSH_KEY: ${{ secrets.ALPHA_SSH_KEY }} - name: 🚀 Deploy to production server
run: | run: |
echo "╔══════════════════════════════════════════════════════════════════════════════╗"
echo "║ Step: Deploy to Production Server ║"
echo "╚══════════════════════════════════════════════════════════════════════════════╝"
echo ""
echo "🚀 Starting deployment process..."
echo " Target Server: alpha.mintel.me"
echo " Deploy User: deploy (via sudo from root)"
echo " Target Path: /home/deploy/sites/mb-grid-solutions.com"
echo ""
# Setup SSH with logging
echo "🔐 Setting up SSH connection..."
mkdir -p ~/.ssh mkdir -p ~/.ssh
echo "$SSH_KEY" > ~/.ssh/id_ed25519 echo "${{ secrets.ALPHA_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519 chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts
# --- Sync files --- echo "🔑 Adding host to known_hosts..."
- name: Sync files to server ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts 2>/dev/null
run: | if [ $? -eq 0 ]; then
echo "Starting file sync to server..." echo "✅ Host key added successfully"
echo "Syncing docker-compose.yaml to alpha.mintel.me..." else
# Use tar to bundle files and send them via SSH in a single connection echo "⚠️ Warning: Could not add host key"
fi
echo ""
# Sync docker-compose.yaml first (as in original workflow)
echo "📦 Syncing docker-compose.yaml..."
tar czf - docker-compose.yaml | \ tar czf - docker-compose.yaml | \
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me \ ssh -o StrictHostKeyChecking=accept-new \
"mkdir -p /home/deploy/sites/mb-grid-solutions.com/ && tar xzf - -C /home/deploy/sites/mb-grid-solutions.com/ && echo 'Files synced successfully' && ls -la /home/deploy/sites/mb-grid-solutions.com/" -o IPQoS=0x00 \
echo "File sync completed" root@alpha.mintel.me \
"mkdir -p /home/deploy/sites/mb-grid-solutions.com/ && tar xzf - -C /home/deploy/sites/mb-grid-solutions.com/ && chown -R deploy:deploy /home/deploy/sites/mb-grid-solutions.com/"
# --- Deploy --- if [ $? -eq 0 ]; then
- name: Deploy on server echo "✅ Files synced successfully"
env: else
REGISTRY_USER: ${{ secrets.REGISTRY_USER }} echo "❌ File sync failed"
REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }} exit 1
fi
echo ""
# Execute deployment commands with detailed logging
echo "📡 Connecting to server and executing deployment commands..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# SSH as root and use sudo to run deployment script as deploy user
# We use a custom command here since we don't have a deploy.sh on the server yet,
# or we follow the pattern of the example which assumes a deploy.sh exists.
# Looking at the original workflow, it ran commands directly.
# The example uses "sudo -u deploy /home/deploy/deploy.sh".
# I will stick to the example's pattern but ensure the site path is correct.
ssh -o StrictHostKeyChecking=accept-new \
-o ServerAliveInterval=30 \
-o ServerAliveCountMax=3 \
-o ConnectTimeout=10 \
root@alpha.mintel.me \
"sudo -u deploy /home/deploy/deploy.sh"
DEPLOY_EXIT_CODE=$?
echo ""
if [ $DEPLOY_EXIT_CODE -eq 0 ]; then
echo "✅ Deployment completed successfully at: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
else
echo "❌ Deployment failed with exit code: $DEPLOY_EXIT_CODE"
echo ""
echo "🔍 Troubleshooting Tips:"
echo " • Check server connectivity: ping alpha.mintel.me"
echo " • Verify SSH key permissions on server"
echo " • Check disk space on target server"
echo " • Review docker compose configuration"
echo " • Ensure /home/deploy/deploy.sh exists and is executable"
exit $DEPLOY_EXIT_CODE
fi
echo ""
# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING: Workflow Summary
# ═══════════════════════════════════════════════════════════════════════════════
- name: 📊 Workflow Summary
if: always()
run: | run: |
echo "Starting deployment on server..." echo "╔══════════════════════════════════════════════════════════════════════════════╗"
echo "║ Workflow Summary ║"
# Execute deployment commands directly with proper error handling echo "╚══════════════════════════════════════════════════════════════════════════════╝"
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me " echo ""
set -e echo "📊 Final Status:"
echo " • Workflow: ${{ job.status }}"
echo '=== Starting deployment ===' echo " • Completed: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
cd /home/deploy/sites/mb-grid-solutions.com echo ""
echo "🎯 Deployment Target:"
echo '=== Logging into Docker registry ===' echo " • Image: registry.infra.mintel.me/mintel/mb-grid-solutions:latest"
echo '${{ secrets.REGISTRY_PASS }}' | docker login registry.infra.mintel.me -u '${{ secrets.REGISTRY_USER }}' --password-stdin echo " • Server: alpha.mintel.me"
echo '✓ Registry login successful' echo " • Service: mb-grid-solutions.com"
echo ""
echo '=== Checking current containers ===' echo "🔐 Security Notes:"
docker compose ps echo " • All secrets are masked (*** ) in logs"
echo " • SSH keys are created with 600 permissions"
echo '=== Pulling latest image ===' echo " • Passwords are never displayed in plain text"
docker compose pull echo ""
echo '✓ Image pulled successfully' echo "╔══════════════════════════════════════════════════════════════════════════════╗"
if [ "${{ job.status }}" == "success" ]; then
echo '=== Verifying new image ===' echo "║ ✅ DEPLOYMENT SUCCESSFUL ║"
docker images registry.infra.mintel.me/mintel/mb-grid-solutions:latest else
echo "║ ❌ DEPLOYMENT FAILED ║"
echo '=== Stopping and recreating containers ===' fi
docker compose up -d echo "╚══════════════════════════════════════════════════════════════════════════════╝"
echo '✓ Containers recreated'
echo '=== Waiting for health check (15 seconds) ==='
sleep 15
echo '=== Verifying deployment ==='
docker compose ps app
echo '=== Checking container logs ==='
docker compose logs --tail=20 app
echo '=== Cleaning up old images ==='
docker image prune -f --filter 'until=24h'
echo '=== Deployment completed successfully ==='
"
echo "Deployment completed"