Auto-commit before deployment Fri Sep 5 20:43:45 UTC 2025
This commit is contained in:
@@ -11,7 +11,7 @@ CONTAINER_NAME="portfolio-app"
|
||||
IMAGE_NAME="portfolio-app"
|
||||
PORT=3000
|
||||
BACKUP_PORT=3001
|
||||
LOG_FILE="/var/log/portfolio-deploy.log"
|
||||
LOG_FILE="./logs/portfolio-deploy.log"
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
@@ -44,7 +44,7 @@ if [[ $EUID -eq 0 ]]; then
|
||||
fi
|
||||
|
||||
# Check if Docker is running
|
||||
if ! docker info > /dev/null 2>&1; then
|
||||
if ! sudo docker info > /dev/null 2>&1; then
|
||||
error "Docker is not running. Please start Docker and try again."
|
||||
exit 1
|
||||
fi
|
||||
@@ -107,14 +107,14 @@ log "🐳 Step 3: Docker operations..."
|
||||
|
||||
# Build Docker image
|
||||
log "🏗️ Building Docker image..."
|
||||
docker build -t "$IMAGE_NAME:latest" . || {
|
||||
sudo docker build -t "$IMAGE_NAME:latest" . || {
|
||||
error "Docker build failed"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Tag with timestamp
|
||||
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
|
||||
docker tag "$IMAGE_NAME:latest" "$IMAGE_NAME:$TIMESTAMP"
|
||||
sudo docker tag "$IMAGE_NAME:latest" "$IMAGE_NAME:$TIMESTAMP"
|
||||
|
||||
success "✅ Docker image built successfully"
|
||||
|
||||
@@ -122,10 +122,10 @@ success "✅ Docker image built successfully"
|
||||
log "🚀 Step 4: Deploying application..."
|
||||
|
||||
# Check if container is running
|
||||
if [ "$(docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null)" = "true" ]; then
|
||||
if [ "$(sudo docker inspect -f '{{.State.Running}}' "$CONTAINER_NAME" 2>/dev/null)" = "true" ]; then
|
||||
log "📦 Stopping existing container..."
|
||||
docker stop "$CONTAINER_NAME" || true
|
||||
docker rm "$CONTAINER_NAME" || true
|
||||
sudo docker stop "$CONTAINER_NAME" || true
|
||||
sudo docker rm "$CONTAINER_NAME" || true
|
||||
fi
|
||||
|
||||
# Check if port is available
|
||||
@@ -138,7 +138,7 @@ fi
|
||||
|
||||
# Start new container
|
||||
log "🚀 Starting new container on port $DEPLOY_PORT..."
|
||||
docker run -d \
|
||||
sudo docker run -d \
|
||||
--name "$CONTAINER_NAME" \
|
||||
--restart unless-stopped \
|
||||
-p "$DEPLOY_PORT:3000" \
|
||||
@@ -189,22 +189,22 @@ fi
|
||||
|
||||
# Show container status
|
||||
log "📊 Container status:"
|
||||
docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
|
||||
sudo docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
|
||||
|
||||
# Show resource usage
|
||||
log "📈 Resource usage:"
|
||||
docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" "$CONTAINER_NAME"
|
||||
sudo docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}" "$CONTAINER_NAME"
|
||||
|
||||
# Step 6: Cleanup
|
||||
log "🧹 Step 6: Cleaning up old images..."
|
||||
|
||||
# Remove old images (keep last 3 versions)
|
||||
docker images "$IMAGE_NAME" --format "table {{.Tag}}\t{{.ID}}" | tail -n +2 | head -n -3 | awk '{print $2}' | xargs -r docker rmi || {
|
||||
sudo docker images "$IMAGE_NAME" --format "table {{.Tag}}\t{{.ID}}" | tail -n +2 | head -n -3 | awk '{print $2}' | xargs -r sudo docker rmi || {
|
||||
warning "No old images to remove"
|
||||
}
|
||||
|
||||
# Clean up unused Docker resources
|
||||
docker system prune -f --volumes || {
|
||||
sudo docker system prune -f --volumes || {
|
||||
warning "Failed to clean up Docker resources"
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ success "🎉 Deployment completed successfully!"
|
||||
log "🌐 Application is available at: http://localhost:$DEPLOY_PORT"
|
||||
log "🏥 Health check endpoint: http://localhost:$DEPLOY_PORT/api/health"
|
||||
log "📊 Container name: $CONTAINER_NAME"
|
||||
log "📝 Logs: docker logs $CONTAINER_NAME"
|
||||
log "📝 Logs: sudo docker logs $CONTAINER_NAME"
|
||||
|
||||
# Update deployment log
|
||||
echo "$(date): Deployment successful - Port: $DEPLOY_PORT - Image: $IMAGE_NAME:$TIMESTAMP" >> "$LOG_FILE"
|
||||
|
||||
Reference in New Issue
Block a user