-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbuild-release.sh
executable file
·134 lines (119 loc) · 3.58 KB
/
build-release.sh
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
set -o errexit
set -o nounset
set -o pipefail
function main() {
# Parse arguments
if [[ "$#" -ne 1 && "$#" -ne 2 && "$#" -ne 3 ]]; then
usage
exit 1
fi
local -r new_version=${1-}
DRY_RUN=true
if [[ "${2-}" == "--no-dry-run" ]]; then
echo "!!! This NOT is a dry run."
DRY_RUN=false
else
echo "This is a dry run."
fi
INTERACTIVE=true
if [[ "${3-}" == "-y" ]]; then
INTERACTIVE=false
fi
# Get and verify version info
local -r version_regex="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$"
if [[ "${new_version}" =~ $version_regex ]]; then
local -r version_major="${BASH_REMATCH[1]}"
local -r version_minor="${BASH_REMATCH[2]}"
local -r version_patch="${BASH_REMATCH[3]}"
else
usage
echo
echo "!!! You specified an invalid version '${new_version}'."
exit 1
fi
# Interactive questions
local container_name="example-api"
if [ $INTERACTIVE == true ]; then
read -p "Container name (default: example-api): " container_name
[ -z "$container_name" ] && container_name="example-api"
fi
local -r github=`git config --get remote.origin.url`
declare -r temp_dir=$(mktemp -d "/tmp/${container_name}-${new_version}.XXXX")
local -r version_tag="registry.local/${container_name}:${new_version}"
local -r latest_tag="registry.local/${container_name}:latest"
git-clone "${github}" "${temp_dir}"
git-checkout "${new_version}" "${temp_dir}"
docker-build "${version_tag}" "${latest_tag}" "${temp_dir}"
docker-push "${version_tag}" "${latest_tag}"
docker-cleanup "${version_tag}" "${latest_tag}"
rm -Rf "${temp_dir}"
}
function usage() {
echo "Usage: ${0} <release_version> [--no-dry-run] [-y]"
echo
echo "<release_version> is the version you want to release,"
echo "--no-dry-run flag turns own the dry run mode and executes real commands"
echo "-y flag turns own interactive mode (no questions asked, uses default values)"
echo
echo "Please see docs/releasing.md for more info."
}
function git-clone() {
local -r github="${1}"
local -r dest="${2}"
echo "Cloning from '${github}' to '${dest}'..."
if $DRY_RUN; then
echo "Dry run: would have done git clone ${github} ${dest}"
else
git clone "${github}" "${dest}"
fi
}
function git-checkout() {
local -r tag="${1}"
local -r dir="${2}"
echo "Checking out tag '${tag}'..."
if $DRY_RUN; then
echo "Dry run: would have done cd ${dir} ; git checkout -b ${tag} ${tag}"
else
(cd ${dir} ; git checkout -b "${tag}" "${tag}")
fi
}
function docker-build() {
local -r version_tag="${1}"
local -r latest_tag="${2}"
local -r dir="${3}"
echo "Building docker container '${version_tag}'..."
if $DRY_RUN; then
echo "Dry run: would have done docker build -t ${version_tag} -t ${latest_tag} ${dir}"
else
docker build -t "${version_tag}" -t "${latest_tag}" "${dir}"
fi
}
function docker-push() {
local -r version_tag="${1}"
local -r latest_tag="${2}"
echo "Pushing '${version_tag}', '${version_tag}' tags to registry..."
if $DRY_RUN; then
echo "Dry run: would have done "
echo "docker push ${version_tag}"
echo "docker push ${latest_tag}"
else
docker push "${version_tag}"
docker push "${latest_tag}"
fi
}
function docker-cleanup() {
local -r version_tag="${1}"
local -r latest_tag="${2}"
echo "Docker cleanup..."
if $DRY_RUN; then
echo "Dry run: would have done "
echo "docker rmi ${version_tag}"
echo "docker rmi ${latest_tag}"
else
docker rmi "${version_tag}"
docker rmi "${latest_tag}"
docker rmi -f $(docker images -q -f "dangling=true") || true
fi
}
main "$@"