Skip to content

Commit 0502fcf

Browse files
Copilotleofang
andcommitted
Improve run ID lookup script with better error handling and tool validation
Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
1 parent de393b0 commit 0502fcf

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

ci/tools/lookup-run-id

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,22 @@ GIT_TAG="$1"
2626
REPOSITORY="$2"
2727
WORKFLOW_NAME="${3:-CI}"
2828

29-
# Ensure we have a GitHub token
29+
# Ensure we have required tools
3030
if [[ -z "${GH_TOKEN:-}" ]]; then
3131
echo "Error: GH_TOKEN environment variable is required" >&2
3232
exit 1
3333
fi
3434

35+
if ! command -v jq >/dev/null 2>&1; then
36+
echo "Error: jq is required but not installed" >&2
37+
exit 1
38+
fi
39+
40+
if ! command -v gh >/dev/null 2>&1; then
41+
echo "Error: GitHub CLI (gh) is required but not installed" >&2
42+
exit 1
43+
fi
44+
3545
echo "Looking up run ID for tag: $GIT_TAG in repository: $REPOSITORY" >&2
3646

3747
# Resolve git tag to commit SHA
@@ -51,23 +61,27 @@ RUN_DATA=$(gh run list \
5161
--repo "$REPOSITORY" \
5262
--commit "$COMMIT_SHA" \
5363
--workflow "$WORKFLOW_NAME" \
54-
--status success \
64+
--status completed \
5565
--json databaseId,workflowName,status,conclusion,headSha \
5666
--limit 10)
5767

5868
if [[ -z "$RUN_DATA" || "$RUN_DATA" == "[]" ]]; then
59-
echo "Error: No successful '$WORKFLOW_NAME' workflow runs found for commit $COMMIT_SHA" >&2
69+
echo "Error: No completed '$WORKFLOW_NAME' workflow runs found for commit $COMMIT_SHA" >&2
6070
echo "Available workflow runs for this commit:" >&2
6171
gh run list --repo "$REPOSITORY" --commit "$COMMIT_SHA" --limit 10 || true
6272
exit 1
6373
fi
6474

65-
# Extract the run ID from the first (most recent) successful run
66-
RUN_ID=$(echo "$RUN_DATA" | jq -r '.[0].databaseId // empty')
75+
# Filter for successful runs (conclusion = success) and extract the run ID from the first one
76+
RUN_ID=$(echo "$RUN_DATA" | jq -r '.[] | select(.conclusion == "success") | .databaseId' | head -1)
6777

6878
if [[ -z "$RUN_ID" || "$RUN_ID" == "null" ]]; then
69-
echo "Error: Could not extract run ID from workflow data" >&2
70-
echo "Workflow data: $RUN_DATA" >&2
79+
echo "Error: No successful '$WORKFLOW_NAME' workflow runs found for commit $COMMIT_SHA" >&2
80+
echo "Available workflow runs for this commit:" >&2
81+
gh run list --repo "$REPOSITORY" --commit "$COMMIT_SHA" --limit 10 || true
82+
echo "" >&2
83+
echo "Completed runs with their conclusions:" >&2
84+
echo "$RUN_DATA" | jq -r '.[] | "\(.databaseId): \(.conclusion)"' >&2
7185
exit 1
7286
fi
7387

0 commit comments

Comments
 (0)