Skip to content

Commit 1237b0d

Browse files
committed
Debug merge base logic
1 parent e87be8f commit 1237b0d

2 files changed

Lines changed: 54 additions & 72 deletions

File tree

.gitlab/benchmarks.yml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.dsm-kafka-benchmarks:
44
stage: benchmarks
55
rules:
6+
- when: never # disable while testing merge base logic
67
- if: '$POPULATE_CACHE'
78
when: never
89
- if: $CI_PIPELINE_SOURCE != "schedule"
@@ -47,6 +48,7 @@ dsm-kafka-consumer-benchmark:
4748
debugger-benchmarks:
4849
stage: benchmarks
4950
rules:
51+
- when: never # disable while testing merge base logic
5052
- if: '$POPULATE_CACHE'
5153
when: never
5254
- if: $CI_PIPELINE_SOURCE != "schedule"
@@ -87,12 +89,13 @@ debugger-benchmarks:
8789
tags: ["arch:amd64"]
8890
interruptible: true
8991
rules:
92+
- when: never # disable while testing merge base logic
9093
- if: '$POPULATE_CACHE'
9194
when: never
9295
- when: on_success
9396
script:
9497
- mkdir -p "$(pwd)/reports/${BENCHMARK_TYPE}"
95-
- .gitlab/scripts/get-baseline-commit-info.sh "$(pwd)/reports/baseline-info.env" "$(pwd)/reports/${BENCHMARK_TYPE}/fallback_to_master.txt"
98+
- .gitlab/scripts/get-baseline-commit-info.sh "$(pwd)/reports/${BENCHMARK_TYPE}"
9699
- .gitlab/scripts/benchmark-compare.sh "${BENCHMARK_TYPE}"
97100
artifacts:
98101
name: "benchmark-compare-${BENCHMARK_TYPE}"
@@ -131,6 +134,7 @@ benchmarks-post-pr-comment:
131134
- job: benchmarks-compare-dacapo
132135
artifacts: true
133136
rules:
137+
- when: never # disable while testing merge base logic
134138
- if: '$POPULATE_CACHE'
135139
when: never
136140
- when: always
@@ -141,3 +145,24 @@ benchmarks-post-pr-comment:
141145
UPSTREAM_BRANCH: $CI_COMMIT_REF_NAME
142146
BP_HEADER: "Benchmarks"
143147
BP_ON_DUPLICATE: "replace"
148+
149+
# DEBUG MERGE BASE LOGIC IMMEDIATELY instead of waiting on all benchmarks to complete
150+
benchmarks-debug-baseline:
151+
stage: benchmark-comparison
152+
image: registry.ddbuild.io/images/benchmarking-platform-tools-ubuntu:6845f3c7
153+
tags: ["arch:amd64"]
154+
interruptible: true
155+
needs: []
156+
rules:
157+
- when: on_success
158+
script:
159+
- mkdir -p reports/debug
160+
- .gitlab/scripts/get-baseline-commit-info.sh "reports/debug"
161+
- echo "Resolved baseline:"
162+
- cat reports/debug/baseline-info.env
163+
- if [[ -f "reports/debug/fallback_to_master.txt" ]]; then cat "reports/debug/fallback_to_master.txt"; fi
164+
artifacts:
165+
when: always
166+
paths:
167+
- reports/debug/
168+
expire_in: 1 week

.gitlab/scripts/get-baseline-commit-info.sh

Lines changed: 28 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,106 +3,58 @@
33
set -euo pipefail
44

55
readonly SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" &>/dev/null && pwd 2>/dev/null)"
6-
readonly OUT_ENV_FILE="${1:-${SCRIPT_DIR}/../reports/baseline-info.env}"
7-
readonly FALLBACK_MARKER_FILE="${2:-${SCRIPT_DIR}/../reports/fallback_to_master.txt}"
6+
readonly OUTPUT_DIR="${1:-${SCRIPT_DIR}/../reports}"
7+
readonly OUT_ENV_FILE="${OUTPUT_DIR}/baseline-info.env"
8+
readonly FALLBACK_MARKER_FILE="${OUTPUT_DIR}/fallback_to_master.txt"
89
readonly TARGET_BRANCH="${TARGET_BRANCH:-master}"
910

