#!/usr/bin/env bash # # Package the Helm chart and publish it to the classic Helm chart repository # in TapTap/Charts. # # The repository keeps index.yaml and the packaged .tgz files on its main # branch, so it works with `helm repo add` and Kustomize's helmCharts # generator. The version is: # - VERSION if set, # - the pushed tag for a v* tag build, # - -sha. for a main branch build, # - the chart version for a local build outside CI. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" CHARTS_REPO_URL="${CHARTS_REPO_URL:-https://gitea.tap-tap.win/TapTap/Charts.git}" CHARTS_BRANCH="${CHARTS_BRANCH:-main}" CHARTS_USERNAME="${CHARTS_USERNAME:-TapTap}" CHARTS_TOKEN="${CHARTS_TOKEN:-}" CHARTS_RAW_URL="${CHARTS_RAW_URL:-https://gitea.tap-tap.win/TapTap/Charts/raw/branch/main}" CHART_DIR="${CHART_DIR:-$ROOT/charts/audiomanager}" VERSION="${VERSION:-}" usage() { cat <<'EOF' Usage: scripts/push-chart.sh [options] Package the chart and publish it to the classic Helm repository in TapTap/Charts. Options: -h, --help Show this help Environment: CHARTS_REPO_URL Git URL of the chart repository (default: https://gitea.tap-tap.win/TapTap/Charts.git) CHARTS_BRANCH Published branch (default: main) CHARTS_USERNAME Git username for the chart repository (default: TapTap) CHARTS_TOKEN Token with write access to the chart repository. When empty, normal git credentials are used. CHARTS_RAW_URL Public URL written into index.yaml (default: https://gitea.tap-tap.win/TapTap/Charts/raw/branch/main) CHART_DIR Chart directory (default: charts/audiomanager) VERSION Chart version to package (overrides auto-detection) EOF } if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then usage exit 0 fi chart_version="$(awk '/^version:/ { print $2; exit }' "$CHART_DIR/Chart.yaml")" if [[ -z "$chart_version" ]]; then echo "Could not read the chart version from $CHART_DIR/Chart.yaml" >&2 exit 1 fi if [[ -z "$VERSION" ]]; then if [[ "${GITHUB_REF:-}" == refs/tags/* ]]; then VERSION="${GITHUB_REF_NAME#v}" elif [[ -n "${GITHUB_SHA:-}" ]]; then VERSION="${chart_version}-sha.${GITHUB_SHA::7}" elif git -C "$ROOT" rev-parse --git-dir >/dev/null 2>&1; then VERSION="${chart_version}-sha.$(git -C "$ROOT" rev-parse --short=7 HEAD)" else VERSION="$chart_version" fi fi workdir="$(mktemp -d "${TMPDIR:-/tmp}/charts-publish.XXXXXX")" trap 'rm -rf "$workdir"' EXIT if [[ -n "$CHARTS_TOKEN" ]]; then askpass="$workdir/askpass.sh" cat > "$askpass" <<'SH' #!/bin/sh case "$1" in *Username*) printf '%s\n' "${CHARTS_USERNAME:?}" ;; *Password*) printf '%s\n' "${CHARTS_TOKEN:?}" ;; esac SH chmod 700 "$askpass" export CHARTS_USERNAME CHARTS_TOKEN export GIT_ASKPASS="$askpass" export GIT_TERMINAL_PROMPT=0 fi git clone --quiet --depth=1 --branch "$CHARTS_BRANCH" \ "$CHARTS_REPO_URL" "$workdir/repo" chart_name="$(awk '/^name:/ { print $2; exit }' "$CHART_DIR/Chart.yaml")" if [[ -f "$workdir/repo/${chart_name}-${VERSION}.tgz" ]]; then echo "Chart $chart_name $VERSION is already published." exit 0 fi helm package "$CHART_DIR" --version "$VERSION" --destination "$workdir/repo" helm repo index "$workdir/repo" --url "$CHARTS_RAW_URL" # Keep the repository README pointing at the newest version of every chart so # the published version is visible without reading index.yaml. app_version="$(awk '/^appVersion:/ { print $2; exit }' "$CHART_DIR/Chart.yaml" | tr -d '"')" source_sha="${GITHUB_SHA:-$(git -C "$ROOT" rev-parse HEAD)}" source_repo="${GITHUB_REPOSITORY:-TapTap/AudioManager}" source_server="${GITHUB_SERVER_URL:-https://gitea.tap-tap.win}" published_at="$(date -u '+%Y-%m-%d %H:%M UTC')" newest_charts="$( for archive in "$workdir/repo"/*.tgz; do [[ -f "$archive" ]] || continue helm show chart "$archive" 2>/dev/null \ | awk ' /^name:/ { name = $2 } /^version:/ { version = $2 } /^appVersion:/ { app = $2; gsub(/"/, "", app) } END { if (name != "") printf "%s|%s|%s\n", name, version, app } ' done | sort -t '|' -k1,1 -k2,2V \ | awk -F '|' ' { if ($1 == last) { row = $0 } else { if (last != "") print row; last = $1; row = $0 } } END { if (last != "") print row } ' )" section="$workdir/newest-chart.md" { echo '' echo '## Newest chart' echo echo "\`$chart_name\` **$VERSION** (app \`$app_version\`), published $published_at" echo "from commit [\`${source_sha:0:7}\`]($source_server/$source_repo/commit/$source_sha)." echo echo '| Chart | Newest version | App version |' echo '| --- | --- | --- |' while IFS='|' read -r name version app; do [[ -n "$name" ]] || continue echo "| \`$name\` | \`$version\` | ${app:--} |" done <<< "$newest_charts" echo '' } > "$section" readme="$workdir/repo/README.md" updated_readme="$workdir/repo/README.md.new" if [[ -f "$readme" ]] && grep -q '' "$readme"; then awk -v section="$section" ' // { while ((getline line < section) > 0) print line skipping = 1 next } // { skipping = 0; next } !skipping { print } ' "$readme" > "$updated_readme" else # No generated section yet: put it before the first "## " heading. { if [[ -f "$readme" ]]; then awk -v section="$section" ' !inserted && /^## / { while ((getline line < section) > 0) print line print "" inserted = 1 } { print } END { if (!inserted) { print "" while ((getline line < section) > 0) print line } } ' "$readme" else { echo '# Charts' echo echo 'Published Helm chart repository for TapTap projects.' echo } cat "$section" fi } > "$updated_readme" fi mv "$updated_readme" "$readme" git -C "$workdir/repo" add . if git -C "$workdir/repo" diff --cached --quiet; then echo "Chart $(basename "$CHART_DIR") $VERSION is already published." exit 0 fi git -C "$workdir/repo" \ -c user.name="${GIT_AUTHOR_NAME:-Chart Publisher}" \ -c user.email="${GIT_AUTHOR_EMAIL:-charts@tap-tap.win}" \ commit -q -m "chart: publish $(basename "$CHART_DIR") $VERSION" git -C "$workdir/repo" push -q origin "$CHARTS_BRANCH" echo "Published $(basename "$CHART_DIR") $VERSION to $CHARTS_RAW_URL"