Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·53 lines (45 loc) · 1.9 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·53 lines (45 loc) · 1.9 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
#!/usr/bin/env bash
# Deploys a published image of the hub and goes back to the previous one when the new one does not become healthy.
#
# scripts/deploy.sh # the newest image (the `latest` tag)
# scripts/deploy.sh sha-1a2b3c4 # a given tag: also how you roll back by hand
#
# It works in gate/ (next to docker-compose.yml) wherever it is started from; run it as a user that may use Docker.
# Each deployment prints the image it replaced.
set -euo pipefail
cd "$(dirname "$0")/../gate"
tag="${1:-latest}"
service="gate"
container="mindgate-gate"
image="ghcr.io/prod-explore/mindgate-hub"
wait_seconds="${DEPLOY_WAIT_SECONDS:-90}"
previous_id="$(docker inspect --format '{{.Image}}' "$container" 2>/dev/null || true)"
previous_ref="$(docker inspect --format '{{.Config.Image}}' "$container" 2>/dev/null || true)"
healthy() {
local deadline=$((SECONDS + wait_seconds))
while ((SECONDS < deadline)); do
case "$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container" 2>/dev/null)" in
healthy) return 0 ;;
unhealthy | exited | dead) return 1 ;;
esac
sleep 2
done
return 1
}
echo "Pulling ${image}:${tag}"
MINDGATE_IMAGE_TAG="$tag" docker compose pull "$service"
echo "Starting it (was: ${previous_ref:-nothing})"
MINDGATE_IMAGE_TAG="$tag" docker compose up -d --no-deps "$service"
if healthy; then
echo "${container} is healthy on ${image}:${tag}"
exit 0
fi
echo "${container} did not become healthy within ${wait_seconds}s" >&2
docker logs --tail 20 "$container" >&2 || true
if [[ -n "$previous_id" ]]; then
echo "Going back to the previous image (${previous_id#sha256:})" >&2
docker tag "$previous_id" "${image}:rollback"
MINDGATE_IMAGE_TAG=rollback docker compose up -d --no-deps "$service"
healthy && echo "Rolled back; ${container} is healthy again" >&2 || echo "The rollback is not healthy either" >&2
fi
exit 1