10-
mkdir -p "$(dirname "${OUT_ENV_FILE}")"
11+
mkdir -p "${OUTPUT_DIR}"
1112
rm -f "${FALLBACK_MARKER_FILE}"
1213

13-
if [[ -z "${CI_PROJECT_ID:-}" || -z "${CI_API_V4_URL:-}" ]]; then
14-
echo "Missing CI_PROJECT_ID/CI_API_V4_URL environment variables." >&2
14+
if [[ -z "${CI_PROJECT_ID:-}" || -z "${CI_API_V4_URL:-}" || -z "${CI_JOB_TOKEN:-}" ]]; then
15+
echo "Missing CI_PROJECT_ID/CI_API_V4_URL/CI_JOB_TOKEN environment variables." >&2
1516
exit 1
1617
fi
1718

1819
readonly PROJECT_API_URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}"
1920

2021
get_pipeline_id_for_commit() {
2122
local commit_sha="$1"
22-
23-
# Get JWT token with `authanywhere`, needed for internal API authentication
24-
# https://datadoghq.atlassian.net/wiki/spaces/DEVX/pages/3733193227/Authanywhere
25-
local auth_header
26-
if ! auth_header="$(authanywhere --audience sdm 2>&1)"; then
27-
echo "ERROR: Failed to get authanywhere token: ${auth_header}" >&2
28-
return 1
29-
fi
30-
31-
# Get short-lived PAT from BTI CI API
32-
# https://datadoghq.atlassian.net/wiki/spaces/DEVX/pages/5421924354/BTI+CI+API+-+the+swiss+army+knife+of+CI+interactions#Get-Short-lived-Project-Access-Token
33-
local bti_response http_status response_body private_token
34-
bti_response="$(curl -w "\nHTTP_STATUS:%{http_code}" --silent --show-error \
35-
--header "${auth_header}" \
36-
"https://bti-ci-api.us1.ddbuild.io/internal/ci/gitlab/token?owner=DataDog&repository=apm-reliability/dd-trace-java" 2>&1)"
37-
http_status="$(echo "${bti_response}" | grep "HTTP_STATUS:" | sed 's/HTTP_STATUS://')"
38-
response_body="$(echo "${bti_response}" | sed '/HTTP_STATUS:/d')"
39-
if [[ "${http_status}" != "200" ]]; then
40-
echo "ERROR: BTI CI API returned HTTP ${http_status} with response: ${response_body:0:200}" >&2
41-
return 1
42-
fi
43-
private_token="$(echo "${response_body}" | grep -o '"token":"[^"]*"' | sed 's/"token":"\([^"]*\)"/\1/')"
44-
if [[ -z "${private_token}" ]]; then
45-
echo "ERROR: Failed to retrieve private token from BTI CI API" >&2
46-
return 1
47-
fi
4823

4924
# Query GitLab API to get pipeline ID that matches the commit SHA
5025
local api_url="${PROJECT_API_URL}/repository/commits/${commit_sha}"
5126
local response pipeline_id
52-
response="$(curl --request GET --silent --header "PRIVATE-TOKEN: ${private_token}" --show-error "${api_url}" 2>&1)"
27+
response="$(curl --request GET --silent --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" "${api_url}" || true)"
5328
pipeline_id="$(echo "${response}" | grep -o '"last_pipeline"[^}]*"id":[0-9]*' | grep -o '[0-9]*$' | head -1 || true)"
5429
if [[ -n "${pipeline_id}" && "${pipeline_id}" != "null" ]]; then
55-
echo "Found pipeline ID: ${pipeline_id} for commit: ${commit_sha}" >&2
5630
echo "${pipeline_id}"
5731
return 0
58-
else
59-
echo "Could not find pipeline ID for commit ${commit_sha}" >&2
60-
return 1
6132
fi
33+
34+
echo ""
35+
return 1
6236
}
6337

6438
get_branch_head_sha() {
6539
local branch="$1"
66-
67-
# Get JWT token with `authanywhere`, needed for internal API authentication
68-
local auth_header
69-
if ! auth_header="$(authanywhere --audience sdm 2>&1)"; then
70-
echo "ERROR: Failed to get authanywhere token: ${auth_header}" >&2
71-
return 1
72-
fi
73-
74-
# Get short-lived PAT from BTI CI API
75-
local bti_response http_status response_body private_token
76-
bti_response="$(curl -w "\nHTTP_STATUS:%{http_code}" --silent --show-error \
77-
--header "${auth_header}" \
78-
"https://bti-ci-api.us1.ddbuild.io/internal/ci/gitlab/token?owner=DataDog&repository=apm-reliability/dd-trace-java" 2>&1)"
79-
http_status="$(echo "${bti_response}" | grep "HTTP_STATUS:" | sed 's/HTTP_STATUS://')"
80-
response_body="$(echo "${bti_response}" | sed '/HTTP_STATUS:/d')"
81-
82-
if [[ "${http_status}" != "200" ]]; then
83-
echo "ERROR: BTI CI API returned HTTP ${http_status} with response: ${response_body:0:200}" >&2
84-
return 1
85-
fi
86-
87-
private_token="$(echo "${response_body}" | grep -o '"token":"[^"]*"' | sed 's/"token":"\([^"]*\)"/\1/')"
88-
if [[ -z "${private_token}" ]]; then
89-
echo "ERROR: Failed to retrieve private token from BTI CI API" >&2
90-
return 1
91-
fi
9240

9341
# Query GitLab API to get branch head SHA
9442
local api_url="${PROJECT_API_URL}/repository/branches/${branch}"
9543
local response branch_sha
96-
response="$(curl --request GET --silent --header "PRIVATE-TOKEN: ${private_token}" --show-error "${api_url}" 2>&1)"
44+
response="$(curl --request GET --silent --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" "${api_url}" || true)"
9745
branch_sha="$(echo "${response}" | grep -o '"commit"[^}]*"id":"[a-f0-9]\{40\}"' | sed -E 's/.*"id":"([a-f0-9]{40})".*/\1/' | head -1 || true)"
9846

99-
if [[ -n "${branch_sha}" ]]; then
100-
echo "${branch_sha}"
101-
return 0
102-
else
103-
echo "ERROR: Could not find branch head SHA for branch ${branch}" >&2
104-
return 1
105-
fi
47+
echo "${branch_sha}"
48+
}
49+
50+
get_latest_pipeline_id_for_branch() {
51+
local branch="$1"
52+
local api_url="${PROJECT_API_URL}/pipelines?ref=${branch}&status=success&order_by=updated_at&sort=desc&per_page=1"
53+
local response pipeline_id
54+
55+
response="$(curl --request GET --silent --show-error --header "JOB-TOKEN: ${CI_JOB_TOKEN}" "${api_url}" || true)"
56+
pipeline_id="$(echo "${response}" | grep -o '"id":[0-9]*' | head -1 | grep -o '[0-9]*' || true)"
57+
echo "${pipeline_id}"
10658
}
10759

10860
resolve_merge_base_sha() {
@@ -131,9 +83,14 @@ if [[ -z "${BASELINE_PIPELINE_ID}" ]]; then
13183

13284
BASELINE_SHA="$(git rev-parse "origin/${TARGET_BRANCH}" 2>/dev/null || true)"
13385
if [[ -z "${BASELINE_SHA}" ]]; then
134-
BASELINE_SHA="$(get_branch_head_sha "${TARGET_BRANCH}")"
86+
BASELINE_SHA="$(get_branch_head_sha "${TARGET_BRANCH}" || true)"
13587
fi
13688
BASELINE_PIPELINE_ID="$(get_pipeline_id_for_commit "${BASELINE_SHA}" || true)"
89+
90+
# use latest successful branch pipeline
91+
if [[ -z "${BASELINE_PIPELINE_ID}" ]]; then
92+
BASELINE_PIPELINE_ID="$(get_latest_pipeline_id_for_branch "${TARGET_BRANCH}" || true)"
93+
fi
13794
fi
13895

13996
if [[ -z "${BASELINE_PIPELINE_ID}" ]]; then

0 commit comments

Comments
 (0)