Skip to content

Commit 1b78bb3

Browse files
Copilotleofangpre-commit-ci[bot]
authored
CI: Avoid manual lookup of the run ID in the release workflow (#918)
* Initial plan * Implement automatic run ID lookup from git tag in release workflow Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * Improve run ID lookup script with better error handling and tool validation Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * Add default empty string value to optional run-id input Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * Add comment explaining fetch-depth: 0 requirement for lookup-run-id script Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> * [pre-commit.ci] auto code formatting * apply review comments --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: leofang <5534781+leofang@users.noreply.github.com> Co-authored-by: Leo Fang <leof@nvidia.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 62e2650 commit 1b78bb3

2 files changed

Lines changed: 134 additions & 5 deletions

File tree

.github/workflows/release.yml

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ on:
2424
required: true
2525
type: string
2626
run-id:
27-
description: "The GHA run ID that generated validated artifacts"
28-
required: true
27+
description: "The GHA run ID that generated validated artifacts (optional - will be auto-detected from git tag if not provided)"
28+
required: false
2929
type: string
30+
default: ""
3031
build-ctk-ver:
3132
type: string
3233
required: true
@@ -43,6 +44,32 @@ defaults:
4344
shell: bash --noprofile --norc -xeuo pipefail {0}
4445

4546
jobs:
47+
determine-run-id:
48+
runs-on: ubuntu-latest
49+
outputs:
50+
run-id: ${{ steps.lookup-run-id.outputs.run-id }}
51+
steps:
52+
- name: Checkout Source
53+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
54+
with:
55+
# fetch-depth: 0 is required so the lookup-run-id script can access all git tags
56+
fetch-depth: 0
57+
58+
- name: Determine Run ID
59+
id: lookup-run-id
60+
env:
61+
GH_TOKEN: ${{ github.token }}
62+
run: |
63+
if [[ -n "${{ inputs.run-id }}" ]]; then
64+
echo "Using provided run ID: ${{ inputs.run-id }}"
65+
echo "run-id=${{ inputs.run-id }}" >> $GITHUB_OUTPUT
66+
else
67+
echo "Auto-detecting run ID for tag: ${{ inputs.git-tag }}"
68+
RUN_ID=$(./ci/tools/lookup-run-id "${{ inputs.git-tag }}" "${{ github.repository }}")
69+
echo "Auto-detected run ID: $RUN_ID"
70+
echo "run-id=$RUN_ID" >> $GITHUB_OUTPUT
71+
fi
72+
4673
check-tag:
4774
runs-on: ubuntu-latest
4875
steps:
@@ -91,13 +118,14 @@ jobs:
91118
pull-requests: write
92119
needs:
93120
- check-tag
121+
- determine-run-id
94122
secrets: inherit
95123
uses: ./.github/workflows/build-docs.yml
96124
with:
97125
build-ctk-ver: ${{ inputs.build-ctk-ver }}
98126
component: ${{ inputs.component }}
99127
git-tag: ${{ inputs.git-tag }}
100-
run-id: ${{ inputs.run-id }}
128+
run-id: ${{ needs.determine-run-id.outputs.run-id }}
101129
is-release: true
102130

103131
upload-archive:
@@ -106,18 +134,20 @@ jobs:
106134
contents: write
107135
needs:
108136
- check-tag
137+
- determine-run-id
109138
secrets: inherit
110139
uses: ./.github/workflows/release-upload.yml
111140
with:
112141
git-tag: ${{ inputs.git-tag }}
113-
run-id: ${{ inputs.run-id }}
142+
run-id: ${{ needs.determine-run-id.outputs.run-id }}
114143
component: ${{ inputs.component }}
115144

116145
publish-wheels:
117146
name: Publish wheels
118147
runs-on: ubuntu-latest
119148
needs:
120149
- check-tag
150+
- determine-run-id
121151
environment:
122152
name: ${{ inputs.wheel-dst }}
123153
url: https://${{ (inputs.wheel-dst == 'testpypi' && 'test.') || '' }}pypi.org/p/${{ inputs.component }}/
@@ -131,7 +161,7 @@ jobs:
131161
env:
132162
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
133163
run: |
134-
./ci/tools/download-wheels "${{ inputs.run-id }}" "${{ inputs.component }}" "${{ github.repository }}" "dist"
164+
./ci/tools/download-wheels "${{ needs.determine-run-id.outputs.run-id }}" "${{ inputs.component }}" "${{ github.repository }}" "dist"
135165
136166
- name: Publish package distributions to PyPI
137167
if: ${{ inputs.wheel-dst == 'pypi' }}

ci/tools/lookup-run-id

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env bash
2+
3+
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
# A utility script to find the GitHub Actions workflow run ID for a given git tag.
8+
# This script looks for the CI workflow run that corresponds to the commit of the given tag.
9+
10+
set -euo pipefail
11+
12+
# Check required arguments
13+
if [[ $# -lt 2 ]]; then
14+
echo "Usage: $0 <git-tag> <repository> [workflow-name]" >&2
15+
echo " git-tag: The git tag to find the corresponding workflow run for" >&2
16+
echo " repository: The GitHub repository (e.g., NVIDIA/cuda-python)" >&2
17+
echo " workflow-name: Optional workflow name to filter by (default: CI)" >&2
18+
echo "" >&2
19+
echo "Examples:" >&2
20+
echo " $0 v13.0.1 NVIDIA/cuda-python" >&2
21+
echo " $0 v13.0.1 NVIDIA/cuda-python \"CI\"" >&2
22+
exit 1
23+
fi
24+
25+
GIT_TAG="${1}"
26+
REPOSITORY="${2}"
27+
WORKFLOW_NAME="${3:-CI}"
28+
29+
# Ensure we have required tools
30+
if [[ -z "${GH_TOKEN:-}" ]]; then
31+
echo "Error: GH_TOKEN environment variable is required" >&2
32+
exit 1
33+
fi
34+
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+
45+
echo "Looking up run ID for tag: ${GIT_TAG} in repository: ${REPOSITORY}" >&2
46+
47+
# Resolve git tag to commit SHA
48+
if ! COMMIT_SHA=$(git rev-parse "${GIT_TAG}"); then
49+
echo "Error: Could not resolve git tag '${GIT_TAG}' to a commit SHA" >&2
50+
echo "Make sure the tag exists and you have fetched it" >&2
51+
exit 1
52+
fi
53+
54+
echo "Resolved tag '${GIT_TAG}' to commit: ${COMMIT_SHA}" >&2
55+
56+
# Find workflow runs for this commit
57+
echo "Searching for '${WORKFLOW_NAME}' workflow runs for commit: ${COMMIT_SHA}" >&2
58+
59+
# Get workflow runs for the commit, filter by workflow name and successful status
60+
RUN_DATA=$(gh run list \
61+
--repo "${REPOSITORY}" \
62+
--commit "${COMMIT_SHA}" \
63+
--workflow "${WORKFLOW_NAME}" \
64+
--status completed \
65+
--json databaseId,workflowName,status,conclusion,headSha \
66+
--limit 10)
67+
68+
if [[ -z "${RUN_DATA}" || "${RUN_DATA}" == "[]" ]]; then
69+
echo "Error: No completed '${WORKFLOW_NAME}' workflow runs found for commit ${COMMIT_SHA}" >&2
70+
echo "Available workflow runs for this commit:" >&2
71+
gh run list --repo "${REPOSITORY}" --commit "${COMMIT_SHA}" --limit 10 || true
72+
exit 1
73+
fi
74+
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)
77+
78+
if [[ -z "${RUN_ID}" || "${RUN_ID}" == "null" ]]; then
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
85+
exit 1
86+
fi
87+
88+
echo "Found workflow run ID: ${RUN_ID} for tag '${GIT_TAG}'" >&2
89+
90+
# Verify the run has the expected artifacts by checking if there are any artifacts
91+
echo "Verifying artifacts exist for run ${RUN_ID}..." >&2
92+
ARTIFACT_LIST=$(gh run view "${RUN_ID}" --repo "${REPOSITORY}" --json url || echo "")
93+
94+
if [[ -z "${ARTIFACT_LIST}" ]]; then
95+
echo "Warning: Could not verify artifacts for workflow run ${RUN_ID}" >&2
96+
fi
97+
98+
# Output the run ID (this is what gets used by calling scripts)
99+
echo "${RUN_ID}"

0 commit comments

Comments
 (0)