Skip to content

Commit b216b82

Browse files
committed
ci(benchmark): wait for base branch benchmark run to succeed
1 parent 7d8efd8 commit b216b82

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

.github/workflows/benchmark.yml

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,88 @@ defaults:
4343
shell: bash
4444

4545
jobs:
46+
# Add a new job to check if base branch run is completed
47+
check-base-benchmark:
48+
name: Check base branch benchmark
49+
if: github.event_name == 'pull_request'
50+
runs-on: ubuntu-latest
51+
steps:
52+
- name: Checkout repository
53+
uses: taiki-e/checkout-action@b13d20b7cda4e2f325ef19895128f7ff735c0b3d # v1.3.1
54+
55+
- name: Wait for base branch benchmark run to succeed
56+
env:
57+
GH_TOKEN: ${{ github.token }}
58+
REPO: ${{ github.repository }}
59+
BASE_BRANCH_NAME: ${{ github.event.pull_request.base.ref }}
60+
WORKFLOW_FILE_NAME: ${{ github.workflow }}
61+
MAX_WAIT_MINUTES: 15
62+
run: |
63+
echo "Looking for the latest workflow run on '$BASE_BRANCH_NAME' for '$WORKFLOW_FILE_NAME'..."
64+
INITIAL_RUN_ID=""
65+
CURRENT_RUN_ID=""
66+
67+
# Try to find the latest run on the base branch initially
68+
INITIAL_RUN_ID=$(gh run list --workflow "$WORKFLOW_FILE_NAME" --branch "$BASE_BRANCH_NAME" --limit 1 --json databaseId --jq '.[0].databaseId // empty')
69+
70+
if [ -z "$INITIAL_RUN_ID" ]; then
71+
echo "Warning: Could not find a recent run on '$BASE_BRANCH_NAME'. Proceeding without waiting."
72+
else
73+
echo "Found initial target run ID: $INITIAL_RUN_ID. Starting to monitor..."
74+
WAIT_START_TIME=$(date +%s)
75+
MAX_WAIT_SECONDS=$((MAX_WAIT_MINUTES * 60))
76+
CURRENT_RUN_ID=$INITIAL_RUN_ID
77+
78+
while true; do
79+
# Re-check for the latest workflow run in case there were new pushes
80+
LATEST_RUN_ID=$(gh run list --workflow "$WORKFLOW_FILE_NAME" --branch "$BASE_BRANCH_NAME" --limit 1 --json databaseId --jq '.[0].databaseId // empty')
81+
82+
# If we found a newer run ID than what we were tracking, switch to it
83+
if [ "$LATEST_RUN_ID" != "$CURRENT_RUN_ID" ]; then
84+
echo "Detected a newer workflow run ($LATEST_RUN_ID) on '$BASE_BRANCH_NAME'. Switching monitoring target."
85+
CURRENT_RUN_ID=$LATEST_RUN_ID
86+
fi
87+
88+
# Check the status of the current run we're monitoring
89+
RUN_INFO=$(gh run view "$CURRENT_RUN_ID" --json status,conclusion --jq '{status: .status, conclusion: .conclusion}')
90+
STATUS=$(echo "$RUN_INFO" | jq -r '.status')
91+
CONCLUSION=$(echo "$RUN_INFO" | jq -r '.conclusion')
92+
93+
echo "Run $CURRENT_RUN_ID status: $STATUS, conclusion: $CONCLUSION"
94+
if [ "$STATUS" == "completed" ]; then
95+
if [ "$CONCLUSION" == "success" ]; then
96+
echo "Run $CURRENT_RUN_ID completed successfully. Proceeding."
97+
break
98+
else
99+
# Allow to proceed if the base branch is main
100+
if [ "$BASE_BRANCH_NAME" == "main" ]; then
101+
echo "Warning: Run $CURRENT_RUN_ID on main branch completed with conclusion '$CONCLUSION'. Proceeding anyway."
102+
break
103+
else
104+
echo "Error: Run $CURRENT_RUN_ID on branch '$BASE_BRANCH_NAME' completed with conclusion '$CONCLUSION'. Cannot establish baseline."
105+
exit 1
106+
fi
107+
fi
108+
fi
109+
110+
CURRENT_TIME=$(date +%s)
111+
ELAPSED_TIME=$((CURRENT_TIME - WAIT_START_TIME))
112+
if [ "$ELAPSED_TIME" -ge "$MAX_WAIT_SECONDS" ]; then
113+
echo "Warning: Timeout waiting after ${MAX_WAIT_MINUTES} minutes. The current run $CURRENT_RUN_ID has status '$STATUS'. Proceeding anyway."
114+
break
115+
fi
116+
117+
echo "Waiting 30 seconds before checking again..."
118+
sleep 30
119+
done
120+
fi
121+
shell: bash
122+
46123
# Build and run benchmarks for all components except linter
47124
benchmark:
48125
name: Benchmark
126+
needs: check-base-benchmark
127+
if: ${{ success() || github.event_name != 'pull_request' }}
49128
runs-on: ubuntu-latest
50129
strategy:
51130
fail-fast: true
@@ -92,6 +171,7 @@ jobs:
92171
# But only build the linter benchmark once.
93172
build-linter:
94173
name: Build Linter Benchmark
174+
if: ${{ success() || github.event_name != 'pull_request' }}
95175
runs-on: ubuntu-latest
96176
steps:
97177
- name: Checkout Branch
@@ -123,7 +203,7 @@ jobs:
123203
# Run linter benchmarks. Each fixture in a separate job.
124204
benchmark-linter:
125205
name: Benchmark linter
126-
needs: build-linter
206+
needs: [check-base-benchmark, build-linter]
127207
runs-on: ubuntu-latest
128208
strategy:
129209
fail-fast: true

0 commit comments

Comments
 (0)