"CI-CD-Add-TeleBot-build-and-deployment"

This commit is contained in:
sysadmin 2025-10-03 16:00:05 +01:00
parent 8fb8c56d9b
commit e0c431cbae

View File

@ -5,7 +5,7 @@ stages:
variables: variables:
DOCKER_HOST: unix:///var/run/docker.sock DOCKER_HOST: unix:///var/run/docker.sock
build: build:littleshop:
stage: build stage: build
image: docker:24 image: docker:24
script: script:
@ -16,7 +16,25 @@ build:
echo "Tagging as version $CI_COMMIT_TAG" echo "Tagging as version $CI_COMMIT_TAG"
docker tag localhost:5000/littleshop:latest localhost:5000/littleshop:$CI_COMMIT_TAG docker tag localhost:5000/littleshop:latest localhost:5000/littleshop:$CI_COMMIT_TAG
fi fi
- echo "Build complete" - echo "LittleShop build complete"
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_COMMIT_TAG'
tags:
- docker
build:telebot:
stage: build
image: docker:24
script:
- echo "Building TeleBot Docker image"
- docker build -t localhost:5000/telebot:latest -f Dockerfile.telebot .
- |
if [ -n "$CI_COMMIT_TAG" ]; then
echo "Tagging as version $CI_COMMIT_TAG"
docker tag localhost:5000/telebot:latest localhost:5000/telebot:$CI_COMMIT_TAG
fi
- echo "TeleBot build complete"
rules: rules:
- if: '$CI_COMMIT_BRANCH == "main"' - if: '$CI_COMMIT_BRANCH == "main"'
- if: '$CI_COMMIT_TAG' - if: '$CI_COMMIT_TAG'
@ -36,11 +54,13 @@ deploy:vps:
script: script:
- export VERSION="${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}" - export VERSION="${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}"
- echo "Deploying version $VERSION to VPS" - echo "Deploying version $VERSION to VPS"
- echo "Building image from source..." - echo "Building images from source..."
- docker build -t littleshop:$VERSION . - docker build -t littleshop:$VERSION .
- docker build -t telebot:$VERSION -f Dockerfile.telebot .
- echo "Copying image to VPS via SSH..." - echo "Copying images to VPS via SSH..."
- docker save littleshop:$VERSION | ssh -i /tmp/deploy_key -p "$VPS_PORT" "$VPS_USER@$VPS_HOST" "docker load" - docker save littleshop:$VERSION | ssh -i /tmp/deploy_key -p "$VPS_PORT" "$VPS_USER@$VPS_HOST" "docker load"
- docker save telebot:$VERSION | ssh -i /tmp/deploy_key -p "$VPS_PORT" "$VPS_USER@$VPS_HOST" "docker load"
- echo "Deploying on VPS..." - echo "Deploying on VPS..."
- | - |
@ -48,14 +68,18 @@ deploy:vps:
set -e set -e
export VERSION="$VERSION" export VERSION="$VERSION"
# Tag the image # Tag the images
docker tag littleshop:\$VERSION localhost:5000/littleshop:\$VERSION docker tag littleshop:\$VERSION localhost:5000/littleshop:\$VERSION
docker tag littleshop:\$VERSION localhost:5000/littleshop:latest docker tag littleshop:\$VERSION localhost:5000/littleshop:latest
docker tag telebot:\$VERSION localhost:5000/telebot:\$VERSION
docker tag telebot:\$VERSION localhost:5000/telebot:latest
# Push to local registry # Push to local registry
echo "Pushing to local Docker registry..." echo "Pushing images to local Docker registry..."
docker push localhost:5000/littleshop:\$VERSION docker push localhost:5000/littleshop:\$VERSION
docker push localhost:5000/littleshop:latest docker push localhost:5000/littleshop:latest
docker push localhost:5000/telebot:\$VERSION
docker push localhost:5000/telebot:latest
# Navigate to deployment directory # Navigate to deployment directory
cd /opt/littleshop cd /opt/littleshop
@ -65,6 +89,11 @@ deploy:vps:
docker stop \$(docker ps -q --filter "name=littleshop") 2>/dev/null || true docker stop \$(docker ps -q --filter "name=littleshop") 2>/dev/null || true
docker rm \$(docker ps -aq --filter "name=littleshop") 2>/dev/null || true docker rm \$(docker ps -aq --filter "name=littleshop") 2>/dev/null || true
# Stop TeleBot container
echo "Stopping TeleBot container..."
docker stop telebot-service 2>/dev/null || true
docker rm telebot-service 2>/dev/null || true
# Stop services with compose (removes networks) # Stop services with compose (removes networks)
echo "Stopping compose services..." echo "Stopping compose services..."
docker-compose down --remove-orphans || true docker-compose down --remove-orphans || true
@ -73,10 +102,20 @@ deploy:vps:
echo "Cleaning up Docker networks..." echo "Cleaning up Docker networks..."
docker network prune -f || true docker network prune -f || true
# Start services with new image # Start services with new images
echo "Starting services with new image..." echo "Starting services with new images..."
docker-compose up -d docker-compose up -d
# Start TeleBot with new image
echo "Starting TeleBot with new image..."
docker run -d \
--name telebot-service \
--restart unless-stopped \
--network silverpay_silverpay-network \
--network littleshop-network \
-e ASPNETCORE_URLS=http://+:5010 \
localhost:5000/telebot:latest
# Wait for startup # Wait for startup
echo "Waiting for services to start..." echo "Waiting for services to start..."
sleep 30 sleep 30
@ -94,6 +133,7 @@ deploy:vps:
echo "❌ Health check failed after deployment" echo "❌ Health check failed after deployment"
docker logs littleshop-admin --tail 50 docker logs littleshop-admin --tail 50
docker logs telebot-service --tail 30
exit 1 exit 1
EOF EOF
environment: environment: