|
| 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 requires a successful CI run that was triggered by the tag push. |
| 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} (tag: ${GIT_TAG})" >&2 |
| 58 | + |
| 59 | +# Get completed workflow runs for this commit. |
| 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,headBranch,event,createdAt,url \ |
| 66 | + --limit 50) |
| 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 push runs from the tag ref. |
| 76 | +RUN_ID=$(echo "${RUN_DATA}" | jq -r --arg tag "${GIT_TAG}" ' |
| 77 | + map(select(.conclusion == "success" and .event == "push" and .headBranch == $tag)) |
| 78 | + | sort_by(.createdAt) |
| 79 | + | reverse |
| 80 | + | .[0].databaseId // empty |
| 81 | +') |
| 82 | + |
| 83 | +if [[ -z "${RUN_ID}" ]]; then |
| 84 | + echo "Error: No successful '${WORKFLOW_NAME}' workflow runs found for tag '${GIT_TAG}'." >&2 |
| 85 | + echo "This release workflow now requires artifacts from a tag-triggered CI run." >&2 |
| 86 | + echo "If you just pushed the tag, wait for CI on that tag to finish and retry." >&2 |
| 87 | + echo "" >&2 |
| 88 | + echo "Completed runs for commit ${COMMIT_SHA}:" >&2 |
| 89 | + echo "${RUN_DATA}" | jq -r '.[] | "\(.databaseId): event=\(.event // "null"), headBranch=\(.headBranch // "null"), conclusion=\(.conclusion // "null"), status=\(.status // "null"), createdAt=\(.createdAt // "null")"' >&2 |
| 90 | + exit 1 |
| 91 | +fi |
| 92 | + |
| 93 | +echo "Found workflow run ID: ${RUN_ID} for tag '${GIT_TAG}'" >&2 |
| 94 | + |
| 95 | +# Verify the run has the expected artifacts by checking if there are any artifacts |
| 96 | +echo "Verifying artifacts exist for run ${RUN_ID}..." >&2 |
| 97 | +ARTIFACT_LIST=$(gh run view "${RUN_ID}" --repo "${REPOSITORY}" --json url || echo "") |
| 98 | + |
| 99 | +if [[ -z "${ARTIFACT_LIST}" ]]; then |
| 100 | + echo "Warning: Could not verify artifacts for workflow run ${RUN_ID}" >&2 |
| 101 | +fi |
| 102 | + |
| 103 | +# Output the run ID (this is what gets used by calling scripts) |
| 104 | +echo "${RUN_ID}" |
0 commit comments