forked from helm/chart-releaser-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcr.sh
More file actions
executable file
·447 lines (397 loc) · 11.6 KB
/
Copy pathcr.sh
File metadata and controls
executable file
·447 lines (397 loc) · 11.6 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env bash
# Copyright The Helm Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
DEFAULT_CHART_RELEASER_VERSION=v1.7.0
show_help() {
cat <<EOF
Usage: $(basename "$0") <options>
-h, --help Display help
-v, --version The chart-releaser version to use (default: $DEFAULT_CHART_RELEASER_VERSION)"
--config The path to the chart-releaser config file
-d, --charts-dir The charts directory (default: charts)
-o, --owner The repo owner
-r, --repo The repo name
--pages-branch The repo pages branch
-n, --install-dir The Path to install the cr tool
-i, --install-only Just install the cr tool
-s, --skip-packaging Skip the packaging step (run your own packaging before using the releaser)
--skip-existing Skip package upload if release exists
--skip-upload Skip package upload, just create the release. Not needed in case of OCI upload.
-l, --mark-as-latest Mark the created GitHub release as 'latest' (default: true)
--packages-with-index Upload chart packages directly into publishing branch
--use-arm Use ARM64 binary (default: false)
--prerelease Mark the created GitHub releases as 'Pre-release' (default: false)
--pages-index-path The relative path to the index file in the publishing branch
EOF
}
main() {
local version="$DEFAULT_CHART_RELEASER_VERSION"
local config=
local charts_dir=charts
local owner=
local repo=
local install_dir=
local install_only=
local skip_packaging=
local skip_existing=
local skip_upload=
local mark_as_latest=true
local packages_with_index=false
local pages_branch=
local use_arm=false
local prerelease=false
local pages_index_path=
parse_command_line "$@"
: "${CR_TOKEN:?Environment variable CR_TOKEN must be set}"
local repo_root
repo_root=$(git rev-parse --show-toplevel)
pushd "$repo_root" >/dev/null
if [[ -z "$skip_packaging" ]]; then
echo 'Looking up latest tag...'
local latest_tag
latest_tag=$(lookup_latest_tag)
echo "Discovering changed charts since '$latest_tag'..."
local changed_charts=()
readarray -t changed_charts <<<"$(lookup_changed_charts "$latest_tag")"
if [[ -n "${changed_charts[*]}" ]]; then
install_chart_releaser
rm -rf .cr-release-packages
mkdir -p .cr-release-packages
rm -rf .cr-index
mkdir -p .cr-index
for chart in "${changed_charts[@]}"; do
if [[ -d "$chart" ]]; then
package_chart "$chart"
else
echo "Nothing to do. No chart changes detected."
fi
done
release_charts
update_index
echo "changed_charts=$(
IFS=,
echo "${changed_charts[*]}"
)" >changed_charts.txt
echo "chart_version=${latest_tag}" >chart_version.txt
else
echo "Nothing to do. No chart changes detected."
echo "changed_charts=" >changed_charts.txt
echo "chart_version=" >chart_version.txt
fi
else
install_chart_releaser
rm -rf .cr-index
mkdir -p .cr-index
release_charts
update_index
fi
# Deliberately last: marking pre-releases is cosmetic metadata on releases that
# already exist, so a failure here must not be able to abort the run before the
# index has been published.
mark_releases_prerelease
popd >/dev/null
}
parse_command_line() {
while :; do
case "${1:-}" in
-h | --help)
show_help
exit
;;
--config)
if [[ -n "${2:-}" ]]; then
config="$2"
shift
else
echo "ERROR: '--config' cannot be empty." >&2
show_help
exit 1
fi
;;
-v | --version)
if [[ -n "${2:-}" ]]; then
version="$2"
shift
else
echo "ERROR: '-v|--version' cannot be empty." >&2
show_help
exit 1
fi
;;
-d | --charts-dir)
if [[ -n "${2:-}" ]]; then
charts_dir="$2"
shift
else
echo "ERROR: '-d|--charts-dir' cannot be empty." >&2
show_help
exit 1
fi
;;
-o | --owner)
if [[ -n "${2:-}" ]]; then
owner="$2"
shift
else
echo "ERROR: '--owner' cannot be empty." >&2
show_help
exit 1
fi
;;
-r | --repo)
if [[ -n "${2:-}" ]]; then
repo="$2"
shift
else
echo "ERROR: '--repo' cannot be empty." >&2
show_help
exit 1
fi
;;
--pages-branch)
if [[ -n "${2:-}" ]]; then
pages_branch="$2"
shift
fi
;;
-n | --install-dir)
if [[ -n "${2:-}" ]]; then
install_dir="$2"
shift
fi
;;
-i | --install-only)
if [[ -n "${2:-}" ]]; then
install_only="$2"
shift
fi
;;
-s | --skip-packaging)
if [[ -n "${2:-}" ]]; then
skip_packaging="$2"
shift
fi
;;
--skip-existing)
if [[ -n "${2:-}" ]]; then
skip_existing="$2"
shift
fi
;;
--skip-upload)
if [[ -n "${2:-}" ]]; then
skip_upload="$2"
shift
fi
;;
-l | --mark-as-latest)
if [[ -n "${2:-}" ]]; then
mark_as_latest="$2"
shift
fi
;;
--packages-with-index)
if [[ -n "${2:-}" ]]; then
packages_with_index="$2"
shift
fi
;;
--use-arm)
if [[ -n "${2:-}" ]]; then
use_arm="$2"
shift
fi
;;
--prerelease)
if [[ -n "${2:-}" ]]; then
prerelease="$2"
shift
fi
;;
--pages-index-path)
if [[ -n "${2:-}" ]]; then
pages_index_path="$2"
shift
fi
;;
*)
break
;;
esac
shift
done
if [[ -z "$owner" ]]; then
echo "ERROR: '-o|--owner' is required." >&2
show_help
exit 1
fi
if [[ -z "$repo" ]]; then
echo "ERROR: '-r|--repo' is required." >&2
show_help
exit 1
fi
if [[ -z "$install_dir" ]]; then
local arch
arch=$(uname -m)
install_dir="$RUNNER_TOOL_CACHE/cr/$version/$arch"
fi
if [[ -n "$install_only" ]]; then
echo "Will install cr tool and not run it..."
install_chart_releaser
exit 0
fi
}
install_chart_releaser() {
if [[ ! -d "$RUNNER_TOOL_CACHE" ]]; then
echo "Cache directory '$RUNNER_TOOL_CACHE' does not exist" >&2
exit 1
fi
if [[ ! -d "$install_dir" ]]; then
mkdir -p "$install_dir"
architecture=linux_amd64
if [[ "$use_arm" = true ]]; then
architecture=linux_arm64
fi
echo "Installing chart-releaser on $install_dir..."
curl -sSLo cr.tar.gz "https://github.com/helm/chart-releaser/releases/download/$version/chart-releaser_${version#v}_${architecture}.tar.gz"
tar -xzf cr.tar.gz -C "$install_dir"
rm -f cr.tar.gz
fi
echo 'Adding cr directory to PATH...'
export PATH="$install_dir:$PATH"
}
lookup_latest_tag() {
git fetch --tags >/dev/null 2>&1
if ! git describe --tags --abbrev=0 HEAD~ 2>/dev/null; then
git rev-list --max-parents=0 --first-parent HEAD
fi
}
filter_charts() {
while read -r chart; do
[[ ! -d "$chart" ]] && continue
local file="$chart/Chart.yaml"
if [[ -f "$file" ]]; then
echo "$chart"
else
echo "WARNING: $file is missing, assuming that '$chart' is not a Helm chart. Skipping." 1>&2
fi
done
}
lookup_changed_charts() {
local commit="$1"
local changed_files
changed_files=$(git diff --find-renames --name-only "$commit" -- "$charts_dir")
local depth=$(($(tr "/" "\n" <<<"$charts_dir" | sed '/^\(\.\)*$/d' | wc -l) + 1))
local fields="1-${depth}"
cut -d '/' -f "$fields" <<<"$changed_files" | uniq | filter_charts
}
package_chart() {
local chart="$1"
local args=("$chart" --package-path .cr-release-packages)
if [[ -n "$config" ]]; then
args+=(--config "$config")
fi
echo "Packaging chart '$chart'..."
cr package "${args[@]}"
}
release_charts() {
local args=(-o "$owner" -r "$repo" -c "$(git rev-parse HEAD)")
if [[ -n "$config" ]]; then
args+=(--config "$config")
fi
if [[ "$packages_with_index" = true ]]; then
args+=(--packages-with-index --push --skip-existing)
elif [[ -n "$skip_existing" ]]; then
args+=(--skip-existing)
fi
if [[ "$mark_as_latest" = false ]]; then
args+=(--make-release-latest=false)
fi
if [[ -n "$pages_branch" ]]; then
args+=(--pages-branch "$pages_branch")
fi
echo 'Releasing charts...'
cr upload "${args[@]}"
}
# The chart-releaser binary cannot create pre-releases: it never sets the
# 'prerelease' field on the GitHub API payload, and no released version of cr
# exposes a --prerelease flag. The releases are therefore patched after upload.
#
# Release tags are derived from the packaged chart filenames, which matches cr's
# default --release-name-template of '{{ .Name }}-{{ .Version }}'. Overriding
# that template in the cr config would break this mapping.
mark_releases_prerelease() {
if [[ "$prerelease" != "true" ]]; then
return
fi
if ! command -v gh >/dev/null 2>&1; then
echo "ERROR: '--prerelease' requires the GitHub CLI (gh), which was not found on PATH." >&2
exit 1
fi
local packages=()
if [[ -d .cr-release-packages ]]; then
while IFS= read -r package; do
packages+=("$package")
done < <(find .cr-release-packages -maxdepth 1 -name '*.tgz' -print)
fi
if [[ ${#packages[@]} -eq 0 ]]; then
echo 'No chart packages found. Nothing to mark as pre-release.'
return
fi
echo 'Marking releases as pre-release...'
local package tag
for package in "${packages[@]}"; do
tag=$(basename "$package" .tgz)
echo "Marking '$tag' as pre-release..."
GH_TOKEN="$CR_TOKEN" gh release edit "$tag" \
--repo "$owner/$repo" \
--prerelease >/dev/null
done
}
update_index() {
if [[ -n "$skip_upload" ]]; then
echo "Skipping index upload..."
return
fi
local args=(-o "$owner" -r "$repo" --push)
if [[ -n "$config" ]]; then
args+=(--config "$config")
fi
if [[ "$packages_with_index" = true ]]; then
args+=(--packages-with-index --index-path .)
fi
# --index-path is where cr writes the index locally; --pages-index-path is
# where it lands in the publishing branch. Only the latter can scope the
# published index to a subdirectory, and its parent directory must already
# exist in the publishing branch: cr creates the file without creating
# intermediate directories.
if [[ -n "$pages_index_path" ]]; then
args+=(--pages-index-path "$pages_index_path")
if [[ "$packages_with_index" = true ]]; then
echo "WARNING: --packages-with-index writes chart packages to the root of the" >&2
echo "publishing branch, but the index is being published to '$pages_index_path'." >&2
echo "Package URLs in that index are resolved relative to the index itself, so" >&2
echo "they will not point at the uploaded packages." >&2
fi
fi
if [[ -n "$pages_branch" ]]; then
args+=(--pages-branch "$pages_branch")
fi
echo 'Updating charts repo index...'
cr index "${args[@]}"
}
main "$@"