Skip to content

Instantly share code, notes, and snippets.

@thbkrkr
Created April 28, 2023 09:25
Show Gist options
  • Save thbkrkr/1184c959ae43b9ea0b5446f89836e10b to your computer and use it in GitHub Desktop.
Save thbkrkr/1184c959ae43b9ea0b5446f89836e10b to your computer and use it in GitHub Desktop.
Release Helm charts
#!/usr/bin/env bash
#
# Script to release Helm charts.
#
# Requires: helm, gsutil, yq.
#
# Usage: release.sh CHART_DIR
#
set -euo pipefail
HELM_REPO=${HELM_REPO:-https://helm-dev.wxyz.co/helm}
GCS_BUCKET=${GCS_BUCKET:-wxyz-helm-charts-dev/helm}
DRY_RUN=${DRY_RUN:-true}
gsutil="echo gsutil"
if [[ "$DRY_RUN" != "true" ]]; then
gsutil="gsutil"
fi
main() {
dir=${1:-./deploy}
cd $dir
# clean up potential old archives
find . -name *.tgz | xargs -n1 rm -f
for chartYaml in $(find . -name Chart.yaml | sort); do
chartDir=$(dirname $chartYaml)
chartName=$(yq .name $chartYaml)
chartVersion=$(yq .version $chartYaml)
echo "~ Release chart $chartDir"
# copy package deps like 'helm dependency update chart'
for depName in $(yq '.dependencies[] | select(has("repository")) | .name' $chartYaml); do
cp -f $(find . -name "$depName-*.tgz") $chartDir/charts/
done
helm package $chartDir --destination $chartDir
# only allow to overwrite SNAPSHOT
dstBucket=gs://$GCS_BUCKET/$(basename $chartDir)/$chartName-$chartVersion.tgz
if [[ $(set +e; gsutil -q stat $dstBucket; set -e) ]]; then
if [[ ! "$chartVersion" =~ SNAPSHOT ]]; then
echo "Cannot overwrite $dstBucket"
exit 1
fi
fi
$gsutil cp $chartDir/*.tgz gs://$GCS_BUCKET/$chartName/
done
echo "~ Update $HELM_REPO index"
$gsutil cp gs://$GCS_BUCKET/index.yaml index.yaml.old
helm repo index . --merge index.yaml.old --url $HELM_REPO
$gsutil cp index.yaml gs://$GCS_BUCKET/index.yaml
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment