Skip to content

Instantly share code, notes, and snippets.

@duncangrist
Created April 7, 2021 10:42
Show Gist options
  • Save duncangrist/211c0be6c30bf53297fc16b2e322dc7f to your computer and use it in GitHub Desktop.
Save duncangrist/211c0be6c30bf53297fc16b2e322dc7f to your computer and use it in GitHub Desktop.
Wraps "helm package" in order to be able to --set values at package time. Depends on yq and helm.
#!/usr/bin/env bash
scriptName=$(basename "$0")
USAGE="Wraps \"helm package\" in order to be able to --set values at package time.
Usage:
$scriptName \\
--set key1=value1 --set key2=value2 \\
[--app-version 1.2.3] <chart-path>"
set -e
function error() {
echo -e "$1" >&2
exit 1
}
if [[ "$#" -eq 0 ]]; then
error "$USAGE";
fi
type helm &>/dev/null || error "helm not installed"
type yq &>/dev/null || error "yq not installed"
setValues=()
originalArgs=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--set)
# specifically --set key=value format - this are for processing by this script only and shouldn't be passed
# to Helm package.
setValues+=("$2")
shift # past argument
shift # past key/value pair
;;
--*=*)
# --option=value format
originalArgs+=("$1")
shift
;;
--*)
originalArgs+=("$1")
if [[ ! "$2" =~ ^--* ]]; then
# if next argument doesn't start with a "--" assume is the value for this argument
originalArgs+=("$2")
shift
fi
shift
;;
*) # nameless option - no "--" - must be the chart directory
chartPath=$1
originalArgs+=("$1")
shift
;;
esac
done
set -- "${originalArgs[@]}" # restore positional parameters
[ -d "${chartPath}" ] || error "Directory ${chartPath} does not exist"
valuesFile="${chartPath%/}/values.yaml"
[ -f "${valuesFile}" ] || error "File ${valuesFile} does not exist"
valuesBackupFile="${chartPath%/}/values.yaml.bak"
cp "${valuesFile}" "${valuesBackupFile}"
function cleanup {
# restore values file
cp "${valuesBackupFile}" "${valuesFile}"
rm -f "${valuesBackupFile}"
}
trap cleanup EXIT
# set values using yq
for keyValue in "${setValues[@]}"; do
key="${keyValue%=*}"
value="${keyValue#*=}"
echo "Setting \"${key}\" to value \"${value}\""
yq w -i "${valuesFile}" "${key}" "${value}"
done
helm package "${originalArgs[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment