Skip to content

Commit

Permalink
CLOUDP-218697: Detect helm chart releases happened
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Vazquez <[email protected]>
  • Loading branch information
josvazg committed Dec 20, 2023
1 parent f802c79 commit 33afd65
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 167 deletions.
20 changes: 19 additions & 1 deletion .github/actions/releaser/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ inputs:
dryrun:
description: "not actually release, but check what would be done"
required: false
action:
type: choice
description: Release or check
options:
- release
- check
default: release
required: false

runs:
using: composite
steps:
- shell: bash
- name: release
shell: bash
if: inputs.action == "release"
run: |
export VERSION="${{ inputs.version }}"
export CHART_DIR="${{ inputs.charts_dir }}"
Expand All @@ -35,3 +45,11 @@ runs:
export REPO=$repo
"$GITHUB_ACTION_PATH/cr.sh" "${{ inputs.target }}"
- name: check
shell: bash
if: inputs.action == "check"
run: |
export CHART_DIR="${{ inputs.charts_dir }}"
export CHARTS_REPO_URL="${{ inputs.charts_repo_url }}"
"$GITHUB_ACTION_PATH/check.sh" "${{ inputs.target }}"
58 changes: 58 additions & 0 deletions .github/actions/releaser/check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env bash

# Check or or all helm charts versions in current branch code have been released

# NOTE: assume GNU semantics for tools like awk, grep, etc

set -o errexit
set -o nounset
set -o pipefail

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# shellcheck source=cr-funcs.sh
source "${SCRIPT_DIR}/cr-funcs.sh"

main() {
local target=("$@")
local charts_dir=${CHART_DIR:-charts}
local charts_repo_url=${CHARTS_REPO_URL:-}
local unreleased_charts=()

if [[ -z "${target[*]}" ]]; then
mapfile -t target< <(find "$charts_dir" -maxdepth 2 -type f -name Chart.yaml | awk -F / '{print $2}')
fi

echo "Checking release status for helm charts:" "${target[@]}"

prepare_helm_repo

for folder in "${target[@]}"; do
[[ ! -f "$charts_dir/$folder/Chart.yaml" ]] && continue
print_line_separator
local chart_name
local chart_version
local chart_was_released

chart_name=$(read_chart_name "${charts_dir}/${folder}")
chart_version=$(read_chart_version "${charts_dir}/${folder}")
echo "Checking if \"$charts_dir/$folder\" has been released to the repo"
chart_was_released=$(chart_released "${chart_name}" "${chart_version}")

echo "released result: \"${chart_was_released}\""

if [ -z "${chart_was_released}" ]; then
unreleased_charts+=("$chart_name")
fi
done

if [[ -n "${unreleased_charts[*]}" ]]; then
echo "FAIL: found unreleased charts:" "${unreleased_charts[@]}"
exit 1
else
echo "PASS: all latest helm charts released for" "${target[@]}"
fi
}

mapfile -t target< <(echo "$1" )
main "${target[@]}"
171 changes: 171 additions & 0 deletions .github/actions/releaser/cr-funcs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/usr/bin/env bash

# common chart release related shell functions

# NOTE: assume GNU semantics for tools like awk, grep, etc

print_line_separator() {
echo "============================================="
}

release_charts_inside_folders() {
local folders=("$@")
local changed_charts=()

prepare_helm_repo

# form list of folder which was changed
for folder in "${folders[@]}"; do
[[ ! -f "$charts_dir/$folder/Chart.yaml" ]] && continue
print_line_separator
local chart_name
local chart_version
local chart_was_released

chart_name=$(read_chart_name "${charts_dir}/${folder}")
chart_version=$(read_chart_version "${charts_dir}/${folder}")
echo "Checking if \"$charts_dir/$folder\" has been released to the repo"
chart_was_released=$(chart_released "${chart_name}" "${chart_version}")

echo "released result: \"${chart_was_released}\""

# if chart is not released or folder has change, then remember as changed_charts
if [ -z "${chart_was_released}" ] || has_changed "$folder"; then
changed_charts+=("$folder")
fi
done
echo "changed charts: " "${changed_charts[@]}"

# continue only with changed charts
if [[ -n "${changed_charts[*]}" ]]; then
if [ "${DRYRUN}" == "true" ]; then
echo "DRYRUN: Would have released charts" "${changed_charts[@]}"
else
release_changed_charts "${changed_charts[@]}"
fi
else
echo "Nothing to do. No chart changes detected."
fi
}

read_chart_name() {
local chart_path=$1
awk '/^name: /{print $2}' "$chart_path/Chart.yaml"
}

read_chart_version() {
local chart_path=$1
awk '/^version: /{print $2}' "$chart_path/Chart.yaml"
}

prepare_helm_repo() {
helm repo add mongodb https://mongodb.github.io/helm-charts
helm repo update mongodb
}

chart_released() {
local chart_name=$1
local version=$2

helm search repo "mongodb/${chart_name}" --version "${version}" |grep "${chart_name}\s"
}

# check if release version and chart version is diffrent
has_changed() {
local folder=$1
local chart_name
chart_name=$(awk '/^name/{print $2}' "$charts_dir/$folder/Chart.yaml")
tag=$(get_latest_tag "$chart_name")
changed_files=$(git diff --find-renames --name-only "$tag" -- "$charts_dir/$folder")

echo "Looking for versions..."
tag_version=$(echo "$tag" | awk -F '-' '{print $NF}') # sample-0.1.1 | 0.1.1
chart_version=$(awk '/^version: /{print $2}' "$charts_dir/$folder/Chart.yaml")
echo "version from tag: $tag_version"
echo "version from chart: $chart_version"

if [[ "$tag_version" != "$chart_version" ]] && [[ -n "$changed_files" ]]; then
return 0
fi
return 1
}

get_latest_tag(){
local name=$1

git fetch --tags > /dev/null 2>&1
git describe --tags --abbrev=0 --match="$name-[0-9.]*" "$(git rev-list --tags --max-count=1)"
}

release_changed_charts() {
local changed_charts=("$@")

helm repo update
install_chart_releaser
cleanup_releaser
package_charts "${changed_charts[@]}"
release_charts
update_index
}

install_chart_releaser() {
print_line_separator
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...'
fi
export PATH="$cache_dir:$PATH"
}

cleanup_releaser() {
rm -rf .cr-release-packages
mkdir -p .cr-release-packages

rm -rf .cr-index
mkdir -p .cr-index
}

package_charts() {
print_line_separator
local changed_charts=("$@")
for chart in "${changed_charts[@]}"; do
folder="$charts_dir/$chart"
if [[ -d "$folder" ]]; then
local args=("$folder" --package-path .cr-release-packages)

echo "Packaging chart folder '$folder'..."
cr package "${args[@]}"
else
echo "Chart '$folder' no longer exists in repo. Skipping it..."
fi
done
}

release_charts() {
local args=(-o "$owner" -r "$repo" -c "$(git rev-parse HEAD)")

echo 'Releasing charts...'
cr upload "${args[@]}"
}

update_index() {
local args=(-o "$owner" -r "$repo" -c "$charts_repo_url" --push)

echo 'Updating charts repo index...'
cr index "${args[@]}"
}
Loading

0 comments on commit 33afd65

Please sign in to comment.