diff --git a/.github/workflows/publish-helm.yaml b/.github/workflows/publish-helm.yaml new file mode 100644 index 0000000000..656b4f4f5b --- /dev/null +++ b/.github/workflows/publish-helm.yaml @@ -0,0 +1,74 @@ +name: Publish Helm Charts + +on: + push: + branches: + - main + +jobs: + release: + + permissions: + contents: write + + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Install Helm + uses: azure/setup-helm@v4 + env: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + + - name: Install Helm HTML Plugin + run: helm plugin install https://github.com/halkeye/helm-repo-html + + - name: Build Helm Chart Repo index.yaml + run: | + # this script from main will be gone on gh-pages branch, + # so lets stash it away in a temp directory in the directory above this one + mkdir ../.tmp + mv hack/find_helm_chart_releases_and_create_helm_index.sh ../.tmp/ + mv hack/gh-pages.tmpl ../.tmp/ + + PAGES_BRANCH="gh-pages" + + git fetch --all --tags + + if git show-ref --verify --quiet refs/heads/$PAGES_BRANCH || git ls-remote --exit-code --heads origin $PAGES_BRANCH; then + # Branch exists + git checkout $PAGES_BRANCH + echo "Checked out existing branch '$PAGES_BRANCH'" + else + # Branch does not exist + git symbolic-ref HEAD refs/heads/$PAGES_BRANCH + rm .git/index + git clean -fdx + echo ".tmp/" > .gitignore + echo "Created and checked out new branch '$PAGES_BRANCH'" + fi + + mv ../.tmp ./ + .tmp/find_helm_chart_releases_and_create_helm_index.sh + helm repo-html -t .tmp/gh-pages.tmpl + + if output=$(git status --porcelain) && [ -z "$output" ]; then + # Working directory clean + + echo "No changes to commit" + else + # Uncommitted changes + echo "Changes detected" + git status + echo "Committing..." + + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git add -A + git commit -m "update helm chart repository index.yaml" + + git push origin gh-pages + fi diff --git a/.gitignore b/.gitignore index 81308f48c9..5e363aef2a 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,6 @@ tags .vscode/* .history # End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode + +charts/ +.tmp/ \ No newline at end of file diff --git a/hack/find_helm_chart_releases_and_create_helm_index.sh b/hack/find_helm_chart_releases_and_create_helm_index.sh new file mode 100755 index 0000000000..fb0bde7e30 --- /dev/null +++ b/hack/find_helm_chart_releases_and_create_helm_index.sh @@ -0,0 +1,97 @@ +#!/bin/bash +set -e + +# GitHub Repository and API token +REPO="knative/operator" +CHART_NAME="knative-operator" + +# GitHub API URL for releases +GITHUB_API="https://api.github.com/repos/$REPO/releases" + +# Create an empty index.yaml file with 2-space indentation +cat > index.yaml <&2 + + # Get response from GitHub API + response=$(curl "$GITHUB_API") + + # Check if the response is valid JSON + if ! echo "$response" | jq 1>/dev/null 2>&1; then + echo "(fetch_tgz_assets) Error: The response is not valid JSON. Here's the raw response:" >&2 + echo "$response" >&2 + exit 1 + fi + + # Parse the response using jq to get the list of .tgz files + echo "$response" | jq -c '.[] | .assets[] | select(.name | test("'$CHART_NAME'-(v?\\d+\\.\\d+\\.\\d+)\\.tgz")) | {url: .browser_download_url, name: .name, published: .updated_at}' +} + +# Function to process each .tgz file and append chart metadata to index.yaml +process_tgz() { + local url=$1 + local name=$2 + local published=$3 + + echo "Processing $name from $url" >&2 + + # Download the .tgz file + curl -L -s -o "$name" "$url" + + # Extract the Chart.yaml and values.yaml + tar -xf "$name" "$CHART_NAME/Chart.yaml" "$CHART_NAME/values.yaml" + + # Parse description from Chart.yaml + DESCRIPTION=$(yq -r '.description' $CHART_NAME/Chart.yaml) + + # Parse version from Chart.yaml (used as appVersion) + CHART_VERSION="$(yq -r '.version' $CHART_NAME/Chart.yaml)" + + # Calculate the SHA-256 digest + DIGEST=$(sha256sum "$name" | cut -d' ' -f1) + + # Append the chart metadata under the existing $CHART_NAME key + cat >> index.yaml < /dev/null 2>&1; then + echo "Error: Invalid JSON in asset line. Here's the raw asset line:" >&2 + echo "$asset" >&2 + continue + fi + + # Parse fields from the asset JSON + url=$(echo "$asset" | jq -r '.url') + name=$(echo "$asset" | jq -r '.name' | sed 's/.tgz$//') # Strip ".tgz" from name + published=$(echo "$asset" | jq -r '.published') + + process_tgz "$url" "$name" "$published" +done + +echo "index.yaml generated successfully!" diff --git a/hack/gh-pages.tmpl b/hack/gh-pages.tmpl new file mode 100644 index 0000000000..8c4ecf5037 --- /dev/null +++ b/hack/gh-pages.tmpl @@ -0,0 +1,108 @@ + + + + + + Helm Charts - Knative + + + + +
+

Helm Charts

+ +
+

Usage

+ + helm repo add knative-operator https://knative.github.io/operator
+ helm show values knative-operator/knative-operator +
+
+ +
+

Charts

+ + {{range $entriesKey, $chartEntries := .Entries }} + {{range $chartKey, $chart := $chartEntries }} +
+

{{ $chart.Name }}

+
+ Chart Version: {{ $chart.Version }} | + App Version: {{ $chart.AppVersion }} +
+

{{ $chart.Description }}

+
+ {{end}} + {{end}} + +
+
+ + +