diff --git a/.github/workflows/scripts/release-against-charts.sh b/.github/workflows/scripts/release-against-charts.sh index e8c525e8..b52da74e 100755 --- a/.github/workflows/scripts/release-against-charts.sh +++ b/.github/workflows/scripts/release-against-charts.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # Bumps Webhook version in a locally checked out rancher/charts repository # @@ -19,11 +19,37 @@ Usage: EOF } +bump_patch() { + version=$1 + major=$(echo "$version" | cut -d. -f1) + minor=$(echo "$version" | cut -d. -f2) + patch=$(echo "$version" | cut -d. -f3) + new_patch=$((patch + 1)) + echo "${major}.${minor}.${new_patch}" +} + +validate_version_format() { + version=$1 + if ! echo "$version" | grep -qE '^v[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?$'; then + echo "Error: Version $version must be in the format v.. or v..-rc." + exit 1 + fi +} + if [ -z "$CHARTS_DIR" ] || [ -z "$PREV_WEBHOOK_VERSION" ] || [ -z "$NEW_WEBHOOK_VERSION" ]; then usage exit 1 fi +validate_version_format "$PREV_WEBHOOK_VERSION" +validate_version_format "$NEW_WEBHOOK_VERSION" + +if echo "$PREV_WEBHOOK_VERSION" | grep -q '-rc'; then + is_prev_rc=true +else + is_prev_rc=false +fi + if [ "$PREV_WEBHOOK_VERSION" = "$NEW_WEBHOOK_VERSION" ]; then echo "Previous and new webhook version are the same: $NEW_WEBHOOK_VERSION, but must be different" exit 1 @@ -35,7 +61,7 @@ NEW_WEBHOOK_VERSION_SHORT=$(echo "$NEW_WEBHOOK_VERSION" | sed 's|^v||') # e.g. set -ue -pushd "${CHARTS_DIR}" > /dev/null +cd "${CHARTS_DIR}" # Validate the given webhook version (eg: 0.5.0-rc.13) if ! grep -q "${PREV_WEBHOOK_VERSION_SHORT}" ./packages/rancher-webhook/package.yaml; then @@ -50,7 +76,12 @@ if ! PREV_CHART_VERSION=$(yq '.version' ./packages/rancher-webhook/package.yaml) cat ./packages/rancher-webhook/package.yaml exit 1 fi -NEW_CHART_VERSION=$PREV_CHART_VERSION + +if [ "$is_prev_rc" = "false" ]; then + NEW_CHART_VERSION=$(bump_patch "$PREV_CHART_VERSION") +else + NEW_CHART_VERSION=$PREV_CHART_VERSION +fi sed -i "s/${PREV_WEBHOOK_VERSION_SHORT}/${NEW_WEBHOOK_VERSION_SHORT}/g" ./packages/rancher-webhook/package.yaml @@ -61,15 +92,18 @@ PACKAGE=rancher-webhook make charts git add ./assets/rancher-webhook ./charts/rancher-webhook git commit -m "make charts" -CHART=rancher-webhook VERSION=${PREV_CHART_VERSION}+up${PREV_WEBHOOK_VERSION_SHORT} make remove -git add ./assets/rancher-webhook ./charts/rancher-webhook ./index.yaml -git commit -m "make remove" +# When previous webhook version is an RC, then we want to remove that RC. We keep +# non-RC version. +if [ "$is_prev_rc" = "true" ]; then + CHART=rancher-webhook VERSION=${PREV_CHART_VERSION}+up${PREV_WEBHOOK_VERSION_SHORT} make remove + git add ./assets/rancher-webhook ./charts/rancher-webhook ./index.yaml + git commit -m "make remove" + + yq --inplace "del(.rancher-webhook.[] | select(. == \"${PREV_CHART_VERSION}+up${PREV_WEBHOOK_VERSION_SHORT}\"))" release.yaml +fi -yq --inplace "del(.rancher-webhook.[] | select(. == \"${PREV_CHART_VERSION}+up${PREV_WEBHOOK_VERSION_SHORT}\"))" release.yaml # Prepends to list yq --inplace ".rancher-webhook = [\"${NEW_CHART_VERSION}+up${NEW_WEBHOOK_VERSION_SHORT}\"] + .rancher-webhook" release.yaml git add release.yaml git commit -m "Add rancher-webhook ${NEW_CHART_VERSION}+up${NEW_WEBHOOK_VERSION_SHORT} to release.yaml" - -popd > /dev/null