diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..f6ae238 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,193 @@ +# GitLab CI/CD Pipeline for AlexHost Deployment +# Server: 193.233.245.41 (teleshop.silentmary.mywire.org) + +stages: + - build + - deploy + - verify + +variables: + ALEXHOST_IP: "193.233.245.41" + ALEXHOST_USER: "sysadmin" + REGISTRY: "localhost:5000" + TELESHOP_IMAGE: "littleshop" + TELEBOT_IMAGE: "telebot" + +# Manual deployment to AlexHost Production +deploy-alexhost: + stage: deploy + image: docker:24.0 + services: + - docker:24.0-dind + rules: + - when: manual + variables: + DOCKER_TLS_CERTDIR: "/certs" + before_script: + - apk add --no-cache openssh-client curl tar gzip + - mkdir -p ~/.ssh + - echo "$ALEXHOST_SSH_KEY" > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan -H $ALEXHOST_IP >> ~/.ssh/known_hosts 2>/dev/null || true + script: + - echo "=== Building and Deploying to AlexHost ===" + + # Build TeleShop image + - echo "Building TeleShop image..." + - docker build -t ${TELESHOP_IMAGE}:${CI_COMMIT_SHA} -t ${TELESHOP_IMAGE}:latest -f Dockerfile . + - docker save ${TELESHOP_IMAGE}:latest | gzip > teleshop-image.tar.gz + + # Build TeleBot image + - echo "Building TeleBot image..." + - docker build -t ${TELEBOT_IMAGE}:${CI_COMMIT_SHA} -t ${TELEBOT_IMAGE}:latest -f Dockerfile.telebot . + - docker save ${TELEBOT_IMAGE}:latest | gzip > telebot-image.tar.gz + + # Transfer images to AlexHost + - echo "Transferring images to AlexHost..." + - scp -o StrictHostKeyChecking=no teleshop-image.tar.gz ${ALEXHOST_USER}@${ALEXHOST_IP}:/tmp/ + - scp -o StrictHostKeyChecking=no telebot-image.tar.gz ${ALEXHOST_USER}@${ALEXHOST_IP}:/tmp/ + - scp -o StrictHostKeyChecking=no docker-compose.alexhost.yml ${ALEXHOST_USER}@${ALEXHOST_IP}:/tmp/ + + # Deploy on AlexHost + - | + ssh -o StrictHostKeyChecking=no ${ALEXHOST_USER}@${ALEXHOST_IP} << 'DEPLOY_EOF' + set -e + echo "=== Loading Docker images ===" + gunzip -c /tmp/teleshop-image.tar.gz | sudo docker load + gunzip -c /tmp/telebot-image.tar.gz | sudo docker load + + echo "=== Pushing to local registry ===" + sudo docker tag littleshop:latest localhost:5000/littleshop:latest + sudo docker push localhost:5000/littleshop:latest + sudo docker tag telebot:latest localhost:5000/telebot:latest + sudo docker push localhost:5000/telebot:latest + + echo "=== Stopping existing containers ===" + sudo docker stop teleshop telebot 2>/dev/null || true + sudo docker rm teleshop telebot 2>/dev/null || true + + echo "=== Starting new containers ===" + mkdir -p /home/sysadmin/teleshop-source + cp /tmp/docker-compose.alexhost.yml /home/sysadmin/teleshop-source/docker-compose.yml + cd /home/sysadmin/teleshop-source + sudo docker compose up -d + + echo "=== Waiting for health checks ===" + sleep 30 + sudo docker ps --format "table {{.Names}}\t{{.Status}}" + + echo "=== Cleanup ===" + rm -f /tmp/teleshop-image.tar.gz /tmp/telebot-image.tar.gz + echo "=== Deployment complete ===" + DEPLOY_EOF + after_script: + - rm -f teleshop-image.tar.gz telebot-image.tar.gz + environment: + name: production + url: https://teleshop.silentmary.mywire.org + tags: + - docker + +# Deploy only TeleShop +deploy-teleshop-only: + stage: deploy + image: docker:24.0 + services: + - docker:24.0-dind + rules: + - when: manual + variables: + DOCKER_TLS_CERTDIR: "/certs" + before_script: + - apk add --no-cache openssh-client curl tar gzip + - mkdir -p ~/.ssh + - echo "$ALEXHOST_SSH_KEY" > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan -H $ALEXHOST_IP >> ~/.ssh/known_hosts 2>/dev/null || true + script: + - echo "Building TeleShop image..." + - docker build -t ${TELESHOP_IMAGE}:latest -f Dockerfile . + - docker save ${TELESHOP_IMAGE}:latest | gzip > teleshop-image.tar.gz + - scp -o StrictHostKeyChecking=no teleshop-image.tar.gz ${ALEXHOST_USER}@${ALEXHOST_IP}:/tmp/ + - scp -o StrictHostKeyChecking=no docker-compose.alexhost.yml ${ALEXHOST_USER}@${ALEXHOST_IP}:/tmp/ + - | + ssh -o StrictHostKeyChecking=no ${ALEXHOST_USER}@${ALEXHOST_IP} << 'EOF' + gunzip -c /tmp/teleshop-image.tar.gz | sudo docker load + sudo docker tag littleshop:latest localhost:5000/littleshop:latest + sudo docker push localhost:5000/littleshop:latest + sudo docker stop teleshop 2>/dev/null || true + sudo docker rm teleshop 2>/dev/null || true + mkdir -p /home/sysadmin/teleshop-source + cp /tmp/docker-compose.alexhost.yml /home/sysadmin/teleshop-source/docker-compose.yml + cd /home/sysadmin/teleshop-source && sudo docker compose up -d teleshop + sleep 30 && sudo docker ps | grep teleshop + rm -f /tmp/teleshop-image.tar.gz + EOF + after_script: + - rm -f teleshop-image.tar.gz + environment: + name: production + url: https://teleshop.silentmary.mywire.org + tags: + - docker + +# Deploy only TeleBot +deploy-telebot-only: + stage: deploy + image: docker:24.0 + services: + - docker:24.0-dind + rules: + - when: manual + before_script: + - apk add --no-cache openssh-client curl tar gzip + - mkdir -p ~/.ssh + - echo "$ALEXHOST_SSH_KEY" > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan -H $ALEXHOST_IP >> ~/.ssh/known_hosts 2>/dev/null || true + script: + - echo "Building TeleBot image..." + - docker build -t ${TELEBOT_IMAGE}:latest -f Dockerfile.telebot . + - docker save ${TELEBOT_IMAGE}:latest | gzip > telebot-image.tar.gz + - scp -o StrictHostKeyChecking=no telebot-image.tar.gz ${ALEXHOST_USER}@${ALEXHOST_IP}:/tmp/ + - | + ssh -o StrictHostKeyChecking=no ${ALEXHOST_USER}@${ALEXHOST_IP} << 'EOF' + gunzip -c /tmp/telebot-image.tar.gz | sudo docker load + sudo docker tag telebot:latest localhost:5000/telebot:latest + sudo docker push localhost:5000/telebot:latest + sudo docker stop telebot 2>/dev/null || true + sudo docker rm telebot 2>/dev/null || true + cd /home/sysadmin/teleshop-source && sudo docker compose up -d telebot + sleep 20 && sudo docker ps | grep telebot + rm -f /tmp/telebot-image.tar.gz + EOF + after_script: + - rm -f telebot-image.tar.gz + environment: + name: production + tags: + - docker + +# Verify deployment status +verify-deployment: + stage: verify + image: alpine:latest + rules: + - when: manual + before_script: + - apk add --no-cache openssh-client curl + - mkdir -p ~/.ssh + - echo "$ALEXHOST_SSH_KEY" > ~/.ssh/id_rsa + - chmod 600 ~/.ssh/id_rsa + - ssh-keyscan -H $ALEXHOST_IP >> ~/.ssh/known_hosts 2>/dev/null || true + script: + - | + ssh -o StrictHostKeyChecking=no ${ALEXHOST_USER}@${ALEXHOST_IP} << 'EOF' + echo "=== Container Status ===" + sudo docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" + echo "" + echo "=== Health Checks ===" + curl -sf http://localhost:5100/health && echo " TeleShop: OK" || echo " TeleShop: FAIL" + echo "" + echo "=== Deployment verified ===" + EOF diff --git a/deploy-alexhost.sh b/deploy-alexhost.sh index 0b19fa8..f0e0ed8 100644 --- a/deploy-alexhost.sh +++ b/deploy-alexhost.sh @@ -171,7 +171,7 @@ deploy_telebot() { verify_deployment() { echo -e "${YELLOW}=== Verifying Deployment ===${NC}" - ssh_exec " + ssh_sudo " echo '' echo 'Container Status:' sudo docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}' | grep -E 'NAMES|teleshop|telebot'