-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_tag.sh
100 lines (86 loc) · 2.64 KB
/
docker_tag.sh
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -e
# Load configuration from yaml
eval $(yq e '.docker | to_entries | .[] | "export \(.key)=\(.value)"' docker-config.yml)
# Harbor authentication token
get_harbor_token() {
local token_url="https://${HARBOR_URL}/service/token?service=harbor-registry&scope=repository:${HARBOR_PROJECT}/${HARBOR_REPO}:pull,push"
local token_response=$(curl -s -u "${HARBOR_USERNAME}:${HARBOR_PASSWORD}" "$token_url")
echo "$token_response" | jq -r '.token'
}
# Get tags from Harbor
get_harbor_tags() {
local token=$1
if [ -z "$token" ]; then
return 1
fi
# Try v2 API first
local api_url="https://${HARBOR_URL}/v2/${HARBOR_PROJECT}/${HARBOR_REPO}/tags/list"
local response=$(curl -s -H "Authorization: Bearer $token" "$api_url")
local tags=$(echo "$response" | jq -r '.tags[]' 2>/dev/null)
# If V2 API fails, try V2.0 API
if [ -z "$tags" ]; then
api_url="https://${HARBOR_URL}/api/v2.0/projects/${HARBOR_PROJECT}/repositories/${HARBOR_REPO}/artifacts"
response=$(curl -s -H "Authorization: Bearer $token" "$api_url")
tags=$(echo "$response" | jq -r '.[].tags[].name' 2>/dev/null)
fi
echo "$tags"
}
# Get latest version from tags
get_latest_version() {
local tags="$1"
local latest_version="0.0.0"
while read -r tag; do
if ! [[ $tag =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
continue
fi
if [ "$(printf '%s\n' "$latest_version" "$tag" | sort -V | tail -n1)" = "$tag" ]; then
latest_version=$tag
fi
done <<< "$tags"
echo "$latest_version"
}
main() {
TOKEN=$(get_harbor_token)
if [ -z "$TOKEN" ]; then
echo "Failed to get valid token"
exit 1
fi
TAGS=$(get_harbor_tags "$TOKEN")
if [ -z "$TAGS" ]; then
NEW_TAG="0.0.1"
else
LATEST_TAG=$(get_latest_version "$TAGS")
IFS='.' read -ra PARTS <<< "$LATEST_TAG"
MAJOR=${PARTS[0]}
MINOR=${PARTS[1]}
PATCH=${PARTS[2]}
case "$VERSION_PART" in
"Major")
NEW_TAG="$((MAJOR + 1)).0.0"
;;
"Minor")
NEW_TAG="$MAJOR.$((MINOR + 1)).0"
;;
"Patch")
NEW_TAG="$MAJOR.$MINOR.$((PATCH + 1))"
;;
*)
echo "Invalid version part specified. Usage: $0 [Major|Minor|Patch]"
exit 1
;;
esac
fi
create_env_file
}
# Create .env file
create_env_file() {
cat << EOF > .env
HARBOR_URL=$HARBOR_URL
HARBOR_PROJECT=$HARBOR_PROJECT
HARBOR_REPO=$HARBOR_REPO
NEW_TAG=$NEW_TAG
PUSH_TO_HARBOR=$PUSH_TO_HARBOR
EOF
}
main