-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathcommand
More file actions
executable file
·141 lines (118 loc) · 4.84 KB
/
command
File metadata and controls
executable file
·141 lines (118 loc) · 4.84 KB
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
#!/bin/bash
set -euo pipefail
if [[ -z "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS:-}" ]]; then
echo "🚨 Missing artifacts configuration for the plugin"
exit 1
fi
PLUGIN_DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)/.."
MAX_SIZE=1024 # in KB
RUBY_IMAGE="${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_RUBY_IMAGE:-ruby:3.1-alpine@sha256:a39e26d0598837f08c75a42c8b0886d9ed5cc862c4b535662922ee1d05272fca}"
artifacts_dir="$(pwd)/$(mktemp -d "junit-annotate-plugin-artifacts-tmp.XXXXXXXXXX")"
annotation_dir="$(pwd)/$(mktemp -d "junit-annotate-plugin-annotation-tmp.XXXXXXXXXX")"
annotation_path="${annotation_dir}/annotation.md"
annotation_style="${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_STYLE:-info}"
fail_build=0
has_errors=0
create_annotation=0
# shellcheck disable=2317 # this is a signal function
function cleanup {
rm -rf "${artifacts_dir}"
rm -rf "${annotation_dir}"
}
function check_size {
local size_in_kb
size_in_kb=$(du -k "${annotation_path}" | cut -f 1)
[ "${size_in_kb}" -lt "${MAX_SIZE}" ]
}
trap cleanup EXIT
echo "--- :junit: Download the junits"
if ! buildkite-agent artifact download "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ARTIFACTS}" "$artifacts_dir"; then
echo "--- :boom: Could not download artifacts"
exit "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILED_DOWNLOAD_EXIT_CODE:-2}"
fi
echo "--- :junit: Processing the junits"
set +e
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_RUN_IN_DOCKER:-true}" =~ "true" ]]; then
docker \
--log-level "error" \
run \
--rm \
--volume "$artifacts_dir:/junits:Z" \
--volume "$PLUGIN_DIR/ruby:/src:Z" \
--env "BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN=${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN:-}" \
--env "BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILURE_FORMAT=${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILURE_FORMAT:-}" \
--env "BUILDKITE_PLUGIN_JUNIT_ANNOTATE_REPORT_SLOWEST=${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_REPORT_SLOWEST:-}" \
--env "BUILDKITE_PLUGIN_JUNIT_ANNOTATE_REPORT_SKIPPED=${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_REPORT_SKIPPED:-}" \
"${RUBY_IMAGE}" ruby /src/bin/annotate /junits \
> "$annotation_path"
else
ruby "${PLUGIN_DIR}/ruby/bin/annotate" "${artifacts_dir}" > "$annotation_path"
fi
exit_code=$?
set -e
if [[ $exit_code -eq 64 ]]; then # special exit code to signal test failures
has_errors=1
create_annotation=1
# annotation_style="error"
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAIL_BUILD_ON_ERROR:-false}" =~ (true|on|1) ]]; then
echo "--- :boom: Build will fail due to errors being found"
fail_build=1
fi
elif [[ $exit_code -ne 0 ]]; then
echo "--- :boom: Error when processing JUnit tests"
exit $exit_code
fi
cat "$annotation_path"
if [ $has_errors -eq 0 ]; then
# done in nested if to simplify outer conditions
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ALWAYS_ANNOTATE:-false}" =~ (true|on|1) ]]; then
echo "Will create annotation anyways"
create_annotation=1
fi
if [[ -n "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_REPORT_SLOWEST:-}" ]]; then
echo "Create annotation with slowest tests"
create_annotation=1
fi
if [[ -e "${annotation_path}" ]]; then
TOTAL_TESTS=$(head -5 "${annotation_path}" | grep 'Total tests' | cut -d\ -f3)
else
TOTAL_TESTS=0
fi
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_MIN_TESTS:-0}" -gt "${TOTAL_TESTS}" ]]; then
create_annotation=1
fail_build=1
echo ":warning: Less than ${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_MIN_TESTS} tests analyzed"
fi
elif ! check_size; then
echo "--- :warning: Failures too large to annotate"
if [[ "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_ADD_ERROR_ANNOTATION:-false}" =~ (true|on|1) ]]; then
fail_build=1
echo "--- :buildkite: Creating failure annotation"
annotation_path="${annotation_dir}/failed-annotation.md"
echo "<details><summary><code>The failures are too large to create a build annotation. Please inspect the failures manually.</code></summary></details>" > ${annotation_path}
create_annotation=1
else
# creating a simplified version of the annotation
mv "${annotation_path}" "${annotation_path}2"
head -5 "${annotation_path}2" >"${annotation_path}"
# || true is to avoid issues if no summary is found
grep '<summary>' "${annotation_path}2" >>"${annotation_path}" || true
if ! check_size; then
echo "The failures are too large to create a build annotation. Please inspect the failed JUnit artifacts manually."
create_annotation=0
else
echo "The failures are too large to create complete annotation, using a simplified annotation"
fi
fi
fi
if [ $create_annotation -ne 0 ]; then
echo "--- :buildkite: Creating annotation"
# shellcheck disable=SC2002
cat "$annotation_path" | buildkite-agent annotate --context "${BUILDKITE_PLUGIN_JUNIT_ANNOTATE_CONTEXT:-junit}" --style "$annotation_style"
fi
if ((fail_build)); then
echo "--- :boom: Failing build due to error"
exit 1
else
exit 0
fi