#!/usr/bin/env bash #Clyhtsuriva PURPLE=$(tput setaf 5) RED=$(tput setaf 1) NC=$(tput sgr0) #just for aesthetic when printing on stdout [ -z "$1" ] && printf "%s\n" "${RED}Image name required${NC}" && exit 1 #the user should give us the image name image="$1" host="localhost" port=5000 request="curl -s $host:$port/v2/$image/tags/list" json=$($request) #actually do the request printf "%s\n" "${PURPLE}request: $request" printf "%s\n" "json response: $json${NC}" grep -q "error" <<< "$json" && printf "%s\n" "${RED}Image not found${NC}" && exit 1 parsed=$(cut -d: -f3 <<< "$json") #only takes what's after "tags" i=1 max_found=0 buffer=0 while [ $max_found -eq 0 ]; do if ! grep -q "v" <<< "$(cut -d'"' -f$i <<< "$parsed")" ; then #if there is a v when cutting with the delimiter " latest=$(cut -d'"' -f$i <<< "$parsed") #since it's ok, we're saving it [ "$(sed 's/v//g' <<< "$latest")" -gt $buffer ] && \ buffer=$(sed 's/v//g' <<< "$latest") #save in buffer in greater than anything else fi ((i++)) # incrementing if [ -z "$(cut -d'"' -f"$i" <<< "$parsed")" ]; then max_found=1 #if it's empty, it means we are out of tags fi done latest=$buffer version=$latest ((version++)) printf "%s\n" "${PURPLE}latest version: v$latest" printf "%s\n" "new tag: v$version" printf "%s\n" "Tagging ...${NC}" docker tag "$image" localhost/"$image":v"$version" printf "%s\n" "${PURPLE}Pushing ...${NC}" docker push localhost/"$image":v"$version" printf "%s\n" "${PURPLE}Looking at the registry ...${NC}" $request