Skip to content

Commit 6da1953

Browse files
committed
add Docker Hub post_push hook
1 parent 4a8eb4a commit 6da1953

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

hooks/post_push

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
#
3+
# Docker Hub Autobuild post push hook
4+
#
5+
# this is supposed to be used in conjunction with an autobuild rule like this:
6+
# - Source Type Tag
7+
# - Source /^v([0-9]+)\.([0-9]+)\.([0-9]+)/
8+
# - Docker Tag {\1}.{\2}.{\3}
9+
# - Build caching disabled
10+
#
11+
12+
echo "---> post_hook running on SOURCE_BRANCH=${SOURCE_BRANCH} with DOCKER_TAG=${DOCKER_TAG}"
13+
14+
SEMVER='(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)'
15+
PRERELEASE='(-([0-9a-zA-Z]*))?' # OK so far
16+
BUILDMETADATA='(\+(.*))?'
17+
RE="^v${SEMVER}${PRERELEASE}${BUILDMETADATA}$"
18+
19+
if [[ ${SOURCE_BRANCH} =~ ${RE} ]]; then
20+
echo '---> semver detected, adding release tags'
21+
MAJOR="${BASH_REMATCH[1]}"
22+
MINOR="${BASH_REMATCH[2]}"
23+
PATCH="${BASH_REMATCH[3]}"
24+
PRERELEASE="${BASH_REMATCH[5]}"
25+
BUILD="${BASH_REMATCH[7]}"
26+
27+
# prepare tags up to the patch level
28+
TAGS="latest ${MAJOR} ${MAJOR}.${MINOR} ${MAJOR}.${MINOR}.${PATCH}"
29+
30+
# ignore PRERELASE, but add tag for builds
31+
if [[ ! -z "${BUILD}" ]]; then
32+
# '+' is not allowed in Docker tags so we use an underscore
33+
TAGS="${TAGS} ${MAJOR}.${MINOR}.${PATCH}_${BUILD}"
34+
fi
35+
36+
echo "---> tag list: ${TAGS}"
37+
for TAG in ${TAGS}; do
38+
# avoid re-tagging the same tag already assigned by Docker autobuild rules
39+
if [[ "${TAG}" != "${DOCKER_TAG}" ]]; then
40+
echo "---> tagging ${TAG}"
41+
docker tag ${IMAGE_NAME} ${DOCKER_REPO}:${TAG}
42+
docker push ${DOCKER_REPO}:${TAG}
43+
else
44+
echo "---> skipping tag ${TAG}"
45+
fi
46+
done
47+
48+
fi

0 commit comments

Comments
 (0)