-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.just
More file actions
213 lines (177 loc) · 6.33 KB
/
chart.just
File metadata and controls
213 lines (177 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# Standalone justfile - intended to be invoked via:
# just --justfile chart.just --set chart_name=my-app <recipe>
#
# Upstream repos should use a thin dispatcher pattern:
# chart_name := "my-app"
# chart *args:
# @curl -sS https://raw.githubusercontent.com/hotosm/justfiles/main/chart.just \
# -o {{justfile_directory()}}/tasks/chart.just;
# @just --justfile {{justfile_directory()}}/tasks/chart.just \
# --set chart_name {{chart_name}} {{args}}
# Chart name - override via --set chart_name=my-app
chart_name := "chart"
# Project root - resolves to where `just` was invoked, not where this file lives.
# Override via --set project_dir=/path/to/repo if needed.
project_dir := invocation_directory()
_self := justfile()
# List available recipes
[private]
default:
@just --justfile {{_self}} --list
# Publish the packaged chart to an OCI registry.
# Registry URL is resolved in the script from env, not from Justfile interpolation.
# Defaults to ghcr.io/<owner>/charts, which results in charts at ghcr.io/<owner>/charts/<chartname>
publish registry="": build
#!/usr/bin/env bash
set -e
cd {{project_dir}}
echo "🔐 Logging in to Helm registry..."
# Resolve full registry path:
# 1. CLI arg: `just chart publish registry="ghcr.io/org/charts"`
# 2. Env: HELM_REGISTRY (e.g. "ghcr.io/hotosm/charts")
# 3. Default: ghcr.io/<owner>/charts
REGISTRY="{{ registry }}"
if [ -z "$REGISTRY" ]; then
if [ -n "${HELM_REGISTRY:-}" ]; then
REGISTRY="${HELM_REGISTRY}"
else
OWNER="${HELM_REGISTRY_OWNER:-${GITHUB_REPOSITORY_OWNER:-}}"
if [ -z "$OWNER" ]; then
echo "❌ Unable to determine chart registry owner."
echo " Provide one of:"
echo " - just chart publish registry=\"ghcr.io/<owner>/charts\""
echo " - HELM_REGISTRY=\"ghcr.io/<owner>/charts\""
echo " - HELM_REGISTRY_OWNER or GITHUB_REPOSITORY_OWNER"
exit 1
fi
REGISTRY="ghcr.io/${OWNER}/charts"
fi
fi
REGISTRY_HOST="$(printf '%s\n' "$REGISTRY" | cut -d/ -f1)"
echo "REGISTRY: ${REGISTRY}"
echo "REGISTRY_HOST: ${REGISTRY_HOST}"
# Prefer explicit Helm registry creds, fall back to GitHub defaults in CI
USERNAME="${HELM_REGISTRY_USERNAME:-${GITHUB_ACTOR:-}}"
PASSWORD="${HELM_REGISTRY_PASSWORD:-${GITHUB_TOKEN:-}}"
if [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
echo "❌ Missing registry credentials."
echo " Set HELM_REGISTRY_USERNAME / HELM_REGISTRY_PASSWORD"
echo " or rely on GITHUB_ACTOR / GITHUB_TOKEN in GitHub Actions."
exit 1
fi
echo "$PASSWORD" | helm registry login "$REGISTRY_HOST" \
--username "$USERNAME" \
--password-stdin
CHART_FILE=$(ls -t {{ chart_name }}-*.tgz | head -1)
if [ -z "$CHART_FILE" ]; then
echo "❌ No chart file found. Run build first"
exit 1
fi
echo "🚀 Publishing chart to OCI registry..."
echo "📦 Chart file: $CHART_FILE"
echo "🔎 Checking if chart version already exists in registry..."
CHART_VERSION=$(helm show chart "$CHART_FILE" | grep '^version:' | awk '{ print $2 }')
if helm show chart "oci://$REGISTRY/{{ chart_name }}" --version "$CHART_VERSION" >/dev/null 2>&1; then
echo "ℹ️ Chart version $CHART_VERSION already exists in OCI registry."
echo " Skipping push."
exit 0
fi
echo "📤 Pushing to: oci://$REGISTRY"
helm push "$CHART_FILE" "oci://$REGISTRY"
echo "✅ Chart published!"
# Build the Helm chart: lint, update deps, test, and package
build: _install-helm lint dependencies package
# Ensure Helm is available (module-local copy to avoid nested `just` calls).
[private]
_install-helm:
#!/usr/bin/env bash
set -e
if command -v helm &> /dev/null; then
exit 0
fi
echo "📦 Installing Helm..."
# Only Linux / amd64 automated install for now; otherwise instruct user
UNAME_S="$(uname -s || echo unknown)"
UNAME_M="$(uname -m || echo unknown)"
if [ "$UNAME_S" != "Linux" ] || { [ "$UNAME_M" != "x86_64" ] && [ "$UNAME_M" != "amd64" ]; }; then
echo "❌ Automatic Helm install only supported on Linux amd64."
echo " Please install Helm manually: https://helm.sh/docs/intro/install/"
exit 1
fi
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
# Get latest Helm release tag
HELM_TAG="$(curl -sSL https://api.github.com/repos/helm/helm/releases/latest | grep -oE '\"tag_name\":\s*\"v[0-9.]+\"' | head -1 | sed -E 's/\"tag_name\":\s*\"(v[0-9.]+)\"/\1/')"
if [ -z "$HELM_TAG" ]; then
echo "❌ Failed to determine latest Helm version."
exit 1
fi
ARCHIVE="helm-${HELM_TAG}-linux-amd64.tar.gz"
URL="https://get.helm.sh/${ARCHIVE}"
echo "⬇️ Downloading ${URL}..."
curl -sSL "$URL" -o "$TMP_DIR/helm.tar.gz"
tar -xzf "$TMP_DIR/helm.tar.gz" -C "$TMP_DIR"
if sudo mv "$TMP_DIR/linux-amd64/helm" /usr/local/bin/helm 2>/dev/null; then
chmod +x /usr/local/bin/helm
echo "✓ Helm installed to /usr/local/bin/helm"
else
mkdir -p "$HOME/.local/bin"
mv "$TMP_DIR/linux-amd64/helm" "$HOME/.local/bin/helm"
chmod +x "$HOME/.local/bin/helm"
export PATH="$HOME/.local/bin:$PATH"
echo "✓ Helm installed to ~/.local/bin/helm"
fi
if ! command -v helm &> /dev/null; then
echo "❌ Error: Failed to install Helm"
exit 1
fi
# Lint the Helm chart
lint:
#!/usr/bin/env sh
set -e
cd {{project_dir}}
echo "🔍 Linting chart..."
helm lint chart
echo "✓ Lint passed"
echo ""
# Update chart dependences
dependencies:
#!/usr/bin/env sh
set -e
cd {{project_dir}}/chart
echo "📥 Updating dependencies..."
helm dependency update
cd {{project_dir}}
echo "✓ Dependencies updated"
echo ""
# Package the Helm chart
package:
#!/usr/bin/env bash
set -e
cd {{project_dir}}
echo "📦 Packaging chart..."
helm package chart
PACKAGE=$(ls -t {{ chart_name }}-*.tgz | head -1)
echo "✓ Chart packaged: $PACKAGE"
echo ""
echo "✅ Build complete!"
# Render chart templates
template:
#!/usr/bin/env bash
set -e
cd {{project_dir}}
helm template {{ chart_name }} chart
# Show chart information
show:
#!/usr/bin/env bash
set -e
cd {{project_dir}}
helm show all chart
# Clean up packaged charts
clean:
#!/usr/bin/env bash
set -e
cd {{project_dir}}
rm -f {{ chart_name }}-*.tgz
rm -rf ./chart/charts/
echo "✓ Cleaned up chart artifacts"