/
pre-exit
executable file
路242 lines (204 loc) 路 7.68 KB
/
pre-exit
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/bin/bash
set -euo pipefail
TOKEN_ENV_NAME="${BUILDKITE_PLUGIN_TEST_COLLECTOR_API_TOKEN_ENV_NAME:-BUILDKITE_ANALYTICS_TOKEN}"
FORMAT="${BUILDKITE_PLUGIN_TEST_COLLECTOR_FORMAT:-}"
TIMEOUT="${BUILDKITE_PLUGIN_TEST_COLLECTOR_TIMEOUT:-30}"
BASE_PATH="${BUILDKITE_PLUGIN_TEST_COLLECTOR_BASE_PATH:-.}"
ANNOTATE="${BUILDKITE_PLUGIN_TEST_COLLECTOR_ANNOTATION_LINK:-false}"
UPLOAD_CONCURRENCY="${BUILDKITE_PLUGIN_TEST_COLLECTOR_UPLOAD_CONCURRENCY:-1}"
REPORT_URLS_FILE=$(mktemp)
CURL_RESP_FILE="${CURL_RESP_FILE:-}"
DEBUG="false"
if [[ "${BUILDKITE_PLUGIN_TEST_COLLECTOR_DEBUG:-}" =~ ^(true|on|1|always)$ ]]; then
DEBUG="true"
elif [[ "${BUILDKITE_ANALYTICS_DEBUG_ENABLED:-}" =~ ^(true|on|1|always)$ ]]; then
DEBUG="true"
fi
if [[ -n "${BUILDKITE_PLUGIN_TEST_COLLECTOR_BRANCHES:-}" ]]; then
if [[ ! "${BUILDKITE_BRANCH}" =~ ${BUILDKITE_PLUGIN_TEST_COLLECTOR_BRANCHES} ]]; then
echo "Branch ${BUILDKITE_BRANCH} does not match selector ${BUILDKITE_PLUGIN_TEST_COLLECTOR_BRANCHES}. Skipping it."
exit 0
fi
fi
if [[ -n "${BUILDKITE_PLUGIN_TEST_COLLECTOR_EXCLUDE_BRANCHES:-}" ]]; then
if [[ "${BUILDKITE_BRANCH}" =~ ${BUILDKITE_PLUGIN_TEST_COLLECTOR_EXCLUDE_BRANCHES} ]]; then
echo "Branch ${BUILDKITE_BRANCH} matches exclude selector ${BUILDKITE_PLUGIN_TEST_COLLECTOR_EXCLUDE_BRANCHES}. Skipping it."
exit 0
fi
fi
TOKEN_VALUE="${!TOKEN_ENV_NAME:-}"
PLUGIN_VERSION=$(git rev-parse --short HEAD 2>/dev/null || echo "")
if [[ -z "${TOKEN_VALUE}" ]]; then
echo "Missing $TOKEN_ENV_NAME environment variable"
exit 1
fi
if [[ -z "${FORMAT}" ]]; then
echo "Missing file format 'format'. Possible values: 'junit', 'json'"
exit 1
fi
# Creates the annotations with all the report URLs
annotation-link() {
local json_file="$1"
mapfile -t REPORT_URLS < <(sort "${json_file}" | uniq)
local REPORTS=""
REPORT_COUNT=1
if [ ${#REPORT_URLS[@]} -gt 0 ]; then
echo "Got ${#REPORT_URLS[@]} report URLs."
for URL in "${REPORT_URLS[@]}"; do
REPORTS+="- [Report #${REPORT_COUNT}](${URL})\n"
REPORT_COUNT=$((REPORT_COUNT + 1))
done
echo -e "Check the ${BUILDKITE_LABEL} test report(s) here: \n\n${REPORTS}" | buildkite-agent annotate --style info --context "test-collector" --append
else
echo "There are no report URLs to annotate."
fi
}
# Saves the build with the report URL
save-report-url() {
local json_file="$1"
local report_url
if [ ! -f "${json_file}" ]; then
echo "Could not get the tests report URL from $json_file. File not found."
return
fi
# Could be easier with jq, but we don't want to require it
if which jq >/dev/null; then
echo "Using jq to parse the report URL"
report_url=$(jq -r '.run_url' "${json_file}")
else
echo "Not using jq to parse the report URL"
report_url=$(sed 's/.*"run_url" *: *"\([^"]*\)".*/\1/g' "${json_file}")
fi
if [ "$report_url" == "null" ]; then
echo "Could not get the tests report URL from $json_file. 'run_url' property not found."
return
fi
echo "$report_url" >> "$REPORT_URLS_FILE"
}
# Uploads files to the Test Analytics API
#
# Upload failures should not fail the build, and should have a sensible timeout,
# so that Test Analytics availability doesn't affect build reliability.
upload() {
local response_file="$4"
local file="$3"
local format="$2"
local curl_args=(
"-X" "POST"
"--silent"
"--show-error"
"--max-time" "${TIMEOUT}"
"--form" "format=${format}"
"--form" "data=@\"$file\""
"--form" "run_env[CI]=buildkite"
"--form" "run_env[key]=\"$BUILDKITE_BUILD_ID\""
"--form" "run_env[url]=\"$BUILDKITE_BUILD_URL\""
"--form" "run_env[branch]=\"$BUILDKITE_BRANCH\""
"--form" "run_env[commit_sha]=\"$BUILDKITE_COMMIT\""
"--form" "run_env[number]=\"$BUILDKITE_BUILD_NUMBER\""
"--form" "run_env[job_id]=\"$BUILDKITE_JOB_ID\""
"--form" "run_env[message]=\"$BUILDKITE_MESSAGE\""
"--form" "run_env[collector]=test-collector-buildkite-plugin"
)
if [[ "$DEBUG" == "true" ]]; then
curl_args+=("--form" "run_env[debug]=\"$DEBUG\"")
fi
if [[ -n "$PLUGIN_VERSION" ]]; then
curl_args+=("--form" "run_env[version]=\"$PLUGIN_VERSION\"")
fi
if [[ "$ANNOTATE" != "false" ]]; then
curl_args+=("-o" "${response_file}")
fi
curl_args+=("${BUILDKITE_PLUGIN_TEST_COLLECTOR_API_URL:-https://analytics-api.buildkite.com/v1/uploads}")
# Print debugging output before we add the token, so it doesn't ever get
# printed to output
if [[ "$DEBUG" == "true" ]]; then
echo curl "${curl_args[@]}"
fi
curl "${curl_args[@]}" -H @<(printf 'Authorization: Token token=\"%s\"\n' "${TOKEN_VALUE}")
}
# Runs the whole plugin logic for a particular find pattern
find_and_upload() {
FILES_PATTERN="$1"
FIND_CMD=(find)
if [[ "${BUILDKITE_PLUGIN_TEST_COLLECTOR_FOLLOW_SYMLINKS:-}" =~ ^(true|on|1|always)$ ]]; then
FIND_CMD+=('-L')
fi
matching_files=()
while IFS=$'' read -r matching_file ; do
matching_files+=("$matching_file")
done < <("${FIND_CMD[@]}" "${BASE_PATH}" -path "${BASE_PATH}/${FILES_PATTERN}" | sort)
if [[ "${#matching_files[@]}" -eq "0" ]]; then
echo "No files found matching '${FILES_PATTERN}'"
if [[ "${BUILDKITE_COMMAND_EXIT_STATUS:-0}" -eq "0" ]]; then
exit "${BUILDKITE_PLUGIN_TEST_COLLECTOR_MISSING_ERROR:-1}"
fi
else
declare -a uploads_in_progress=()
echo "Uploading '${#matching_files[@]}' files matching '${FILES_PATTERN}'"
# needs to be part of else for bash4.3 compatibility
for file in "${matching_files[@]}"; do
iterations_waited=0
while [[ "${#uploads_in_progress[@]}" -ge $UPLOAD_CONCURRENCY ]]; do
iterations_waited=$((iterations_waited + 1))
if [[ "${DEBUG}" == "true" ]]; then
echo "Waiting for uploads to finish..."
fi
sleep 1
for index in "${!uploads_in_progress[@]}"; do
# Note: kill -0 does not kill the pid, it provides a *nix compatible way to test the pid is responding.
if ! kill -0 "${uploads_in_progress[index]}" > /dev/null; then
unset 'uploads_in_progress[index]'
elif [[ "$iterations_waited" -gt $TIMEOUT ]]; then
echo "Upload '${uploads_in_progress[index]}' has been running for more than '${TIMEOUT}' seconds, killing it"
kill "${uploads_in_progress[index]}"
unset 'uploads_in_progress[index]'
fi
done
done
# Spawn a subcommand group to allow parallel uploads
{
echo "Uploading '$file'..."
if [[ -n "${CURL_RESP_FILE}" ]]; then
response_file="${CURL_RESP_FILE}"
else
response_file="$(mktemp -t 'response.XXXXXX')"
fi
if ! upload "$TOKEN_VALUE" "$FORMAT" "${file}" "${response_file}"; then
echo "Error uploading, will continue"
fi
if [[ "$ANNOTATE" != "false" ]]; then
save-report-url "${response_file}"
fi
if [[ "${DEBUG}" == "true" ]]; then
echo "Finished uploading '$file'"
fi
} &
# Store the PID of the upload
uploads_in_progress+=($!)
done
# Wait for all uploads to finish
wait "${uploads_in_progress[@]}"
fi
}
if [ -n "${BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES:-}" ]; then
find_and_upload "${BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES}"
elif [ -n "${BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES_0:-}" ]; then
prefix="BUILDKITE_PLUGIN_TEST_COLLECTOR_FILES"
parameter="${prefix}_0"
if [ -n "${!parameter:-}" ]; then
i=0
parameter="${prefix}_${i}"
while [ -n "${!parameter:-}" ]; do
find_and_upload "${!parameter}"
i=$((i+1))
parameter="${prefix}_${i}"
done
fi
else
echo "Missing file upload pattern 'files', e.g. 'junit-*.xml'"
exit 1
fi
if [ "$ANNOTATE" != "false" ]; then
annotation-link "${REPORT_URLS_FILE}"
fi