aboutsummaryrefslogtreecommitdiff
path: root/autotag.sh
blob: d3de206d9d97deab79265390dcb64fc92d9ba772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env bash
#Clyhtsuriva

PURPLE='\033[1;35m'
RED='\033[0;31m'
NC='\033[0m'

#just for aesthetic when printing on stdout
[ -z "$1" ] && printf "${RED}Image name required${NC}\n" && 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 "${PURPLE}request: $request\n"
printf "json response: $json${NC}\n"

[ -n "$(grep "error" <<< $json)" ] && printf "${RED}Image not found${NC}\n" && 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 [ ! -z $(grep "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 "${PURPLE}latest version: v$latest\n"
printf "new tag: v$version\n"

printf "Tagging ...${NC}\n"
docker tag $image localhost/$image:v$version
printf "${PURPLE}Pushing ...${NC}\n"
docker push localhost/$image:v$version

printf "${PURPLE}Looking at the registry ...${NC}\n"
$request