Skip to content

Commit e002926

Browse files
committed
feat: create helm chart repository in gh-pages
try with helm index.yaml (@kromanow94) find all helm chart releases and put to index.yaml (@kromanow94) add chart description and version (@kromanow94) add bash script for helm repo index.yaml generation from releases (@kromanow94) script doesnt need token, rm unused python version, move to hack/, run from publish-helm gha workflow push changes remove unused py file create clean branch for gh-pages dont delete the script we need typo typo gh-pages html remove debug logs git fetch if statement wasnt working copy needed files from hack to .tmp to use in empty gh-pages debug debug cleanup debug logs remove test file fix: token not needed to install helm fix: change trigger to release published
1 parent dc2f488 commit e002926

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed

.github/workflows/publish-helm.yaml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Publish Helm Charts
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
release:
9+
10+
permissions:
11+
contents: write
12+
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
with:
18+
fetch-depth: 0
19+
20+
- name: Install Helm
21+
uses: azure/setup-helm@v4
22+
23+
- name: Install Helm HTML Plugin
24+
run: helm plugin install https://github.com/halkeye/helm-repo-html
25+
26+
- name: Build Helm Chart Repo index.yaml
27+
run: |
28+
# this script from main will be gone on gh-pages branch,
29+
# so lets stash it away in a temp directory in the directory above this one
30+
mkdir ../.tmp
31+
mv hack/find_helm_chart_releases_and_create_helm_index.sh ../.tmp/
32+
mv hack/gh-pages.tmpl ../.tmp/
33+
34+
PAGES_BRANCH="gh-pages"
35+
36+
git fetch --all --tags
37+
38+
if git show-ref --verify --quiet refs/heads/$PAGES_BRANCH || git ls-remote --exit-code --heads origin $PAGES_BRANCH; then
39+
# Branch exists
40+
git checkout $PAGES_BRANCH
41+
echo "Checked out existing branch '$PAGES_BRANCH'"
42+
else
43+
# Branch does not exist
44+
git symbolic-ref HEAD refs/heads/$PAGES_BRANCH
45+
rm .git/index
46+
git clean -fdx
47+
echo ".tmp/" > .gitignore
48+
echo "Created and checked out new branch '$PAGES_BRANCH'"
49+
fi
50+
51+
mv ../.tmp ./
52+
.tmp/find_helm_chart_releases_and_create_helm_index.sh
53+
helm repo-html -t .tmp/gh-pages.tmpl
54+
55+
if output=$(git status --porcelain) && [ -z "$output" ]; then
56+
# Working directory clean
57+
58+
echo "No changes to commit"
59+
else
60+
# Uncommitted changes
61+
echo "Changes detected"
62+
git status
63+
echo "Committing..."
64+
65+
git config --local user.email "github-actions[bot]@users.noreply.github.com"
66+
git config --local user.name "github-actions[bot]"
67+
git add -A
68+
git commit -m "update helm chart repository index.yaml"
69+
70+
git push origin gh-pages
71+
fi

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,6 @@ tags
8080
.vscode/*
8181
.history
8282
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
83+
84+
charts/
85+
.tmp/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# GitHub Repository and API token
5+
REPO="knative/operator"
6+
CHART_NAME="knative-operator"
7+
8+
# GitHub API URL for releases
9+
GITHUB_API="https://api.github.com/repos/$REPO/releases"
10+
11+
# Create an empty index.yaml file with 2-space indentation
12+
cat > index.yaml <<EOF
13+
apiVersion: v1
14+
entries:
15+
$CHART_NAME:
16+
EOF
17+
18+
# Function to fetch all releases and filter .tgz files
19+
fetch_tgz_assets() {
20+
echo "Fetching release assets from GitHub API..." >&2
21+
22+
# Get response from GitHub API
23+
response=$(curl "$GITHUB_API")
24+
25+
# Check if the response is valid JSON
26+
if ! echo "$response" | jq 1>/dev/null 2>&1; then
27+
echo "(fetch_tgz_assets) Error: The response is not valid JSON. Here's the raw response:" >&2
28+
echo "$response" >&2
29+
exit 1
30+
fi
31+
32+
# Parse the response using jq to get the list of .tgz files
33+
echo "$response" | jq -c '.[] | .assets[] | select(.name | test("'$CHART_NAME'-(v?\\d+\\.\\d+\\.\\d+)\\.tgz")) | {url: .browser_download_url, name: .name, published: .updated_at}'
34+
}
35+
36+
# Function to process each .tgz file and append chart metadata to index.yaml
37+
process_tgz() {
38+
local url=$1
39+
local name=$2
40+
local published=$3
41+
42+
echo "Processing $name from $url" >&2
43+
44+
# Download the .tgz file
45+
curl -L -s -o "$name" "$url"
46+
47+
# Extract the Chart.yaml and values.yaml
48+
tar -xf "$name" "$CHART_NAME/Chart.yaml" "$CHART_NAME/values.yaml"
49+
50+
# Parse description from Chart.yaml
51+
DESCRIPTION=$(yq -r '.description' $CHART_NAME/Chart.yaml)
52+
53+
# Parse version from Chart.yaml (used as appVersion)
54+
CHART_VERSION="$(yq -r '.version' $CHART_NAME/Chart.yaml)"
55+
56+
# Calculate the SHA-256 digest
57+
DIGEST=$(sha256sum "$name" | cut -d' ' -f1)
58+
59+
# Append the chart metadata under the existing $CHART_NAME key
60+
cat >> index.yaml <<EOF
61+
- name: "$CHART_NAME"
62+
apiVersion: v2
63+
version: "v$CHART_VERSION"
64+
appVersion: "$CHART_VERSION"
65+
description: "$DESCRIPTION"
66+
created: "$published"
67+
urls:
68+
- "$url"
69+
digest: "$DIGEST"
70+
EOF
71+
72+
# Cleanup
73+
rm -f "$name"
74+
rm -f $CHART_NAME/Chart.yaml $CHART_NAME/values.yaml
75+
}
76+
77+
# Fetch all .tgz assets
78+
tgz_assets=$(fetch_tgz_assets)
79+
80+
# Loop through all the assets and process them
81+
echo "$tgz_assets" | while read -r asset; do
82+
# Check if each asset is valid JSON
83+
if ! echo "$asset" | jq '.' > /dev/null 2>&1; then
84+
echo "Error: Invalid JSON in asset line. Here's the raw asset line:" >&2
85+
echo "$asset" >&2
86+
continue
87+
fi
88+
89+
# Parse fields from the asset JSON
90+
url=$(echo "$asset" | jq -r '.url')
91+
name=$(echo "$asset" | jq -r '.name' | sed 's/.tgz$//') # Strip ".tgz" from name
92+
published=$(echo "$asset" | jq -r '.published')
93+
94+
process_tgz "$url" "$name" "$published"
95+
done
96+
97+
echo "index.yaml generated successfully!"

hack/gh-pages.tmpl

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Helm Charts - Knative</title>
7+
<style>
8+
body {
9+
font-family: Arial, sans-serif;
10+
background-color: #f4f6f8;
11+
color: #2c3e50;
12+
margin: 0;
13+
padding: 20px;
14+
display: flex;
15+
justify-content: center;
16+
}
17+
.container {
18+
max-width: 900px;
19+
width: 100%;
20+
}
21+
h1 {
22+
font-size: 28px;
23+
color: #333;
24+
font-weight: bold;
25+
margin-bottom: 20px;
26+
}
27+
.usage, .chart-list {
28+
background: #fff;
29+
padding: 20px;
30+
border-radius: 10px;
31+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
32+
margin-bottom: 20px;
33+
}
34+
.usage-code {
35+
background: #333;
36+
color: #fff;
37+
padding: 12px;
38+
border-radius: 5px;
39+
font-family: "Courier New", monospace;
40+
display: block;
41+
font-size: 14px;
42+
}
43+
.chart-item {
44+
margin-bottom: 20px;
45+
padding: 15px;
46+
border: 1px solid #ddd;
47+
border-radius: 8px;
48+
}
49+
.chart-item h2 {
50+
font-size: 18px;
51+
margin: 0;
52+
color: #007d9c;
53+
}
54+
.chart-item a {
55+
color: #007d9c;
56+
text-decoration: none;
57+
font-weight: 500;
58+
}
59+
.chart-versions {
60+
font-size: 14px;
61+
color: #555;
62+
margin-top: 5px;
63+
}
64+
.version-label {
65+
font-weight: bold;
66+
color: #333;
67+
}
68+
.chart-description {
69+
font-size: 14px;
70+
color: #777;
71+
margin-top: 4px;
72+
}
73+
</style>
74+
</head>
75+
<body>
76+
77+
<div class="container">
78+
<h1>Helm Charts</h1>
79+
80+
<div class="usage">
81+
<h2>Usage</h2>
82+
<code class="usage-code">
83+
helm repo add knative-operator https://knative.github.io/operator<br/>
84+
helm show values knative-operator/knative-operator
85+
</code>
86+
</div>
87+
88+
<div class="chart-list">
89+
<h2>Charts</h2>
90+
91+
{{range $entriesKey, $chartEntries := .Entries }}
92+
{{range $chartKey, $chart := $chartEntries }}
93+
<div class="chart-item">
94+
<h2><a href="{{ (index $chart.Urls 0) }}" title="{{ (index $chart.Urls 0) }}">{{ $chart.Name }}</a></h2>
95+
<div class="chart-versions">
96+
<span class="version-label">Chart Version:</span> {{ $chart.Version }} |
97+
<span class="version-label">App Version:</span> {{ $chart.AppVersion }}
98+
</div>
99+
<p class="chart-description">{{ $chart.Description }}</p>
100+
</div>
101+
{{end}}
102+
{{end}}
103+
104+
</div>
105+
</div>
106+
107+
</body>
108+
</html>

0 commit comments

Comments
 (0)