Files
configucci/deploy-docker.sh

153 lines
3.9 KiB
Bash

#!/bin/bash
# Configucci Docker Deployment Script
# Usage: ./deploy-docker.sh [server]
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
SERVER="${1:-}"
APP_NAME="configucci"
GIT_BRANCH="main"
echo -e "${GREEN}==================================${NC}"
echo -e "${GREEN} Configucci Docker Deployment ${NC}"
echo -e "${GREEN}==================================${NC}"
# Function to deploy locally
deploy_local() {
echo -e "${YELLOW}Building Docker image...${NC}"
docker-compose build --no-cache
echo -e "${YELLOW}Stopping existing container (if any)...${NC}"
docker-compose down || true
echo -e "${YELLOW}Starting new container...${NC}"
docker-compose up -d
echo -e "${YELLOW}Waiting for application to start...${NC}"
sleep 5
echo -e "${GREEN}==================================${NC}"
echo -e "${GREEN} Deployment Complete! ${NC}"
echo -e "${GREEN}==================================${NC}"
echo ""
echo -e "Application URL: ${GREEN}http://localhost${NC}"
echo ""
echo -e "Useful commands:"
echo -e " View logs: ${YELLOW}docker-compose logs -f${NC}"
echo -e " Stop app: ${YELLOW}docker-compose down${NC}"
echo -e " Restart app: ${YELLOW}docker-compose restart${NC}"
echo -e " Rebuild: ${YELLOW}docker-compose build && docker-compose up -d${NC}"
}
# Function to deploy to remote server
deploy_remote() {
echo -e "${YELLOW}Deploying to server: ${SERVER}${NC}"
# Check if server is accessible
if ! ping -c 1 "$SERVER" &> /dev/null; then
echo -e "${RED}Cannot connect to server: ${SERVER}${NC}"
exit 1
fi
# Create deployment script on server
cat << 'EOF' > /tmp/deploy-configucci.sh
#!/bin/bash
set -e
APP_DIR="/opt/configucci"
GIT_URL="https://git.six83.ru/ssa/configucci.git"
GIT_BRANCH="main"
echo "Creating app directory..."
mkdir -p \$APP_DIR
cd \$APP_DIR
echo "Pulling latest code..."
if [ -d ".git" ]; then
git pull origin \$GIT_BRANCH
else
git clone --depth 1 --branch \$GIT_BRANCH \$GIT_URL .
fi
echo "Building Docker image..."
docker-compose build --no-cache
echo "Stopping existing container..."
docker-compose down || true
echo "Starting new container..."
docker-compose up -d
echo "Cleaning up old images..."
docker image prune -f
echo "Deployment complete!"
docker-compose ps
EOF
# Copy docker-compose.yml to server
echo -e "${YELLOW}Copying files to server...${NC}"
scp docker-compose.yml Dockerfile nginx.conf "$SERVER:/tmp/"
# Execute deployment on server
echo -e "${YELLOW}Executing deployment on server...${NC}"
ssh "$SERVER" << 'ENDSSH'
#!/bin/bash
set -e
APP_DIR="/opt/configucci"
cd $APP_DIR
# Copy files from /tmp
cp /tmp/docker-compose.yml .
cp /tmp/Dockerfile .
cp /tmp/nginx.conf .
# Build and deploy
docker-compose build --no-cache
docker-compose down || true
docker-compose up -d
# Cleanup
docker image prune -f
docker-compose ps
ENDSSH
echo -e "${GREEN}==================================${NC}"
echo -e "${GREEN} Remote Deployment Complete! ${NC}"
echo -e "${GREEN}==================================${NC}"
echo ""
echo -e "Server: ${GREEN}http://${SERVER}${NC}"
echo ""
echo -e "Useful commands:"
echo -e " SSH to server: ${YELLOW}ssh ${SERVER}${NC}"
echo -e " View logs: ${YELLOW}ssh ${SERVER} 'docker-compose logs -f'${NC}"
echo -e " Stop app: ${YELLOW}ssh ${SERVER} 'docker-compose down'${NC}"
}
# Check if Docker is available
if ! command -v docker &> /dev/null; then
echo -e "${RED}Docker is not installed or not in PATH${NC}"
exit 1
fi
if ! command -v docker-compose &> /dev/null; then
echo -e "${RED}docker-compose is not installed or not in PATH${NC}"
exit 1
fi
# Deploy
if [ -z "$SERVER" ]; then
deploy_local
else
deploy_remote
fi