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:
push:
branches:
- main
branches: [main]
jobs:
deploy:
build-and-deploy:
# ────────────────────────────────────────────────
# WICHTIG: Kein "docker" mehr sondern eines der neuen Labels
runs-on: docker
steps:
# --- Checkout ---
- name: Checkout repo
uses: actions/checkout@v3
# --- Tools ---
- name: Install tools
# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING: Workflow Start - Full Transparency
# ═══════════════════════════════════════════════════════════════════════════════
- name: 📋 Log Workflow Start
run: |
apt-get update
apt-get install -y \
docker.io \
openssh-client \
rsync
echo "╔══════════════════════════════════════════════════════════════════════════════╗"
echo "║ MB Grid Solutions Deployment Workflow Started ║"
echo "╚══════════════════════════════════════════════════════════════════════════════╝"
echo ""
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: Login to registry
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
- name: Checkout repository
uses: actions/checkout@v4
# --- Build image ---
- name: Build image
# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING: Registry Login Phase
# ═══════════════════════════════════════════════════════════════════════════════
- name: 🔐 Login to private registry
run: |
echo "Starting Docker build..."
docker build \
echo "╔══════════════════════════════════════════════════════════════════════════════╗"
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 \
-t registry.infra.mintel.me/mintel/mb-grid-solutions:latest .
echo "Docker build completed successfully"
echo "Built image:"
docker images registry.infra.mintel.me/mintel/mb-grid-solutions:latest --format "table {{.Repository}}:{{.Tag}}\t{{.ID}}\t{{.CreatedSince}}"
--platform linux/arm64 \
-t registry.infra.mintel.me/mintel/mb-grid-solutions:latest \
--push .
BUILD_EXIT_CODE=$?
if [ $BUILD_EXIT_CODE -eq 0 ]; then
echo ""
echo "✅ Build completed successfully at: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
echo ""
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 ""
# --- Push image ---
- name: Push image
run: |
echo "Starting Docker push..."
docker push registry.infra.mintel.me/mintel/mb-grid-solutions:latest
echo "Docker push completed successfully"
# --- SSH setup ---
- name: Setup SSH
env:
SSH_KEY: ${{ secrets.ALPHA_SSH_KEY }}
# ═══════════════════════════════════════════════════════════════════════════════
# LOGGING: Deployment Phase
# ═══════════════════════════════════════════════════════════════════════════════
- name: 🚀 Deploy to production server
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
echo "$SSH_KEY" > ~/.ssh/id_ed25519
echo "${{ secrets.ALPHA_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts
# --- Sync files ---
- name: Sync files to server
run: |
echo "Starting file sync to server..."
echo "Syncing docker-compose.yaml to alpha.mintel.me..."
# Use tar to bundle files and send them via SSH in a single connection
echo "🔑 Adding host to known_hosts..."
ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts 2>/dev/null
if [ $? -eq 0 ]; then
echo "✅ Host key added successfully"
else
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 | \
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me \
"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/"
echo "File sync completed"
ssh -o StrictHostKeyChecking=accept-new \
-o IPQoS=0x00 \
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/"
if [ $? -eq 0 ]; then
echo "✅ Files synced successfully"
else
echo "❌ File sync failed"
exit 1
fi
echo ""
# --- Deploy ---
- name: Deploy on server
env:
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }}
# 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: |
echo "Starting deployment on server..."
# Execute deployment commands directly with proper error handling
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me "
set -e
echo '=== Starting deployment ==='
cd /home/deploy/sites/mb-grid-solutions.com
echo '=== Logging into Docker registry ==='
echo '${{ secrets.REGISTRY_PASS }}' | docker login registry.infra.mintel.me -u '${{ secrets.REGISTRY_USER }}' --password-stdin
echo '✓ Registry login successful'
echo '=== Checking current containers ==='
docker compose ps
echo '=== Pulling latest image ==='
docker compose pull
echo '✓ Image pulled successfully'
echo '=== Verifying new image ==='
docker images registry.infra.mintel.me/mintel/mb-grid-solutions:latest
echo '=== Stopping and recreating containers ==='
docker compose up -d
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"
echo "╔══════════════════════════════════════════════════════════════════════════════╗"
echo "║ Workflow Summary ║"
echo "╚══════════════════════════════════════════════════════════════════════════════╝"
echo ""
echo "📊 Final Status:"
echo " • Workflow: ${{ job.status }}"
echo " • Completed: $(date -u +'%Y-%m-%d %H:%M:%S UTC')"
echo ""
echo "🎯 Deployment Target:"
echo " • Image: registry.infra.mintel.me/mintel/mb-grid-solutions:latest"
echo " • Server: alpha.mintel.me"
echo " • Service: mb-grid-solutions.com"
echo ""
echo "🔐 Security Notes:"
echo " • All secrets are masked (*** ) in logs"
echo " • SSH keys are created with 600 permissions"
echo " • Passwords are never displayed in plain text"
echo ""
echo "╔══════════════════════════════════════════════════════════════════════════════╗"
if [ "${{ job.status }}" == "success" ]; then
echo "║ ✅ DEPLOYMENT SUCCESSFUL ║"
else
echo "║ ❌ DEPLOYMENT FAILED ║"
fi
echo "╚══════════════════════════════════════════════════════════════════════════════╝"