-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerhub.bash
65 lines (53 loc) · 3.04 KB
/
dockerhub.bash
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
# This file contains functionality for docker registries, and some of it may assume the registry is dockerhub
if ! [ -d "$BASE_DIR" ]; then
exit_with_error_message "\$BASE_DIR should already be defined before this was included at ${BASH_SOURCE[1]}:${BASH_LINENO}"
fi
. "$BASE_DIR/lib/bash_include/config.bash"
# making sure publish fails if version.txt has not been updated to avoid re-using the version tag:
function assert_main_version_unused {
if main_version_already_published; then
exit_with_error_message "Version $MAIN_VERSION has already been published, and refers to a different image. Something must have changed, perhaps a newer version of $BASE_IMAGE (BASE_IMAGE). Please edit version.txt in $BASE_GITHUB_URL according to semver and try again to publish these changes. There are more details in the playbook at https://docs.google.com/document/d/1vJz6hXdjAu8LBAfIS4oMy7yLIUX177gU1H7BxTPPzAU/edit#heading=h.8gg7zdig7ttm ."
fi
}
function main_version_already_published {
docker_image_has_been_published "hub.docker.com" "$MAIN_IMAGE_FULLNAME" "$MAIN_VERSION"
}
function docker_image_has_been_published {
local REGISTRY="$1"
local IMAGE_SCOPED_NAME="$2"
local TAG="$3"
max_attempts=3
attempt_number=1
until docker_image_has_been_published_unsafe "$REGISTRY" "$IMAGE_SCOPED_NAME" "latest"; do
if [ "$attempt_number" -gt "$max_attempts" ]; then
exit_with_error_message "FAILED to verify that image \"$IMAGE_SCOPED_NAME:latest\" has been previously published, which probably just means we cannot reach the registry ( $REGISTRY/ )."
else
echo "It seems like we cannot reach the docker registry right now (attempt $attempt_number), will try again shortly..."
sleep 5
fi
attempt_number="$(($attempt_number + 1))"
done
docker_image_has_been_published_unsafe "$REGISTRY" "$IMAGE_SCOPED_NAME" "$TAG" #un-explicitly sets the return value
}
function docker_image_has_been_published_unsafe {
local REGISTRY="$1"
local IMAGE_SCOPED_NAME="$2"
local TAG="$3"
TAG_TEST_URL="https://$REGISTRY/v2/repositories/$IMAGE_SCOPED_NAME/tags/$TAG/"
HTTP_CODE="$(curl -X "HEAD" -L -s -w "%{http_code}" "$TAG_TEST_URL" -m 2 )"
[ "200" == "$HTTP_CODE" ] # no explicit return or if statement needed
}
function docker_force_pull {
local IMAGE="$1"
echo "Forcefully pulling a fresh version of the docker image \"$IMAGE\":" >&2
if docker inspect $IMAGE --format '.' >/dev/null 2>&1; then # if IMAGE exists
docker tag $IMAGE temp || { echo "ERROR: FAILED at $BASH_SOURCE:$LINENO" >&2; return 1; }
docker rmi $IMAGE || { echo "ERROR: FAILED at $BASH_SOURCE:$LINENO" >&2; return 1; }
fi
docker pull $IMAGE || { echo "ERROR: FAILED at $BASH_SOURCE:$LINENO" >&2; return 1; }
echo "...got a fresh version of the docker image \"$IMAGE\"." >&2
if docker inspect temp --format '.' >/dev/null 2>&1; then # if temp image exists
docker rmi temp || { echo "ERROR: FAILED at $BASH_SOURCE:$LINENO" >&2; return 1; }
fi
return 0
}