From 84693a17784af8c6ab3f713aa549491338a966cf Mon Sep 17 00:00:00 2001 From: kjeld Schouten-Lebbing Date: Tue, 7 Sep 2021 17:25:30 +0200 Subject: [PATCH] add initial work on script to construct and test the releases --- tools/build-release.sh | 308 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 308 insertions(+) create mode 100644 tools/build-release.sh diff --git a/tools/build-release.sh b/tools/build-release.sh new file mode 100644 index 00000000000..ae6513447aa --- /dev/null +++ b/tools/build-release.sh @@ -0,0 +1,308 @@ +#!/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.2.1 + +show_help() { +cat << EOF +Usage: $(basename "$0") + -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) + -u, --charts-repo-url The GitHub Pages URL to the charts repo (default: https://.github.io/) + -o, --owner The repo owner + -r, --repo The repo name + -p, --production Enables uploading releases to github releases +EOF +} + +main() { + local version="$DEFAULT_CHART_RELEASER_VERSION" + local config= + local charts_dir=charts + local owner= + local repo= + local production= + local charts_repo_url= + + 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 + + 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 + chartversion=$(cat ${chart}/Chart.yaml | grep "^version: " | awk -F" " '{ print $2 }') + chartname=$(basename ${chart}) + prep_app "${chart}" + helm dependency update "${chart}" --skip-refresh + package_chart "$chart" + if [ -d "${chart}/SCALE" ]; then + clean_apps "$chart" "$chartname" "$train" "$chartversion" + patch_apps "$chart" "$chartname" "$train" "$chartversion" + copy_apps "$chart" "$chartname" "$train" "$chartversion" + else + echo "Skipping chart ${chart}, no correct SCALE compatibility layer detected" + fi + else + echo "Chart '$chart' no longer exists in repo. Skipping it..." + fi + done + if [[ -n "${production}" ]] + release_charts + fi + update_index + else + echo "Nothing to do. No chart changes detected." + fi + + popd > /dev/null +} + +prep_app() { + local chart="$1" + echo "Creating app-readme.md for App: ${chartname}" + description=$(cat ${chart}/Chart.yaml | grep "^description: " | sed -r 's/^description: //') + echo "${description}" >> ${chart}/app-readme.md +} + +clean_apps() { + local chart="$1" + local chartname="$2" + local train="$3" + local chartversion="$4" + echo "Cleaning SCALE catalog for App: ${chartname}" + rm -Rf catalog/${train}/${chartname}/${chartversion} || true + rm -Rf catalog/${train}/${chartname}/item.yaml || true +} + +patch_apps() { + local chart="$1" + local chartname="$2" + local train="$3" + local chartversion="$4" + echo "Applying SCALE patches for App: ${chartname}" + mv ${chart}/SCALE/item.yaml ${chart}/ + mv ${chart}/SCALE/ix_values.yaml ${chart}/ + mv ${chart}/SCALE/questions.yaml ${chart}/ + cp -rf ${chart}/SCALE/templates/* ${chart}/templates || echo "ignoring templates directory..." + rm -rf ${chart}/SCALE + mv ${chart}/values.yaml ${chart}/test_values.yaml + touch ${chart}/values.yaml +} + +copy_apps() { + local chart="$1" + local chartname="$2" + local train="$3" + local chartversion="$4" + echo "Copying App to Catalog: ${2}" + mkdir -p catalog/${train}/${chartname}/${chartversion} + cp -Rf ${chart}/* catalog/${train}/${chartname}/${chartversion}/ + mv catalog/${train}/${chartname}/${chartversion}/item.yaml catalog/${train}/${chartname}/item.yaml +} + +validate_catalog() { + echo "Starting Catalog Validation" + /bin/bash -c "PWD=${pwd}; /usr/local/bin/catalog_validate validate --path $PWD/catalog" +} + + +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 + ;; + -u|--charts-repo-url) + if [[ -n "${2:-}" ]]; then + charts_repo_url="$2" + shift + else + echo "ERROR: '-u|--charts-repo-url' 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 + ;; + -p|--production) + production="true" + ;; + *) + 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 "$charts_repo_url" ]]; then + charts_repo_url="https://$owner.github.io/$repo" + fi +} + +lookup_latest_tag() { + git fetch --tags > /dev/null 2>&1 + + if ! git describe --tags --abbrev=0 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 + + echo 'Releasing charts...' + cr upload "${args[@]}" +} + +update_index() { + local args=(-o "$owner" -r "$repo" -c "$charts_repo_url") + if [[ -n "$config" ]]; then + args+=(--config "$config") + fi + + echo 'Updating charts repo index...' + cr index "${args[@]}" +} + +main "$@"