This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·178 lines (141 loc) · 4.54 KB
/
run.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
set -euo pipefail
function error() {
printf "\033[31m%-8s\033[0m %s\n" "error" "$@" >&2
exit 1
}
function export-to-file() {
if [[ "$#" -ne 2 ]]; then
error "Arguments mismatch: required 2, received $#."
fi
if [[ -z "$1" || -z "$2" ]]; then
error "Received invalid arguments: $1 $2"
fi
local input
local output="$2"
# Read input - split by space to be array-safe
read -ra input <<<"$1"
# Since Bash redirects execute before the command,
# we need a temporary file to act as a buffer for output.
local tmp_file
tmp_file=$(mktemp)
# Output to buffer with newline as separator between entries
printf "%s\n" "${input[@]}" >"${tmp_file}"
# Ensure the directory containing the output file exists
mkdir -p "$(dirname "${output}")"
# Redirect output from buffer and cleanup
cat "${tmp_file}" >"${output}"
rm -f "${tmp_file}"
}
function command-docker-tags() {
if [[ "$#" -ne 2 ]]; then
error "Arguments mismatch: required 2, received $#."
fi
if [[ -z "$1" || -z "$2" ]]; then
error "Received invalid arguments: $1 $2"
fi
local image="$1"
local output="$2"
# Docker v2 API endpoints
local tokenUri="https://auth.docker.io/token"
local listUri="https://registry-1.docker.io/v2/${image}/tags/list"
# Get authorization token from the registry
local token
local data=("service=registry.docker.io" "scope=repository:${image}:pull")
token="$(curl -s -G --data-urlencode "${data[0]}" --data-urlencode "${data[1]}" ${tokenUri} | jq -r ".token")"
# Get list of image tags (filters out variant images)
local tags
tags="$(curl -s -G -H "Accept: application/json" -H "Authorization: Bearer ${token}" "${listUri}" | jq -r '.tags | map(select(test("-") | not)) | @sh')"
# Output
export-to-file "$(echo "${tags}" | tr -d \'\")" "${output}"
}
function command-minor-tags() {
if [[ "$#" -lt 1 || "$#" -gt 2 ]]; then
error "Arguments mismatch: required 1 or 2, received $#."
fi
if [[ -z "$1" ]]; then
error "Received invalid arguments: $1 ${2-}"
fi
# ShellCheck gets confused if these lines are merged
local input
input="$1"
local output="${2-$input}"
local parts=()
local tags=()
while read -r version; do
# Split version into array of semver parts (x.y.z => (x y z))
IFS="." read -ra parts <<<"${version}"
# Only include minor versions (x.y)
if [ ${#parts[@]} -eq "2" ]; then
tags+=("${version}")
fi
done <"${input}"
# Output
export-to-file "${tags[*]}" "${output}"
}
function command-test() {
if [[ "$#" -ne 2 ]]; then
error "Arguments mismatch: required 2, received $#."
fi
if [[ -z "$1" || -z "$2" ]]; then
error "Received invalid arguments: $1 $2"
fi
local name="$1"
local tag="$2"
# This is intentionally global - it will be used by `trap` for cleanup
container="test-${tag}"
# Spawn a new container with an interactive shell to keep it alive
docker run --detach --init --tty \
--cap-add=SYS_ADMIN --name "${container}" --user circleci:circleci \
"${name}:${tag}" bash >/dev/null
# Copy fixtures and the Makefile into the container
docker cp ./fixtures/. "${container}":/home/circleci/project/fixtures
docker cp ./Makefile "${container}":/home/circleci/project/Makefile
# Parse the container's Node.js version
local node_version
local parts=()
node_version=$(docker exec "${container}" node --version | sed -e "s|^[vV]||g")
IFS="." read -ra parts <<<"${node_version}"
local major="${parts[0]}"
local minor="${parts[1]}"
local patch="${parts[2]}"
# Puppeteer@2 is used as a baseline since it supports all available tags for cimg/node_version
local puppeteer_versions=("2")
if [[ ($major -eq 10 && $minor -eq 18 && $patch -ge 1) || ($major -eq 10 && $minor -gt 18) || $major -gt 10 ]]; then
# All of v3, v4 and v5 of puppeteer support Node.js 10.18.1+
puppeteer_versions+=("3" "4" "5")
fi
# Cleanup the spawned container on error or exit
function cleanup() {
# Check for container's existence - it might have crashed
if docker ps -a | grep -q "${container}"; then
docker kill "${container}" >/dev/null
docker rm "${container}" >/dev/null
fi
}
trap cleanup err exit
# Run tests for all compatible puppeteer versions
docker exec "${container}" make verify-all puppeteer="${puppeteer_versions[*]}"
}
case $# in
0)
error "Unknown command: $*"
;;
esac
case $1 in
docker-tags)
shift
command-docker-tags "$@"
;;
minor-tags)
shift
command-minor-tags "$@"
;;
test)
shift
command-test "$@"
;;
*)
error "Unknown arguments: $*"
;;
esac