From 4c7099ec6ae2c50599a3668ecbdad03acedb2f83 Mon Sep 17 00:00:00 2001 From: leecalcote Date: Thu, 13 May 2021 16:54:41 -0500 Subject: [PATCH] init Signed-off-by: leecalcote --- action.yml | 67 ++++++++++++ cr.sh | 295 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 362 insertions(+) create mode 100644 action.yml create mode 100755 cr.sh diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..40ac020 --- /dev/null +++ b/action.yml @@ -0,0 +1,67 @@ +name: "Helm Chart Releaser For Remote" +description: "Host a Helm charts repo on GitHub repo" +author: "aisuko" +branding: + color: blue + icon: anchor +inputs: + version: + description: "The chart-releaser version to use" + config: + description: "The relative path to the chart-releaser config file" + charts_dir: + description: The charts directory + default: charts + charts_repo_url: + description: "The GitHub Pages URL to the charts repo (default: https://.github.io/)" + pages_branch: + description: The branch of chart's repo + activity: + description: activity (pr, push and none) + default: none + owner: + description: The repo owner + repo: + description: The chart repo +runs: + using: composite + steps: + - run: | + if [[ -n "${{ inputs.owner }}" ]]; then + args=(--owner "${{ inputs.owner }}") + else + owner=$(cut -d '/' -f 1 <<< "$GITHUB_REPOSITORY") + args=(--owner "$owner") + fi + + if [[ -n "${{ inputs.repo }}" ]]; then + args+=(--repo "${{ inputs.repo }}") + else + repo=$(cut -d '/' -f 2 <<< "$GITHUB_REPOSITORY") + args+=(--repo "$repo") + fi + + args+=(--charts-dir "${{ inputs.charts_dir }}") + + if [[ -n "${{ inputs.version }}" ]]; then + args+=(--version "${{ inputs.version }}") + fi + + if [[ -n "${{ inputs.config }}" ]]; then + args+=(--config "${{ inputs.config }}") + fi + + if [[ -n "${{ inputs.charts_repo_url }}" ]]; then + args+=(--charts-repo-url "${{ inputs.charts_repo_url }}") + fi + + if [[ -n "${{ inputs.pages_branch }}" ]]; then + args+=(--pages-branch "${{inputs.pages_branch}}") + fi + + if [[ -n "${{ inputs.activity }}" ]]; then + args+=(--activity "${{inputs.activity}}") + fi + + "$GITHUB_ACTION_PATH/cr.sh" "${args[@]}" + shell: bash \ No newline at end of file diff --git a/cr.sh b/cr.sh new file mode 100755 index 0000000..cdad822 --- /dev/null +++ b/cr.sh @@ -0,0 +1,295 @@ +set -o errexit +set -o nounset +set -o pipefail + +DEFAULT_CHART_RELEASER_VERSION=v1.2.0 + +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 + --page-branch The branch of index.yaml push or pr + --activity The activity of push or pr index.yaml +EOF +} + +main() { + local version="$DEFAULT_CHART_RELEASER_VERSION" + local config= + local charts_dir=charts + local owner= + local repo= + local charts_repo_url= + local pages_branch= + local activity= + + 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 'Looking up latest commit' + local latest_commit + latest_commit=$(lookup_latest_commit_id) + + # echo "Discovering changed charts since '$latest_tag'..." + # local changed_charts=() + # readarray -t changed_charts <<< "$(lookup_changed_charts "$latest_tag")" + + echo "Discovering changed charts since '$latest_commit'..." + local changed_charts=() + readarray -t changed_charts <<< "$(lookup_changed_charts "$latest_commit")" + + 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 + package_chart "$chart" + else + echo "Chart '$chart' no longer exists in repo. Skipping it..." + fi + done + + release_charts + update_index + else + echo "Nothing to do. No chart changes detected." + fi + + popd > /dev/null +} + +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 + ;; + --pages-branch) + if [[ -n "${2:-}" ]]; then + pages_branch="$2" + shift + else + echo "ERROR: '--pages_branch' cannot be empty." >&2 + show_help + exit 1 + fi + ;; + --activity) + if [[ -n "${2:-}" ]]; then + activity="$2" + shift + else + echo "ERROR: '--activity' cannot be empty." >&2 + show_help + exit 1 + fi + ;; + *) + 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 +} + +install_chart_releaser() { + if [[ ! -d "$RUNNER_TOOL_CACHE" ]]; then + echo "Cache directory '$RUNNER_TOOL_CACHE' does not exist" >&2 + exit 1 + fi + + local arch + arch=$(uname -m) + + local cache_dir="$RUNNER_TOOL_CACHE/ct/$version/$arch" + if [[ ! -d "$cache_dir" ]]; then + mkdir -p "$cache_dir" + + echo "Installing chart-releaser..." + curl -sSLo cr.tar.gz "https://github.com/helm/chart-releaser/releases/download/$version/chart-releaser_${version#v}_linux_amd64.tar.gz" + tar -xzf cr.tar.gz -C "$cache_dir" + rm -f cr.tar.gz + + echo 'Adding cr directory to PATH...' + export PATH="$cache_dir:$PATH" + 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 +} + +# Return git commmit id +lookup_latest_commit_id(){ + git rev-parse HEAD +} + +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" | 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") + if [[ -n "$config" ]]; then + args+=(--config "$config") + fi + + echo 'Releasing charts...' + echo "The parameters below: ${args[@]}" + cr upload "${args[@]}" +} + +update_index() { + local args=(-o "$owner" -r "$repo" -c "$charts_repo_url" --pages-branch "$pages_branch") + if [[ -n "$config" ]]; then + args+=(--config "$config") + fi + echo "The activity: $activity" + # https://stackoverflow.com/questions/64270867/auth-error-trying-to-copy-a-repo-with-github-actions + if [[ "$activity" = "pr" ]]; then + args+=("--pr") + elif [[ "$activity" = "push" ]]; then + args+=("--push") + fi + + echo 'Updating charts repo index...' + echo "The parameters below: ${args[@]}" + cr index "${args[@]}" +} + +main "$@" \ No newline at end of file