| #!/bin/bash |
| |
| set -ex |
| |
| trap cleanup_exit INT TERM EXIT |
| |
| cleanup_exit() |
| { |
| rm -rf ${HOME}/.docker |
| rm -f ${WORKSPACE}/{log,config.json,version.txt} |
| } |
| |
| docker_log_in() |
| { |
| mkdir -p ${HOME}/.docker |
| cat > ${HOME}/.docker/config.json << EOF |
| { |
| "auths": { |
| "https://index.docker.io/v1/": { |
| "auth": "${DOCKER_AUTH}" |
| } |
| } |
| } |
| EOF |
| chmod 0600 ${HOME}/.docker/config.json |
| } |
| export AWS_DEFAULT_REGION=eu-west-1 |
| update_images=$(find -type f -name .docker-tag) |
| docker_log_in |
| for imagename in ${update_images}; do |
| ( |
| docker_tag=$(cat $imagename) |
| if [ x"${GERRIT_BRANCH}" != x"master" ]; then |
| new_tag=${docker_tag}-${GERRIT_BRANCH} |
| docker tag ${docker_tag} ${new_tag} |
| docker_tag=${new_tag} |
| fi |
| |
| for i in 30 60 120; |
| do |
| # The following code pushes docker images to both DockerHub and PRIVATE_CONTAINER_REGISTRY |
| # PRIVATE_CONTAINER_REGISTRY is used to overcome the DockerHub rate limiting |
| mirror_tag=${PRIVATE_CONTAINER_REGISTRY}/${docker_tag} |
| docker tag ${docker_tag} ${mirror_tag} |
| aws ecr get-login-password | docker login --username AWS --password-stdin ${PRIVATE_CONTAINER_REGISTRY} |
| REPO_NAME=$(echo "$docker_tag" | cut -d: -f1) |
| # create if repository not already present in PRIVATE_CONTAINER_REGISTRY |
| aws ecr describe-repositories --repository-names ${REPO_NAME} || aws ecr create-repository --repository-name ${REPO_NAME} |
| docker push ${docker_tag} && docker push ${mirror_tag} && exit 0 || true |
| sleep $i |
| docker_log_in |
| done |
| exit 1 |
| )||echo $imagename push failed >> ${WORKSPACE}/log |
| done |
| |
| if [ -e ${WORKSPACE}/log ] |
| then |
| echo "some images failed:" |
| cat ${WORKSPACE}/log |
| exit 1 |
| fi |