Files
klz-cables.com/.gitea/workflows/deploy.yml
Marc Mintel c121398381
Some checks failed
Build & Deploy KLZ Cables / deploy (push) Has been cancelled
deploy
2026-01-25 17:00:02 +01:00

190 lines
7.0 KiB
YAML

name: Build & Deploy KLZ Cables
on:
push:
branches:
- main
jobs:
deploy:
runs-on: docker
steps:
- name: Checkout repo
uses: actions/checkout@v3
- name: Install tools
run: |
apt-get update
apt-get install -y \
docker.io \
openssh-client \
rsync
- name: Login to registry
env:
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASS: ${{ secrets.REGISTRY_PASS }}
run: |
echo "$REGISTRY_PASS" | DOCKER_API_VERSION=1.44 docker login registry.infra.mintel.me -u "$REGISTRY_USER" --password-stdin
- name: Build image
run: |
DOCKER_API_VERSION=1.44 docker build \
--pull \
--build-arg NEXT_PUBLIC_UMAMI_WEBSITE_ID=${{ secrets.NEXT_PUBLIC_UMAMI_WEBSITE_ID }} \
--build-arg NEXT_PUBLIC_UMAMI_SCRIPT_URL=${{ secrets.NEXT_PUBLIC_UMAMI_SCRIPT_URL }} \
--build-arg NEXT_PUBLIC_SENTRY_DSN=${{ secrets.SENTRY_DSN }} \
-t registry.infra.mintel.me/mintel/klz-cables.com:latest .
- name: Push image
run: DOCKER_API_VERSION=1.44 docker push registry.infra.mintel.me/mintel/klz-cables.com:latest
- name: Setup SSH
run: |
mkdir -p ~/.ssh
printf "%s\n" "${{ secrets.ALPHA_SSH_KEY }}" > ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_ed25519
ssh-keyscan -H alpha.mintel.me >> ~/.ssh/known_hosts
- name: Prepare and Deploy
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 "Preparing deployment files..."
COMPOSE_FILE=""
if [ -f "docker-compose.yml" ]; then
COMPOSE_FILE="docker-compose.yml"
elif [ -f "docker-compose.yaml" ]; then
COMPOSE_FILE="docker-compose.yaml"
else
echo "ERROR: No docker-compose file found!"
exit 1
fi
# Create a temporary directory for all files to sync
mkdir -p /tmp/klz-deploy
cp "$COMPOSE_FILE" /tmp/klz-deploy/docker-compose.yml
if [ -d "varnish" ]; then
cp -r varnish /tmp/klz-deploy/
fi
# Create environment file
cat > /tmp/klz-deploy/deploy.env << EOF
export REGISTRY_USER='${REGISTRY_USER}'
export REGISTRY_PASS='${REGISTRY_PASS}'
export NEXT_PUBLIC_UMAMI_WEBSITE_ID='${NEXT_PUBLIC_UMAMI_WEBSITE_ID}'
export NEXT_PUBLIC_UMAMI_SCRIPT_URL='${NEXT_PUBLIC_UMAMI_SCRIPT_URL}'
export SENTRY_DSN='${SENTRY_DSN}'
export REDIS_URL='${REDIS_URL}'
export REDIS_KEY_PREFIX='${REDIS_KEY_PREFIX}'
EOF
# Create deployment script
cat > /tmp/klz-deploy/deploy.sh << 'DEPLOY_EOF'
#!/bin/bash
set -e
# Load environment variables
if [ -f ./deploy.env ]; then
source ./deploy.env
else
echo "ERROR: deploy.env not found in $(pwd)"
ls -la
exit 1
fi
echo "=== Starting deployment at $(date) ==="
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 ==='
if ! echo "${REGISTRY_PASS}" | docker login registry.infra.mintel.me -u "${REGISTRY_USER}" --password-stdin; then
echo "ERROR: Docker 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 '=== Pulling latest image ==='
# Explicitly pull the image to ensure we get the latest version
echo "Pulling registry.infra.mintel.me/mintel/klz-cables.com:latest..."
if ! docker pull registry.infra.mintel.me/mintel/klz-cables.com:latest; then
echo "ERROR: docker pull failed"
exit 1
fi
# Check if we should use 'docker compose' or 'docker-compose'
COMPOSE_CMD="docker compose"
if ! $COMPOSE_CMD version >/dev/null 2>&1; then
COMPOSE_CMD="docker-compose"
fi
echo "Using compose command: $COMPOSE_CMD"
echo "Pulling via compose..."
$COMPOSE_CMD pull
echo '=== Restarting containers ==='
# Use --force-recreate to ensure containers are updated even if compose thinks they are up to date
echo "Running $COMPOSE_CMD up..."
if ! $COMPOSE_CMD up -d --force-recreate --remove-orphans; then
echo "ERROR: docker compose up failed"
exit 1
fi
echo '=== Verifying images ==='
docker images registry.infra.mintel.me/mintel/klz-cables.com:latest
echo '=== Waiting for containers (30s) ==='
sleep 30
echo '=== Verifying health ==='
if curl -f -s http://localhost:80/health > /dev/null 2>&1; then
echo '✓ Application health check passed'
else
echo '✗ Application health check failed'
docker compose logs --tail=50 app
exit 1
fi
echo '=== Cleaning up ==='
docker image prune -f --filter 'until=24h'
echo '=== Deployment completed successfully ==='
DEPLOY_EOF
chmod +x /tmp/klz-deploy/deploy.sh
echo "Syncing and executing on server..."
# Use a single SSH connection to sync and execute
# We use tar to bundle everything and pipe it to SSH
# We use the same SSH options that worked in the previous Sync step
# Added -v to ssh for more debug info if it fails
# Added set -x to the remote command to see exactly what's happening
tar czf - -C /tmp/klz-deploy . | \
ssh -i ~/.ssh/id_ed25519 -o StrictHostKeyChecking=no -o IPQoS=0x00 deploy@alpha.mintel.me \
"set -x; mkdir -p /home/deploy/sites/klz-cables.com/ && tar xzf - -C /home/deploy/sites/klz-cables.com/ && cd /home/deploy/sites/klz-cables.com/ && ls -la && bash -x ./deploy.sh"
if [ $? -ne 0 ]; then
echo "ERROR: Remote deployment failed!"
exit 1
fi
echo "Deployment process finished"