Skip to content

Instantly share code, notes, and snippets.

@ahawkins
Last active May 4, 2017 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahawkins/de374928d18d5c13245e9086126ee498 to your computer and use it in GitHub Desktop.
Save ahawkins/de374928d18d5c13245e9086126ee498 to your computer and use it in GitHub Desktop.
script/build -- Templated helm chart generation
#!/usr/bin/env bash
set -euo pipefail
main() {
local market stage branch build_number src_directory destination_directory
local name version chart_version
version="$(cat VERSION)"
while getopts :s:d:m:S:b:g:n: opt; do
case "${opt}" in
s)
src_directory="${OPTARG}"
;;
d)
destination_directory="${OPTARG}"
;;
m)
market="${OPTARG}"
;;
S)
stage="${OPTARG}"
;;
b)
branch="${OPTARG}"
;;
g)
gitsha="${OPTARG}"
;;
n)
build_number="${OPTARG}"
;;
\?)
echo "Invalid option -${OPTARG}" 1>&2
return 1
;;
:)
echo "Option -${OPTARG} requires an argument" 1>&2
return 1
;;
esac
done
if [ -z "${src_directory:-}" ] || [ -z "${destination_directory:-}" ]; then
echo "-s and -d are required" 1>&2
return 1
fi
if [ ! -d "${src_directory}" ]; then
echo "${src_directory} does not exist!" 1>&2
return 1
fi
if [ -z "${market:-}" ]; then
echo "-m required" 1>&2
return 1
fi
if [ -z "${stage:-}" ]; then
echo "-e stage" 1>&2
return 1
fi
if [ "${stage:-}" = 'sandbox' ] && [ -z "${branch:-}" ]; then
echo "-e sandbox requires -b" 1>&2
return 1
fi
if [ -n "${branch:-}" ] && [ -z "${build_number:-}" ]; then
echo "-b requires -n" 1>&2
return 1
fi
if ! [[ "${build_number:-}" =~ ^[0-9]+$ ]]; then
echo "-n must be numeric, did you provide a build number?" 1>&2
return 1
fi
if [ -z "${gitsha:-}" ]; then
echo "-g is required" 1>&2
return 1
fi
case "${stage}" in
production)
chart_version="${version}"
;;
sandbox)
chart_version="${version}-beta.${build_number}+${branch//[^0-9A-Za-z-]/-}.g${gitsha}"
;;
staging)
chart_version="${version}-rc.${build_number}+${branch//[^0-9A-Za-z-]/-}.g${gitsha}"
;;
*)
echo "Do not know how to consturct version for ${stage}" 1>&2
return 1
;;
esac
local chart_name="kviberg-${market}"
local chart_id="${chart_name}-${chart_version//+/-}"
destination_directory="${destination_directory}/${stage}/${chart_name}"
if [ ${#chart_id} -gt 63 ]; then
echo "chart id '${chart_id}' too long" 1>&2
return 1
fi
if [ -d "${destination_directory}" ]; then
echo "${destination_directory} already exists" 1>&2
return 1
fi
mkdir -p "${destination_directory}"
cp -r "${src_directory}/"* "${destination_directory}"
echo "--- Building ${chart_id}"
for file in values.yaml Chart.yaml; do
env \
"CHART_ID=${chart_id}" \
"CHART_NAME=${chart_name}" \
"CHART_MARKET=${market}" \
"CHART_VERSION=${chart_version}" \
"CHART_STAGE=${stage}" \
"CHART_COMMIT=${gitsha}" \
envsubst < "config/${file}" > "${destination_directory}/${file}"
done
echo "Built chart ${market} ${chart_version} in ${destination_directory}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment