Skip to content

Commit

Permalink
feat: added feature to upload chart to ECR (#239)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
junydania authored May 23, 2024
1 parent 7bdd573 commit b020d6e
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/jobs/helm/helm-publish-charts-ecr.yaml
Original file line number Diff line number Diff line change
@@ -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: <<include(scripts/helm/package-publish-charts-ecr.sh)>>
environment:
CHARTS: << parameters.charts >>
20 changes: 20 additions & 0 deletions src/jobs/helm/helm-publish-prerelease-ecr.yaml
Original file line number Diff line number Diff line change
@@ -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: <<include(scripts/helm/publish-prerelease-ecr.sh)>>
45 changes: 45 additions & 0 deletions src/scripts/helm/package-publish-charts-ecr.sh
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions src/scripts/helm/publish-prerelease-ecr.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b020d6e

Please sign in to comment.