Skip to content

Instantly share code, notes, and snippets.

@lavk7
Created September 15, 2020 06:31
Show Gist options
  • Save lavk7/941b62da22fb067f09e3073ad62f3226 to your computer and use it in GitHub Desktop.
Save lavk7/941b62da22fb067f09e3073ad62f3226 to your computer and use it in GitHub Desktop.
GITLAB_TO_ECR
#!/usr/bin/env bash
set -ex
GITLAB_REGISTRY="XXXXXX"
GITLAB_URL="XXXXX"
AWS_REGION="XXXX"
PROFILE="XXXX"
AWS_ACCOUNT="XXXXXXX"
ECR_ARN=XXXXX
function create_ecr {
SERVICE=$1
aws --profile ${PROFILE} --region ${AWS_REGION} ecr create-repository --repository-name ${SERVICE} | jq -r '.repository.repositoryArn'
}
function gitlab_to_ecr {
SERVICE=$1
PROJECT_PATH="XXXX/XXXX/$SERVICE"
CLONE="$GITLAB_URL:$PROJECT_PATH.git"
REGISTRY="$GITLAB_REGISTRY/$PROJECT_PATH"
ECR_URL=${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com/${SERVICE}
# Clone git
git clone --filter=blob:none --sparse $CLONE
TAG=$(cd ${SERVICE} && git co develop > /dev/null && git rev-parse --short HEAD && cd ..)
rm -rf ${SERVICE}
# pull image
docker pull $REGISTRY:$TAG
docker tag $REGISTRY:$TAG $ECR_URL:$TAG
# push ecr image
eval $(aws --profile ${PROFILE} --region ${AWS_REGION} ecr get-login --no-include-email)
docker push ${ECR_URL}:${TAG}
}
while read -r service; do
create_ecr ${service}
gitlab_to_ecr ${service}
done < projects
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment