Skip to content

Instantly share code, notes, and snippets.

@zxkane
Created September 27, 2021 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zxkane/f548965c8d84871cd8f5eb4ceaaffbc9 to your computer and use it in GitHub Desktop.
Save zxkane/f548965c8d84871cd8f5eb4ceaaffbc9 to your computer and use it in GitHub Desktop.
push helm chart to all ecr regions
#!/bin/bash -xe
create_repo() {
local name=$1
local region=$2
# create ecr repo
aws ecr create-repository --region $region --repository-name "$name" --image-tag-mutability IMMUTABLE --image-scanning-configuration scanOnPush=true --encryption-configuration encryptionType=AES256 2>/dev/null
# set repo permission
read -r -d '' POLICY_TEXT << EOM
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "public statement",
"Effect": "Allow",
"Principal": "*",
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
}
]
}
EOM
aws ecr set-repository-policy --region $region --repository-name "$name" --policy-text "$POLICY_TEXT" 2>/dev/null
}
push_to_ecr() {
local name=$1
local tag=$2
local region=$3
REGISTRYID=`aws ecr describe-repositories --region $region --repository-names $name --query 'repositories[].registryId' 2>/dev/null |jq -r '.[0]'`
REPO=`aws ecr describe-repositories --region $region --repository-names $name --query 'repositories[0].repositoryUri' --output text 2>/dev/null | sed -E 's/(.*\.amazonaws\.com(.cn)?).*/\1/'`
aws ecr get-login-password --region "$region" | helm registry login --username AWS --password-stdin $REPO
helm push "$name-$tag.tgz" "oci://$REPO/"
}
push_chart() {
local name=$1
local tag=$2
local region=$3
EXISTINGREPO=`aws ecr describe-repositories --region $region --repository-names $name --query 'repositories[].repositoryName' 2>/dev/null|jq '.[]'|jq '.'`
if [[ -z $EXISTINGREPO ]]
then
create_repo "$name" "$region"
echo "The repo with name '$name' is created in region '$region'."
else
echo "The repo with name '$name' already exists in region '$region'."
fi
# push to ecr via docker
push_to_ecr "$name" "$tag" $region
}
CHARTNAME=$1
CHARTTAG=$2
if [[ -z $CHARTNAME ]] || [[ -z $CHARTTAG ]]
then
echo "pls specify CHARTNAME and CHARTTAG."
exit -1
fi
export -f push_chart create_repo push_to_ecr
export HELM_EXPERIMENTAL_OCI=1
aws ec2 describe-regions --query 'Regions[].RegionName' --output json | jq '.[]'|jq '.'|xargs -I {} -n 1 bash -c 'push_chart "$@"' _ "$CHARTNAME" "$CHARTTAG" {}
@zxkane
Copy link
Author

zxkane commented Sep 27, 2021

Prerequisite:

  • Helm 3.7.0+

Example Usage:

helm repo add eks-charts https://aws.github.io/eks-charts
helm pull eks-charts/aws-load-balancer-controller --version 1.2.7
./push-helm-chart-to-all-ecr-regions.sh aws-load-balancer-controller 1.2.7

@ddiawara
Copy link

HEllo i try to push argocd chart to my private repository. it's possible to do it with dependency ?

@zxkane
Copy link
Author

zxkane commented Apr 21, 2022

HEllo i try to push argocd chart to my private repository. it's possible to do it with dependency ?

It's a case not covered. You have to push all dependencies manually, then update the repo of dependency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment