-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·121 lines (100 loc) · 3.16 KB
/
deploy.sh
File metadata and controls
executable file
·121 lines (100 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash
set -euo pipefail
# Deploy script with health check and rollback capability
# Usage: ./deploy.sh <image_tag>
# Example: ./deploy.sh your-dockerhub-username/deploy-demo-app:abc123
IMAGE="${1:-}"
if [ -z "$IMAGE" ]; then
echo "Error: Image tag required"
echo "Usage: $0 <image_tag>"
exit 1
fi
# Configuration
COMPOSE_FILE="docker-compose.yml"
ENV_FILE=".env"
PREV_IMAGE_FILE="prev_image.txt"
DEPLOY_DIR="${DEPLOY_DIR:-$(pwd)}"
DOMAIN="${DOMAIN:-example.com}"
HEALTH_CHECK_URL="https://${DOMAIN}/healthz"
HEALTH_CHECK_TIMEOUT=30
HEALTH_CHECK_INTERVAL=3
cd "$DEPLOY_DIR" || exit 1
echo "=========================================="
echo "Deploying: $IMAGE"
echo "=========================================="
# Load current environment
if [ -f "$ENV_FILE" ]; then
source "$ENV_FILE"
fi
# Save previous image for rollback
if [ -f "$ENV_FILE" ] && grep -q "APP_IMAGE=" "$ENV_FILE"; then
PREV_IMAGE=$(grep "APP_IMAGE=" "$ENV_FILE" | cut -d '=' -f2)
echo "$PREV_IMAGE" > "$PREV_IMAGE_FILE"
echo "Previous image saved: $PREV_IMAGE"
else
echo "Warning: No previous image found. Rollback may not work."
fi
# Pull new image
echo "Pulling image: $IMAGE"
docker pull "$IMAGE" || {
echo "Error: Failed to pull image $IMAGE"
exit 1
}
# Update .env file with new image
if [ -f "$ENV_FILE" ]; then
if grep -q "APP_IMAGE=" "$ENV_FILE"; then
sed -i "s|APP_IMAGE=.*|APP_IMAGE=$IMAGE|" "$ENV_FILE"
else
echo "APP_IMAGE=$IMAGE" >> "$ENV_FILE"
fi
else
echo "APP_IMAGE=$IMAGE" > "$ENV_FILE"
fi
# Update compose and restart services
echo "Updating containers..."
docker compose pull web || true
docker compose up -d web
# Wait for container to be ready
echo "Waiting for container to start..."
sleep 5
# Health check with retries
echo "Running health check on $HEALTH_CHECK_URL"
HEALTH_CHECK_PASSED=false
ELAPSED=0
while [ $ELAPSED -lt $HEALTH_CHECK_TIMEOUT ]; do
if curl -fsS --max-time 5 "$HEALTH_CHECK_URL" > /dev/null 2>&1; then
HEALTH_CHECK_PASSED=true
break
fi
echo "Health check failed, retrying in ${HEALTH_CHECK_INTERVAL}s... (${ELAPSED}/${HEALTH_CHECK_TIMEOUT}s)"
sleep $HEALTH_CHECK_INTERVAL
ELAPSED=$((ELAPSED + HEALTH_CHECK_INTERVAL))
done
if [ "$HEALTH_CHECK_PASSED" = true ]; then
echo "=========================================="
echo "✓ Deployment successful!"
echo "✓ Health check passed"
echo "=========================================="
# Save successful image
echo "$IMAGE" > "$PREV_IMAGE_FILE"
exit 0
else
echo "=========================================="
echo "✗ Health check failed!"
echo "Rolling back to previous image..."
echo "=========================================="
# Rollback
if [ -f "$PREV_IMAGE_FILE" ] && [ -n "$(cat "$PREV_IMAGE_FILE")" ]; then
ROLLBACK_IMAGE=$(cat "$PREV_IMAGE_FILE")
echo "Rolling back to: $ROLLBACK_IMAGE"
# Update .env with previous image
sed -i "s|APP_IMAGE=.*|APP_IMAGE=$ROLLBACK_IMAGE|" "$ENV_FILE"
# Restart with previous image
docker compose pull web || true
docker compose up -d web
echo "Rollback complete. Previous image restored."
else
echo "Error: Cannot rollback - no previous image found"
fi
exit 1
fi