aboutsummaryrefslogtreecommitdiff
path: root/autotag.sh
blob: 4f443c53e55b62be20cc7fd723f40a19a4642a9f (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
56
#!/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