|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Fetch the latest ci-logs artifact for a branch (default: dev) using GitHub CLI |
| 5 | +# Requirements: gh CLI authenticated (GH_TOKEN or gh auth login) |
| 6 | +# Usage: |
| 7 | +# tools/fetch-latest-ci-logs.sh [branch] [output_dir] |
| 8 | +# tools/fetch-latest-ci-logs.sh --run <run-id> [output_dir] |
| 9 | +# tools/fetch-latest-ci-logs.sh --help |
| 10 | + |
| 11 | +usage() { |
| 12 | + cat <<EOF |
| 13 | +Fetch latest ci-logs artifact from GitHub Actions. |
| 14 | +
|
| 15 | +Usage: |
| 16 | + $(basename "$0") [branch] [output_dir] |
| 17 | + $(basename "$0") --run <run-id> [output_dir] |
| 18 | +
|
| 19 | +Examples: |
| 20 | + $(basename "$0") # fetch from branch 'dev' into ./ci_logs |
| 21 | + $(basename "$0") main ./out # fetch from branch 'main' into ./out |
| 22 | + $(basename "$0") --run 123456789 # fetch by run id into ./ci_logs |
| 23 | +
|
| 24 | +Notes: |
| 25 | + - Requires 'gh' CLI with repo read access. Run: gh auth login |
| 26 | + - Artifact name expected: ci-logs (from CI workflow) |
| 27 | +EOF |
| 28 | +} |
| 29 | + |
| 30 | +if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then |
| 31 | + usage; exit 0 |
| 32 | +fi |
| 33 | + |
| 34 | +if ! command -v gh >/dev/null 2>&1; then |
| 35 | + echo "Error: 'gh' CLI not found. Install from https://cli.github.com/" >&2 |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +ARTIFACT_NAME="ci-logs" |
| 40 | +BRANCH="dev" |
| 41 | +RUN_ID="" |
| 42 | +OUTDIR="ci_logs" |
| 43 | + |
| 44 | +if [[ "${1:-}" == "--run" ]]; then |
| 45 | + RUN_ID="${2:-}" |
| 46 | + if [[ -z "$RUN_ID" ]]; then echo "Error: --run requires a run id" >&2; exit 2; fi |
| 47 | + OUTDIR="${3:-$OUTDIR}" |
| 48 | +else |
| 49 | + BRANCH="${1:-$BRANCH}" |
| 50 | + OUTDIR="${2:-$OUTDIR}" |
| 51 | +fi |
| 52 | + |
| 53 | +mkdir -p "$OUTDIR" |
| 54 | + |
| 55 | +if [[ -n "$RUN_ID" ]]; then |
| 56 | + echo "Downloading artifact '$ARTIFACT_NAME' from run $RUN_ID into $OUTDIR ..." |
| 57 | + gh run download "$RUN_ID" --name "$ARTIFACT_NAME" --dir "$OUTDIR" |
| 58 | +else |
| 59 | + echo "Resolving latest run on branch '$BRANCH' ..." |
| 60 | + RUN_ID=$(gh run list --branch "$BRANCH" --limit 1 --json databaseId --jq '.[0].databaseId') |
| 61 | + if [[ -z "$RUN_ID" || "$RUN_ID" == "null" ]]; then |
| 62 | + echo "Error: No runs found on branch '$BRANCH'" >&2 |
| 63 | + exit 3 |
| 64 | + fi |
| 65 | + echo "Downloading artifact '$ARTIFACT_NAME' from run $RUN_ID into $OUTDIR ..." |
| 66 | + gh run download "$RUN_ID" --name "$ARTIFACT_NAME" --dir "$OUTDIR" |
| 67 | +fi |
| 68 | + |
| 69 | +echo "Downloaded files:" |
| 70 | +find "$OUTDIR" -maxdepth 2 -type f -print |
| 71 | + |
| 72 | +SUMMARY_FILE="$OUTDIR/ci-logs/ci_summary.txt" |
| 73 | +if [[ -f "$SUMMARY_FILE" ]]; then |
| 74 | + echo |
| 75 | + echo "--- CI Summary (tail) ---" |
| 76 | + tail -n 200 "$SUMMARY_FILE" || true |
| 77 | +fi |
| 78 | + |
| 79 | +echo "Done." |
| 80 | + |
0 commit comments