File size: 1,146 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail

### Expected binaries
# - curl

# Sanitize parameters
build="${1:-}"

if [[ -z "$build" ]]; then
	echo "Usage: ${0} <build-number>"
	echo ""
	echo "Example: ${0} 1234"
	echo ""
	echo "It will hit https://calypso.live?image=registry.a8c.com/calypso/app:build-<build-number> and get"
	echo "and output the corresponding https://<container-name>.calypso.live URL".
	exit 1
fi

IMAGE_URL="https://calypso.live?image=registry.a8c.com/calypso/app:build-${build}";
MAX_LOOP=10
COUNTER=0

# Transform an URL like https://calypso.live?image=... into https://<container>.calypso.live
while [[ $COUNTER -le $MAX_LOOP ]]; do
	COUNTER=$((COUNTER+1))
	REDIRECT=$(curl --output /dev/null --silent --show-error  --write-out "%{http_code} %{redirect_url}" "${IMAGE_URL}")
	read -r HTTP_STATUS URL <<< "${REDIRECT}"

	# 202 means the image is being downloaded, retry in a few seconds
	if [[ "${HTTP_STATUS}" -eq "202" ]]; then
		sleep 5
		continue
	fi

	break
done

if [[ -z "$URL" ]]; then
	echo "Can't redirect to ${IMAGE_URL}" >&2
	echo "Curl response: ${REDIRECT}" >&2
	exit 1
fi

echo "$URL"