-
Notifications
You must be signed in to change notification settings - Fork 14
/
publish-version
executable file
·87 lines (72 loc) · 2.79 KB
/
publish-version
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
#!/bin/bash
# SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
#
# SPDX-License-Identifier: Apache-2.0
# Poor-man's tool to tag a release by the following:
# 1) build the docker image via 'make images'
# 2) tag the current commit with the specified version tag
# 3) push the docker image(s) up with the specified version tag
# The tool requires the user to have the authorization to:
# 1) push tag to the upstream repo
# 2) publish docker image to dockerhub
# Also, the following conditions must be met:
# 1) repo must have upstream (or origin) remote push enabled via ssh
# 2) git tag does not already exist
# 3) non-empty version number is specified
# 4) user is logged in to dockerhub
# 5) following the build all images must exist in the local docker image cache
set -e -u -o pipefail
dry_run=${DRY_RUN:-"0"}
version="${1}"
MAKEFILE=${MAKEFILE:-"Makefile"}
shift 1
# Validate args...
[ -z "$version" ] && echo "usage: $(dirname $0) <version> [<docker-image>...]" >&2 && exit 1
# Validate upstream (or origin) push URL is via SSH...
if [ "${dry_run}" == "0" ]; then
git remote -v | grep upstream | grep -q push && upstream="upstream" || upstream="origin"
! git remote -v | grep $upstream | grep push | cut -f2 | cut -d\ -f1 | grep -q 'git@github.com:' && \
echo "Upstream remote not setup with SSH push URL" >&2 && exit 1
fi
! git status -b | grep -q "nothing to commit, working tree clean" && \
echo "There are modified files" >&2 && git status && exit 1
# Validate tag does not already exist
git tag | grep -q $version && echo "Tag $version already exists" >&2 && exit 1
if [ $# -gt 0 ]; then
if [ "${dry_run}" == "0" ]; then
# Validate docker logins...
! egrep -q '"auths": {$' ~/.docker/config.json && echo "Not logged in to dockerhub!" >&2 && exit 1
fi
# 1) Make images
make -f $MAKEFILE images || exit 1
# 1a) Validate that each docker image exists
for image in "$@"; do
id=$(docker images $image:latest -q)
[ -z "$id" ] && echo "Docker image $image:latest does not exist!" && exit 1
done
else
make -f $MAKEFILE || exit 1
fi
# 2) Push the specified version tag to upstream (or origin) repo
echo "Tagging sources with $version..."
git tag $version
if [ "${dry_run}" == "0" ]; then
echo "Pushing $version tag to $upstream..."
git push $upstream $version
else
echo "Skipping tag push of $version"
fi
docker_tag=${version/[a-z-_]*\//}
if [ $# -gt 0 ]; then
# 3) Push each docker image to docker hub; assumed docker login was already run
for image in "$@"; do
echo "Publishing image $image..."
id=$(docker images $image:latest -q)
docker tag $id $image:$docker_tag
if [ "${dry_run}" == "0" ]; then
docker push $image:$docker_tag
else
echo "Skipping push of image $image"
fi
done
fi