This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish.sh
executable file
·109 lines (84 loc) · 2.18 KB
/
publish.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
#!/bin/bash
function run {(
set -e # `validate` might throw from subshell, bail out if so
version="${1}"
validate "${version}"
curr=$(get_current_version)
replace "s/FROM node:${curr}/FROM node:${version}-buster/" "$(root)/Dockerfile"
tag="uber/web-base-image:${version}-buster"
info "Building ${tag}"
if ! docker build -q -t "${tag}" $(root)
then
throw "Failed to build image ${tag}"
fi
info "Publishing ${tag}"
if ! docker push -q "${tag}"
then
throw "Failed to push image ${tag}.\nCheck that you have publish permissions"
fi
)}
function get_current_version {(
grep "FROM node:" "$(root)/Dockerfile" | awk '{print $2}' | sed -E 's/node://g'
)}
function validate {(
version="${1}"
validate-arg "${version}"
validate-docker-hub "${version}"
validate-docker-daemon
)}
function validate-arg {(
version="${1}"
if [ -z "${version}" ]
then
throw "No version specified. Example usage: ./upgrade-node.sh 14.18.0"
fi
)}
function validate-docker-hub {(
version="${1}"
page=1
while true
do
# paginate through JSON API until we find either `"name":"${version}"` (found) or `"next":null` (not found)
json=$(curl -s "https://hub.docker.com/v2/repositories/uber/web-base-image/tags?n=100&page=${page}")
if echo "${json}" | grep -q "\"name\":\"${version}\""
then
throw "Image with version ${version} already exists"
elif echo "${json}" | grep -q "\"next\":null"
then
break
else
((page++))
fi
done
)}
function validate-docker-daemon {(
# validate docker daemon is running so that build and push commands work
if ! docker ps >/dev/null 2>/dev/null
then
throw "Docker Desktop is not running"
fi
)}
# utils
function root {(
cd "$(dirname "${BASH_SOURCE[0]}")" && pwd
)}
function info {(
green="\033[1;32m"
end="\033[0m"
echo -e "${green}${1}${end}" 1>&2
)}
function throw {(
red="\033[1;31m"
end="\033[0m"
echo -e "${red}Error: ${1}${end}" 1>&2
exit 1
)}
function replace {(
replace="${1}"
file="${2}"
case "$(uname -s)" in
Darwin*) sed -Ei '' "${replace}" "${file}";; # mac sed needs empty arg after -i
*) sed -Ei "${replace}" "${file}";;
esac
)}
run "${1}"