From b020d6e0eceb710bcd11c0dfc8ae70e67150af46 Mon Sep 17 00:00:00 2001 From: OJ <4754439+junydania@users.noreply.github.com> Date: Thu, 23 May 2024 19:03:12 +0200 Subject: [PATCH] feat: added feature to upload chart to ECR (#239) * feat: added feature to upload chart to ECR * fixed wrong file name * refactored script for pushing to ECR to use the correct helm command * added oci protocol to the shell command * add prepublish step to publick chart ecr helm job * fixed multiple execution in the helm publish chart ecr job --- src/jobs/helm/helm-publish-charts-ecr.yaml | 24 ++++++++++ .../helm/helm-publish-prerelease-ecr.yaml | 20 +++++++++ .../helm/package-publish-charts-ecr.sh | 45 +++++++++++++++++++ src/scripts/helm/publish-prerelease-ecr.sh | 41 +++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 src/jobs/helm/helm-publish-charts-ecr.yaml create mode 100644 src/jobs/helm/helm-publish-prerelease-ecr.yaml create mode 100644 src/scripts/helm/package-publish-charts-ecr.sh create mode 100644 src/scripts/helm/publish-prerelease-ecr.sh diff --git a/src/jobs/helm/helm-publish-charts-ecr.yaml b/src/jobs/helm/helm-publish-charts-ecr.yaml new file mode 100644 index 00000000..8b07a973 --- /dev/null +++ b/src/jobs/helm/helm-publish-charts-ecr.yaml @@ -0,0 +1,24 @@ +executor: build-executor +parameters: + chart_directory: + description: Directory containining charts + type: string + default: . + charts: + description: List of charts to publish + type: string + default: "" + prepublish_steps: + description: Steps to run on repo before publishing charts + type: steps + default: [] +steps: + - checkout_shallow_clone + - steps: << parameters.prepublish_steps >> + - helm-add-repos + - run: + name: Package and publish charts + working_directory: << parameters.chart_directory >> + command: <> + environment: + CHARTS: << parameters.charts >> diff --git a/src/jobs/helm/helm-publish-prerelease-ecr.yaml b/src/jobs/helm/helm-publish-prerelease-ecr.yaml new file mode 100644 index 00000000..744b060a --- /dev/null +++ b/src/jobs/helm/helm-publish-prerelease-ecr.yaml @@ -0,0 +1,20 @@ +executor: build-executor +parameters: + working_directory: + description: Directory containing chart directories + type: string + default: "./" + prepublish_steps: + description: Steps to run on repo before publishing charts + type: steps + default: [] +steps: + - checkout_shallow_clone + - set-beta-version: + working_directory: << parameters.working_directory >> + - steps: << parameters.prepublish_steps >> + - helm-add-repos + - run: + name: Package and publish charts + working_directory: << parameters.working_directory >> + command: <> diff --git a/src/scripts/helm/package-publish-charts-ecr.sh b/src/scripts/helm/package-publish-charts-ecr.sh new file mode 100644 index 00000000..88694b3b --- /dev/null +++ b/src/scripts/helm/package-publish-charts-ecr.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Expected environment variables: +echo "CHARTS: ${CHARTS?}" +echo "AWS_REGION: ${AWS_REGION?}" + +# Set a default value for ECR_REPOSITORY_URI if not set +ECR_REPOSITORY_URI=${ECR_REPOSITORY_URI:-"168387678261.dkr.ecr.us-east-1.amazonaws.com"} + +echo "ECR_REPOSITORY_URI: ${ECR_REPOSITORY_URI}" + +# Enable experimental OCI support in Helm +export HELM_EXPERIMENTAL_OCI=1 + +# Login to ECR +aws ecr get-login-password --region "$AWS_REGION" | helm registry login --username AWS --password-stdin "$ECR_REPOSITORY_URI" + +for chart in ${CHARTS?}; do + echo "Packaging $chart" + + # Create a temporary directory to store the packaged chart + dist="$(mktemp -d)" + + helm dep update "$chart/$chart" + helm package "$chart/$chart" --destination "$dist" + + # Get the chart version from the packaged chart + packaged_chart="$(ls "$dist")" + chart_version=$(helm show chart "$dist/$packaged_chart" | yq --raw-output '.version') + channel=$(helm show chart "$chart/$chart" | yq --raw-output '.annotations."release-repository"') + echo "Publishing in $channel channel" + + repo="voiceflow-charts-private" + if [[ "$channel" == "public" ]]; then + repo="voiceflow-charts-public" + fi + + # Construct the full ECR URL with the OCI protocol + FULL_ECR_URL="oci://$ECR_REPOSITORY_URI/$repo/$chart:$chart_version" + + # Push the chart to ECR using the OCI protocol + helm push "$dist/$packaged_chart" "$FULL_ECR_URL" + + rm -rf "$dist" +done diff --git a/src/scripts/helm/publish-prerelease-ecr.sh b/src/scripts/helm/publish-prerelease-ecr.sh new file mode 100644 index 00000000..e1399943 --- /dev/null +++ b/src/scripts/helm/publish-prerelease-ecr.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +# Expected environment variables: +echo "BETA_VERSION: ${BETA_VERSION:?}" +echo "AWS_REGION: ${AWS_REGION:?}" + +# Set a default value for ECR_REPOSITORY_URI if not set +ECR_REPOSITORY_URI=${ECR_REPOSITORY_URI:-"168387678261.dkr.ecr.us-east-1.amazonaws.com"} + +echo "ECR_REPOSITORY_URI: ${ECR_REPOSITORY_URI}" + +# Enable experimental OCI support in Helm +export HELM_EXPERIMENTAL_OCI=1 + +# Login to ECR +aws ecr get-login-password --region "$AWS_REGION" | helm registry login --username AWS --password-stdin "$ECR_REPOSITORY_URI" + +# Publishes beta releases for helm charts in subdirectories of working directory +for file in * ; do + if [[ -d "$file" && -f "$file/$file/Chart.yaml" ]]; then + echo "Packaging $file" + cd "$file" || exit + helm dep update "$file" + + echo "Packaging version $BETA_VERSION" + helm package "$file" --version "$BETA_VERSION" + + echo "Publishing beta release" + CHART="$file-$BETA_VERSION.tgz" + + if [ ! -f "$CHART" ]; then + echo "Packaged chart does not have expected name $CHART. Does the name in Chart.yaml match the directory name?" + exit 3 + fi + + # Push the chart to ECR + helm push "$CHART" "oci://$ECR_REPOSITORY_URI/voiceflow-charts-beta" + + cd .. + fi +done