Skip to content

Commit de393b0

Browse files
Copilotleofang
andcommitted
Implement automatic run ID lookup from git tag in release workflow
Co-authored-by: leofang <5534781+leofang@users.noreply.github.com>
1 parent 2f4af79 commit de393b0

2 files changed

Lines changed: 118 additions & 5 deletions

File tree

.github/workflows/release.yml

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ 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
3030
build-ctk-ver:
3131
type: string
@@ -43,6 +43,31 @@ defaults:
4343
shell: bash --noprofile --norc -xeuo pipefail {0}
4444

4545
jobs:
46+
determine-run-id:
47+
runs-on: ubuntu-latest
48+
outputs:
49+
run-id: ${{ steps.lookup-run-id.outputs.run-id }}
50+
steps:
51+
- name: Checkout Source
52+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
53+
with:
54+
fetch-depth: 0
55+
56+
- name: Determine Run ID
57+
id: lookup-run-id
58+
env:
59+
GH_TOKEN: ${{ github.token }}
60+
run: |
61+
if [[ -n "${{ inputs.run-id }}" ]]; then
62+
echo "Using provided run ID: ${{ inputs.run-id }}"
63+
echo "run-id=${{ inputs.run-id }}" >> $GITHUB_OUTPUT
64+
else
65+
echo "Auto-detecting run ID for tag: ${{ inputs.git-tag }}"
66+
RUN_ID=$(./ci/tools/lookup-run-id "${{ inputs.git-tag }}" "${{ github.repository }}")
67+
echo "Auto-detected run ID: $RUN_ID"
68+
echo "run-id=$RUN_ID" >> $GITHUB_OUTPUT
69+
fi
70+
4671
check-tag:
4772
runs-on: ubuntu-latest
4873
steps:
@@ -91,13 +116,14 @@ jobs:
91116
pull-requests: write
92117
needs:
93118
- check-tag
119+
- determine-run-id
94120
secrets: inherit
95121
uses: ./.github/workflows/build-docs.yml
96122
with:
97123
build-ctk-ver: ${{ inputs.build-ctk-ver }}
98124
component: ${{ inputs.component }}
99125
git-tag: ${{ inputs.git-tag }}
100-
run-id: ${{ inputs.run-id }}
126+
run-id: ${{ needs.determine-run-id.outputs.run-id }}
101127
is-release: true
102128

103129
upload-archive:
@@ -106,18 +132,20 @@ jobs:
106132
contents: write
107133
needs:
108134
- check-tag
135+
- determine-run-id
109136
secrets: inherit
110137
uses: ./.github/workflows/release-upload.yml
111138
with:
112139
git-tag: ${{ inputs.git-tag }}
113-
run-id: ${{ inputs.run-id }}
140+
run-id: ${{ needs.determine-run-id.outputs.run-id }}
114141
component: ${{ inputs.component }}
115142

116143
publish-wheels:
117144
name: Publish wheels
118145
runs-on: ubuntu-latest
119146
needs:
120147
- check-tag
148+
- determine-run-id
121149
environment:
122150
name: ${{ inputs.wheel-dst }}
123151
url: https://${{ (inputs.wheel-dst == 'testpypi' && 'test.') || '' }}pypi.org/p/${{ inputs.component }}/
@@ -131,7 +159,7 @@ jobs:
131159
env:
132160
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
133161
run: |
134-
./ci/tools/download-wheels "${{ inputs.run-id }}" "${{ inputs.component }}" "${{ github.repository }}" "dist"
162+
./ci/tools/download-wheels "${{ needs.determine-run-id.outputs.run-id }}" "${{ inputs.component }}" "${{ github.repository }}" "dist"
135163
136164
- name: Publish package distributions to PyPI
137165
if: ${{ inputs.wheel-dst == 'pypi' }}

ci/tools/lookup-run-id

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 a GitHub token
30+
if [[ -z "${GH_TOKEN:-}" ]]; then
31+
echo "Error: GH_TOKEN environment variable is required" >&2
32+
exit 1
33+
fi
34+
35+
echo "Looking up run ID for tag: $GIT_TAG in repository: $REPOSITORY" >&2
36+
37+
# Resolve git tag to commit SHA
38+
if ! COMMIT_SHA=$(git rev-parse "$GIT_TAG" 2>/dev/null); then
39+
echo "Error: Could not resolve git tag '$GIT_TAG' to a commit SHA" >&2
40+
echo "Make sure the tag exists and you have fetched it" >&2
41+
exit 1
42+
fi
43+
44+
echo "Resolved tag '$GIT_TAG' to commit: $COMMIT_SHA" >&2
45+
46+
# Find workflow runs for this commit
47+
echo "Searching for '$WORKFLOW_NAME' workflow runs for commit: $COMMIT_SHA" >&2
48+
49+
# Get workflow runs for the commit, filter by workflow name and successful status
50+
RUN_DATA=$(gh run list \
51+
--repo "$REPOSITORY" \
52+
--commit "$COMMIT_SHA" \
53+
--workflow "$WORKFLOW_NAME" \
54+
--status success \
55+
--json databaseId,workflowName,status,conclusion,headSha \
56+
--limit 10)
57+
58+
if [[ -z "$RUN_DATA" || "$RUN_DATA" == "[]" ]]; then
59+
echo "Error: No successful '$WORKFLOW_NAME' workflow runs found for commit $COMMIT_SHA" >&2
60+
echo "Available workflow runs for this commit:" >&2
61+
gh run list --repo "$REPOSITORY" --commit "$COMMIT_SHA" --limit 10 || true
62+
exit 1
63+
fi
64+
65+
# Extract the run ID from the first (most recent) successful run
66+
RUN_ID=$(echo "$RUN_DATA" | jq -r '.[0].databaseId // empty')
67+
68+
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
71+
exit 1
72+
fi
73+
74+
echo "Found workflow run ID: $RUN_ID for tag '$GIT_TAG'" >&2
75+
76+
# Verify the run has the expected artifacts by checking if there are any artifacts
77+
echo "Verifying artifacts exist for run $RUN_ID..." >&2
78+
ARTIFACT_LIST=$(gh run view "$RUN_ID" --repo "$REPOSITORY" --json url 2>/dev/null || echo "")
79+
80+
if [[ -z "$ARTIFACT_LIST" ]]; then
81+
echo "Warning: Could not verify artifacts for workflow run $RUN_ID" >&2
82+
fi
83+
84+
# Output the run ID (this is what gets used by calling scripts)
85+
echo "$RUN_ID"

0 commit comments

Comments
 (0)