Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildspec/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ phases:
build:
commands:
- |
echo "Hello, CodeBuild!, push to ${targetRegion}"
echo "Hello, CodeBuild!, push to ${AWS_DEFAULT_REGION}"
bash ./mirror-images.sh
post_build:
commands:
Expand Down
66 changes: 34 additions & 32 deletions buildspec/mirror-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
# set -x

AccountID=$(aws sts get-caller-identity | jq -r ".Account")
ECR_DN="${AccountID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com.cn"

url_suffix="amazonaws.com"
if [ "$AWS_DEFAULT_REGION" == "cn-north-1" ] || [ "$AWS_DEFAULT_REGION" == "cn-northwest-1" ]; then
url_suffix="amazonaws.cn"
fi
ECR_DN="${AccountID}.dkr.ecr.${AWS_DEFAULT_REGION}.${url_suffix}"

echo "Account ID:${AccountID} Region:${AWS_DEFAULT_REGION} ECR DN: ${ECR_DN}"

declare -A DOMAIN_MAP
DOMAIN_MAP["quayio"]="quay"
Expand All @@ -13,75 +20,72 @@ DOMAIN_MAP["us.gcr.io"]="gcr"
DOMAIN_MAP["k8s.gcr.io"]="gcr/google_containers"
DOMAIN_MAP["docker.io"]="dockerhub"

function replaceDomainName(){
function replaceDomainName() {
math_mirror=False
URI="$1"
for key in ${!DOMAIN_MAP[*]};do
for key in ${!DOMAIN_MAP[*]}; do
if [[ $URI == ${key}* ]]; then
math_mirror=True
URI=${URI/#${key}/${DOMAIN_MAP[$key]}}
break
math_mirror=True
URI=${URI/#${key}/${DOMAIN_MAP[$key]}}
break
fi
done
if [[ $math_mirror == False ]] ; then
if [[ $math_mirror == False ]]; then
URI="dockerhub/${URI}"
fi
}

function createEcrRepo() {
if inArray "$1" "$allEcrRepos"
then
if inArray "$1" "$allEcrRepos"; then
echo "repo: $1 already exists"
else
echo "creating repo: $1"
aws ecr create-repository --repository-name "$1"
aws ecr create-repository --repository-name "$1"
fi
}

function deleteEcrRepo() {
if inArray "$1" "$allEcrRepos"
then
if inArray "$1" "$allEcrRepos"; then
echo "deleting repo: $1"
aws ecr delete-repository --repository-name "$1" --force
fi
}

function isRemoteImageExists(){
function isRemoteImageExists() {
# is_remote_image_exists repositoryName:Tag Digests
fullrepo=${1#*/}
repoName=${fullrepo%%:*}
tag=${fullrepo##*:}
res=$(aws ecr describe-images --repository-name "$repoName" --query "imageDetails[?(@.imageDigest=='$2')].contains(@.imageTags, '$tag') | [0]")

if [ "$res" == "true" ]; then
return 0
if [ "$res" == "true" ]; then
return 0
else
return 1
fi
}

function getLocalImageDigests(){
function getLocalImageDigests() {
x=$(docker image inspect --format='{{index .RepoDigests 0}}' "$1")
echo ${x##*@}
# docker images --digests --no-trunc -q "$1"
}

function inArray() {
local list=$2
local elem=$1
for i in ${list[@]}
do
if [ "$i" == "${elem}" ] ; then
return 0
fi
done
return 1
local list=$2
local elem=$1
for i in ${list[@]}; do
if [ "$i" == "${elem}" ]; then
return 0
fi
done
return 1
}

function pullAndPush(){
function pullAndPush() {
origimg="$1"
echo "------origimg:${origimg}------"
repo=`echo ${origimg}|cut -d: -f1`
repo=$(echo ${origimg} | cut -d: -f1)

docker pull $origimg
replaceDomainName $origimg
Expand All @@ -101,14 +105,12 @@ $(aws ecr get-login --no-include-email)
IMAGES_FILE_LIST='images.txt'

repos=$(grep -v ^# $IMAGES_FILE_LIST | cut -d: -f1 | sort -u)
for repo in ${repos[@]}
do
for repo in ${repos[@]}; do
replaceDomainName $repo
createEcrRepo $URI $repo
done

images=$(grep -v ^# $IMAGES_FILE_LIST)
for image in ${images[@]}
do
for image in ${images[@]}; do
pullAndPush $image
done
done
Loading