Skip to content

Commit 1f7aff6

Browse files
committed
Harden script and workflow
1 parent 9c7a0fe commit 1f7aff6

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

.github/workflows/update-toolhive-reference.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@ name: Update ToolHive Reference Docs
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: 'ToolHive version to update reference docs for'
8+
required: true
9+
default: 'latest'
510
repository_dispatch:
611
types: [published-release]
712

813
permissions:
914
contents: write
1015
pull-requests: write
1116

17+
concurrency:
18+
group: update-toolhive-reference
19+
cancel-in-progress: false
20+
1221
jobs:
1322
update-reference:
1423
runs-on: ubuntu-latest
@@ -23,11 +32,23 @@ jobs:
2332
git config --global user.name "github-actions[bot]"
2433
git config --global user.email "github-actions[bot]@users.noreply.github.com"
2534
26-
- name: Run update-toolhive-reference.sh and capture version
35+
- name: Determine version
36+
id: get-version
37+
run: |
38+
if [[ "${{ github.event_name }}" == "repository_dispatch" ]]; then
39+
echo "version=${{ github.event.client_payload.version }}" >> $GITHUB_OUTPUT
40+
else
41+
echo "version=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
42+
fi
43+
44+
- name: Run update script
2745
id: imports
2846
run: |
2947
chmod +x scripts/update-toolhive-reference.sh
30-
scripts/update-toolhive-reference.sh
48+
if ! scripts/update-toolhive-reference.sh ${{ steps.get-version.outputs.version }}; then
49+
echo "::error::Failed to update ToolHive reference docs"
50+
exit 1
51+
fi
3152
3253
- name: Check for changes
3354
id: git-diff

scripts/update-toolhive-reference.sh

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#!/bin/bash
22

3+
set -euo pipefail
4+
35
REPO_ROOT=$(git rev-parse --show-toplevel)
4-
cd $REPO_ROOT
6+
cd "$REPO_ROOT"
57

68
IMPORT_DIR="./imports"
79
DOCS_DIR="./docs"
@@ -20,29 +22,40 @@ if [ ! -d "$STATIC_DIR" ]; then
2022
exit 1
2123
fi
2224

25+
# Check if jq is installed
26+
if ! command -v jq >/dev/null 2>&1; then
27+
echo "Error: 'jq' is required but not installed. Please install jq and try again."
28+
exit 1
29+
fi
30+
31+
VERSION=$(echo "${1:-}" | tr -cd '[:alnum:].-')
32+
2333
# Handle tag name parameter - default to "latest" if not provided
24-
if [ -z "$1" ]; then
34+
if [ -z "$VERSION" ] || [ "$VERSION" = "latest" ]; then
2535
API_ENDPOINT="https://api.github.com/repos/stacklok/toolhive/releases/latest"
26-
echo "No tag specified, using latest release"
36+
echo "No tag specified or 'latest' specified, using latest release"
2737
else
28-
TAG_NAME="$1"
38+
TAG_NAME="$VERSION"
2939
API_ENDPOINT="https://api.github.com/repos/stacklok/toolhive/releases/tags/$TAG_NAME"
3040
echo "Using specified tag: $TAG_NAME"
3141
fi
3242

3343
# Fetch release information
34-
RELEASE_JSON=$(curl -s "$API_ENDPOINT")
35-
RELEASE_TARBALL=$(echo "$RELEASE_JSON" | grep "tarball_url" | cut -d '"' -f 4)
36-
RELEASE_VERSION=$(echo "$RELEASE_JSON" | grep '"tag_name"' | cut -d '"' -f 4)
44+
RELEASE_JSON=$(curl -sf "$API_ENDPOINT" || {
45+
echo "Failed to fetch release information from GitHub API"
46+
exit 1
47+
})
48+
RELEASE_TARBALL=$(echo "$RELEASE_JSON" | jq -r '.tarball_url // empty')
49+
RELEASE_VERSION=$(echo "$RELEASE_JSON" | jq -r '.tag_name // empty')
3750

3851
if [ -z "$RELEASE_TARBALL" ]; then
3952
echo "Failed to get release tarball URL for release: ${RELEASE_VERSION}"
4053
echo "Please check if the tag exists in the repository"
4154
exit 1
4255
fi
4356

44-
# Output the release version for use in CI workflows
45-
if [ ! -z "$GITHUB_OUTPUT" ]; then
57+
# Output the release version for use in CI workflows (if running in GitHub Actions)
58+
if [ -n "${GITHUB_OUTPUT:-}" ]; then
4659
echo "version=$RELEASE_VERSION" >> "$GITHUB_OUTPUT"
4760
fi
4861

@@ -54,7 +67,7 @@ echo "Fetching ToolHive release (${RELEASE_VERSION}) from: $RELEASE_TARBALL"
5467
echo "Importing to: $IMPORT_DIR"
5568

5669
# Download and extract the release tarball
57-
curl -sL "$RELEASE_TARBALL" | tar xz --strip-components=1 -C ./imports/toolhive
70+
curl -sfL "$RELEASE_TARBALL" | tar xz --strip-components=1 -C ./imports/toolhive
5871

5972
# Determine release type and process accordingly
6073
if [[ "$RELEASE_VERSION" =~ ^v.* ]]; then

0 commit comments

Comments
 (0)