Skip to content

Instantly share code, notes, and snippets.

@dotCipher
Created February 7, 2024 19:34
Show Gist options
  • Save dotCipher/92660b81fe5f5527b175e94dd395598d to your computer and use it in GitHub Desktop.
Save dotCipher/92660b81fe5f5527b175e94dd395598d to your computer and use it in GitHub Desktop.
Retags a `.dev-` semantic-release helm chart into a semver compliant format using `.rc` instead
#!/usr/bin/env bash
# Retags a helm chart in ECR, finding the chart by name and version,
# then updating the version in the Chart.yaml and pushing the new chart to ECR.
# The version it will re-tag as will be a reformatted semver compliant version.
# ** OSX Note: **
# Make sure to install gnused
# brew install gnused
# Otherwise this won't work with the sed command natively
# sed: 1: "foo-bar ...": invalid command code j
# https://stackoverflow.com/questions/19456518/error-when-using-sed-with-find-command-on-os-x-invalid-command-code
if [ $# -lt 3 ]; then
echo "Usage: $0 <aws_account_id> <chart_name> <version_to_reformat>"
exit 1
fi
ACCOUNT_ID=$1
CHART_NAME=$2
OLD_VERSION=$3
NEW_VERSION="${OLD_VERSION//dev\./rc}"
echo "reformatting version from ${OLD_VERSION} to ${NEW_VERSION}"
ECR="${ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com"
TMP_DIR="${CHART_NAME}-${OLD_VERSION}"
ECR_OCI="oci://${ECR}/${CHART_NAME}"
helm pull "${ECR_OCI}" --version "${OLD_VERSION}"
mkdir -p "${TMP_DIR}"
tar -xvf "${CHART_NAME}-${OLD_VERSION}.tgz" -C "${TMP_DIR}"
# Replace old version with new version
if ! command -v gsed &> /dev/null
then
echo "gsed could not be found, using sed for chart variable replacement"
sed -i "s/\(^version: \).*/\1$NEW_VERSION/" "${TMP_DIR}/${CHART_NAME}/Chart.yaml"
sed -i "s/\(^appVersion: \).*/\1$NEW_VERSION/" "${TMP_DIR}/${CHART_NAME}/Chart.yaml"
else
echo "gsed found, using gsed for chart variable replacement"
gsed -i "s/\(^version: \).*/\1$NEW_VERSION/" "${TMP_DIR}/${CHART_NAME}/Chart.yaml"
gsed -i "s/\(^appVersion: \).*/\1$NEW_VERSION/" "${TMP_DIR}/${CHART_NAME}/Chart.yaml"
fi
# Re-package chart
helm package "${TMP_DIR}/${CHART_NAME}"
# Push to ECR
helm push "${CHART_NAME}-${NEW_VERSION}.tgz" "oci://${ECR}"
# Clean up
rm -rf "${TMP_DIR}"
rm "${CHART_NAME}-${OLD_VERSION}.tgz"
rm "${CHART_NAME}-${NEW_VERSION}.tgz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment