Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created November 12, 2019 12:00
Show Gist options
  • Save alejandrobernardis/2fab56b559ddadf1a9a6147607fc6bc6 to your computer and use it in GitHub Desktop.
Save alejandrobernardis/2fab56b559ddadf1a9a6147607fc6bc6 to your computer and use it in GitHub Desktop.
Script para el transporte de imágenes
#!/bin/bash
# Author: Alejandro M. BERNARDIS
# Created: 2019/08/30
set -e
# colors
BLACK=$(printf '\033[30m')
RED=$(printf '\033[31m')
GREEN=$(printf '\033[32m')
YELLOW=$(printf '\033[33m')
BLUE=$(printf '\033[34m')
MAGENTA=$(printf '\033[35m')
CYAN=$(printf '\033[36m')
GRAY=$(printf '\033[37m')
# styles
NORMAL=$(printf '\033[0m')
BOLD=$(printf '\033[1m')
RESET=$(printf '\033[m')
# welcome
echo "${BOLD}${CYAN} ## TRANSPORT MANAGER ## ${RESET}"
# checkpoint...
[[ "$(id -u)" == '0' ]] \
|| (echo " ! ${YELLOW}Run with ${BOLD}\"root\"${RESET}" && exit 1)
# variables
reg_user=${1:-""}
reg_dev=""
reg_pro=""
tag_from="rc"
tag_to="latest"
# session
if [[ ! -z "${reg_user}" ]]; then
# password
echo -n " > ${BOLD}${BLUE}Password${RESET}: "
read -s reg_pass
echo '***'
[ ! -z "${reg_pass}" ] || (echo " - ${RED}Password is empty.${RESET}" && exit 1)
# registry login
echo "docker login -u ${reg_user} -p ${reg_pass} ${reg_pro}" \
| bash
[ $? -eq 0 ] || (echo " - ${RED}Login failed.${RESET}" && exit 2)
echo " - ${GREEN}Login succeeded into ${BOLD}\"${reg_pro}\".${RESET}"
fi
# tagging
docker images "${reg_dev}/dash/*:${tag_from}" --format "{{.Repository}}" \
| grep -Po 'dash/.*' \
| awk -v origin="${reg_dev}/%s:${tag_from}" -v target="${reg_pro}/%s:${tag_to}" \
'{printf("docker tag " origin " " target "\n", $1, $1);}' \
| bash
[ $? -eq 0 ] || (echo " - ${RED}Tagging failed.${RESET}" && exit 3)
image_filter="${reg_pro}/dash/*:${tag_to}"
total=$(docker images "${image_filter}" | grep -P '/dash/' | wc -l)
echo " - ${GREEN}Has been retagged \"${total}\" image(s).${RESET}"
# transport
docker images "${image_filter}" --format "docker push {{.Repository}}:{{.Tag}}" \
| bash
[ $? -eq 0 ] || (echo " - ${RED}Transport failed.${RESET}" && exit 4)
echo " - ${GREEN}Has been transported \"${total}\" image(s).${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment