Skip to content

Commit cebcfd9

Browse files
authored
Merge pull request #1 from buildkite-plugins/bm/append_annotations
Fix annotation appending and env var issue
2 parents 9e7047f + 8c9715a commit cebcfd9

5 files changed

Lines changed: 93 additions & 15 deletions

File tree

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,39 @@ steps:
8686
bep_file: my-workspace/bazel-events.json
8787
```
8888
89+
### Multiple Bazel jobs in a pipeline with consolidated annotations
90+
91+
```yaml
92+
steps:
93+
- label: "🔨 Build with Bazel"
94+
command: |
95+
bazel build //... --build_event_json_file=bazel-build-events.json
96+
plugins:
97+
- bazel-annotate#v0.1.0:
98+
bep_file: bazel-build-events.json
99+
100+
- label: "🧪 Test with Bazel"
101+
command: |
102+
bazel test //... --build_event_json_file=bazel-test-events.json
103+
plugins:
104+
- bazel-annotate#v0.1.0:
105+
bep_file: bazel-test-events.json
106+
107+
- label: "📦 Package with Bazel"
108+
command: |
109+
bazel run //:package --build_event_json_file=bazel-package-events.json
110+
plugins:
111+
- bazel-annotate#v0.1.0:
112+
bep_file: bazel-package-events.json
113+
```
114+
89115
## How It Works
90116
91117
1. After your Bazel command runs, the plugin looks for the BEP file
92118
2. It parses the BEP data to extract build status, test results, and performance metrics
93119
3. It creates a detailed Buildkite annotation with this information
94120
4. The annotation shows success/failure status, test performance, and detailed error logs
121+
5. Multiple plugin usages in a pipeline will append to the same annotation, allowing for consolidated build information
95122
96123
## Troubleshooting
97124

hooks/post-command

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ BEP_FILE=$(plugin_read_config BEP_FILE "")
1414
# Check if we should skip if no BEP file found
1515
SKIP_IF_NO_BEP=$(plugin_read_config SKIP_IF_NO_BEP "false")
1616

17+
# Check if this is the first job to create the annotation
18+
# Only do this check if we're running in Buildkite
19+
if [ -n "${BUILDKITE:-}" ] && command -v buildkite-agent >/dev/null 2>&1; then
20+
if buildkite-agent meta-data exists "bazel-annotate-header-created" 2>/dev/null; then
21+
echo "Detected existing annotation header from metadata"
22+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_IS_FIRST_JOB="false"
23+
else
24+
echo "This appears to be the first job creating an annotation"
25+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_IS_FIRST_JOB="true"
26+
fi
27+
fi
28+
1729
# If we still don't have a BEP file, check if it exists at common locations
1830
if [[ -z "${BEP_FILE}" ]]; then
1931
# Try some common locations

lib/bazel-bep.bash

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,35 @@ create_annotation() {
2828
local style="$1"
2929
local content="$2"
3030
local context_id="bazel-bep-results"
31+
local job_name="${BUILDKITE_LABEL:-Unknown Job}"
32+
local is_first_job="${BUILDKITE_PLUGIN_BAZEL_ANNOTATE_IS_FIRST_JOB:-true}"
3133

3234
# Check if we're running in Buildkite
3335
if [ -n "${BUILDKITE:-}" ] && command -v buildkite-agent >/dev/null 2>&1; then
34-
echo "Creating Buildkite annotation..."
36+
# Modify content based on whether this is the first job
37+
if [ "$is_first_job" != "true" ]; then
38+
# If not the first job, add a job section instead of the main header
39+
echo "Appending to existing Buildkite annotation..."
40+
41+
# Replace the main header with a job-specific header or remove it
42+
content=$(echo "$content" | sed '1,3d') # Remove the first 3 lines (header + blank line)
43+
44+
# Add job-specific section header
45+
content="### 🧩 ${job_name}\n\n${content}"
46+
else
47+
echo "Creating initial Buildkite annotation with header..."
48+
fi
49+
3550
# Use printf to ensure newlines are properly interpreted
36-
printf "%s" "$content" | buildkite-agent annotate --style "$style" --context "$context_id"
51+
printf "%s" "$content" | buildkite-agent annotate --style "$style" --context "$context_id" --append
52+
53+
# Signal to future jobs that they're not the first
54+
if buildkite-agent meta-data exists "bazel-annotate-header-created" 2>/dev/null; then
55+
echo "Header already marked as created in metadata"
56+
else
57+
echo "Setting metadata to indicate header has been created"
58+
buildkite-agent meta-data set "bazel-annotate-header-created" "true" || true
59+
fi
3760
else
3861
# We're not in Buildkite, just display the content on stdout
3962
echo "Not running in Buildkite. Would create annotation with style '$style':"
@@ -251,8 +274,10 @@ process_bep() {
251274
status_emoji="⏭️"
252275
fi
253276

254-
# Clean header for the output
255-
local summary="## 🚀 Bazel Results
277+
# Use job-specific header
278+
local job_label="${BUILDKITE_LABEL:-Bazel Results}"
279+
# Simple, clean job header
280+
local summary="### ${job_label}
256281
257282
"
258283
# Add command used if running in Buildkite
@@ -362,13 +387,13 @@ $failure_details</details>
362387
"
363388
fi
364389

365-
# Add random inspirational quote
390+
# Add extra spacing between appended annotations for better readability
366391
summary+="
392+
393+
367394
---
368395
369-
💡 **Random Dev Wisdom:**
370396
371-
_$(get_random_quote)_
372397
"
373398

374399
# Create the annotation

lib/plugin.bash

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22
set -euo pipefail
33

4-
PLUGIN_PREFIX="BAZEL_BEP_ANNOTATE"
4+
# The environment variables set by Buildkite don't include "BEP" in the prefix
5+
PLUGIN_PREFIX="BAZEL_ANNOTATE"
56

67
# Reads either a value or a list from the given env prefix
78
function prefix_read_list() {

tests/post-command.bats

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ setup() {
88

99
# Setup environment
1010
export BUILDKITE=true
11-
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATE_SKIP_IF_NO_BEP=false
11+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_SKIP_IF_NO_BEP=false
1212

1313
# Mock the process_bep function to avoid relying on external tools
1414
cat > "$TEMP_DIR/mock-bazel-bep.bash" << 'EOF'
@@ -21,6 +21,8 @@ process_bep() {
2121
echo "Mock processing BEP file: $BEP_FILE"
2222
if [[ -f "$BEP_FILE" ]]; then
2323
echo "Mock BEP file exists, processing successful"
24+
# Call create_annotation after successful processing
25+
create_annotation "info" "This is a mock summary"
2426
return 0
2527
else
2628
echo "Mock BEP file does not exist"
@@ -31,7 +33,7 @@ process_bep() {
3133
create_annotation() {
3234
local style="$1"
3335
local content="$2"
34-
echo "Mock annotation created with style: $style"
36+
echo "Mock annotation created with style: $style and append flag enabled"
3537
}
3638
EOF
3739

@@ -115,7 +117,7 @@ teardown() {
115117

116118
@test "Skip when no BEP file and skip option is enabled" {
117119
# No BEP file, but we'll enable skip option
118-
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATE_SKIP_IF_NO_BEP=true
120+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_SKIP_IF_NO_BEP=true
119121

120122
# Ensure no common files exist
121123
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-events.json" || true
@@ -130,7 +132,7 @@ teardown() {
130132

131133
@test "Fail when no BEP file and skip option is disabled" {
132134
# No BEP file, skip option disabled
133-
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATE_SKIP_IF_NO_BEP=false
135+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_SKIP_IF_NO_BEP=false
134136

135137
# Ensure no common files exist
136138
rm -f "${BUILDKITE_BUILD_CHECKOUT_PATH:-$PWD}/bazel-events.json" || true
@@ -146,7 +148,7 @@ teardown() {
146148
@test "Process BEP file when explicitly provided" {
147149
# Create a sample BEP file
148150
touch "$TEMP_DIR/sample.bep"
149-
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATE_BEP_FILE="$TEMP_DIR/sample.bep"
151+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_BEP_FILE="$TEMP_DIR/sample.bep"
150152

151153
run "$TEMP_DIR/hooks/post-command"
152154

@@ -173,11 +175,22 @@ teardown() {
173175

174176
@test "Skip when BEP file doesn't exist and skip is enabled" {
175177
# Reference a non-existent BEP file with skip enabled
176-
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATE_BEP_FILE="$TEMP_DIR/nonexistent.bep"
177-
export BUILDKITE_PLUGIN_BAZEL_BEP_ANNOTATE_SKIP_IF_NO_BEP=true
178+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_BEP_FILE="$TEMP_DIR/nonexistent.bep"
179+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_SKIP_IF_NO_BEP=true
178180

179181
run "$TEMP_DIR/hooks/post-command"
180182

181183
assert_success
182184
assert_output --partial "BEP file not found at '$TEMP_DIR/nonexistent.bep' and skip_if_no_bep is true"
183185
}
186+
187+
@test "Verify annotations are created with append flag" {
188+
# Create a sample BEP file
189+
touch "$TEMP_DIR/sample.bep"
190+
export BUILDKITE_PLUGIN_BAZEL_ANNOTATE_BEP_FILE="$TEMP_DIR/sample.bep"
191+
192+
run "$TEMP_DIR/hooks/post-command"
193+
194+
assert_success
195+
assert_output --partial "Mock annotation created with style: info and append flag enabled"
196+
}

0 commit comments

Comments
 (0)