-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.sh
163 lines (136 loc) · 3.96 KB
/
build.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
set -eo pipefail
# Simplify directory references
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)"
PROJECT_ROOT_DIR="$SCRIPT_DIR"
# Default values
DOCKER_ORGANIZATIONS="${DOCKER_ORGANIZATIONS:-"docker.io/serversideup ghcr.io/serversideup"}"
DOCKER_REPOSITORY_NAME="${DOCKER_REPOSITORY_NAME:-"docker-ssh"}"
GITHUB_REF_NAME="${GITHUB_REF_NAME:-""}"
PRINT_TAGS_ONLY=false
RELEASE_TYPE="dev"
VERSION=""
# At the top with other variable declarations
declare -a tags=()
##################################################
# Functions
##################################################
add_tag() {
local new_tag="$1"
local prefix=""
# Set prefix based on RELEASE_TYPE
if [ "$RELEASE_TYPE" != "latest" ]; then
prefix="${RELEASE_TYPE}-"
fi
# Prevent duplicate prefixes
if [[ "$new_tag-" == "$prefix" ]]; then
prefix=""
fi
# Construct the full tag
full_tag="${prefix}${new_tag}"
# Add tags for each Docker organization
for org in $DOCKER_ORGANIZATIONS; do
if [ -n "$GITHUB_REF_NAME" ] && [ "$RELEASE_TYPE" == "pr" ]; then
# Only add PR-specific tags
tags+=("${org}/${DOCKER_REPOSITORY_NAME}:${full_tag}-${GITHUB_REF_NAME}")
break
else
# Add the regular tag
tags+=("${org}/${DOCKER_REPOSITORY_NAME}:${full_tag}")
fi
done
}
build_image() {
echo "Building image..."
# Generate tag arguments for docker buildx
local tag_args=()
while IFS= read -r tag; do
tag_args+=("-t" "$tag")
done < <(generate_tags)
# Build the image with all tags
docker buildx build \
-f "$(pwd)/src/Dockerfile" \
"${tag_args[@]}" \
.
echo "✅ Image built with tags:"
generate_tags
}
generate_tags() {
local tags=()
if [ -n "$VERSION" ]; then
# Strip 'v' prefix if present
VERSION="${VERSION#v}"
# Split version into major, minor, patch
local major=$(echo "$VERSION" | cut -d. -f1)
local minor=$(echo "$VERSION" | cut -d. -f2)
local patch=$(echo "$VERSION" | cut -d. -f3)
# Add all version tags
add_tag "v${major}.${minor}.${patch}" # v3.0.1
add_tag "v${major}.${minor}" # v3.0
add_tag "v${major}" # v3
fi
# Add release type tag
add_tag "$RELEASE_TYPE"
# Print tags
printf '%s\n' "${tags[@]}" | sort -u
}
print_tags() {
local tags=($(generate_tags))
echo "The following tags have been generated (Release type: $RELEASE_TYPE):"
printf '%s\n' "${tags[@]}" | sort
# Save to GitHub's environment if in CI
if [[ $CI == "true" && -n "$GITHUB_ENV" ]]; then
{
echo "DOCKER_TAGS<<EOF"
printf '%s\n' "${tags[@]}"
echo "EOF"
} >> "$GITHUB_ENV"
fi
}
help_menu() {
echo "Usage: $0 [options]"
echo
echo "This script generates Docker tags for the SSH image."
echo
echo "Optional arguments:"
echo " --version <version> Set the version (e.g., 3.0.1, v3.0.1)"
echo " --release-type <type> Set the release type (e.g., latest, beta, edge). Default: dev"
echo " --repository <repos> Space-separated list of Docker repositories"
}
##################################################
# Main
##################################################
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--version)
VERSION="$2"
shift 2
;;
--release-type)
RELEASE_TYPE="$2"
shift 2
;;
--repository)
DOCKER_ORGANIZATIONS="$2"
shift 2
;;
--print-tags-only)
PRINT_TAGS_ONLY=true
shift
;;
--help)
help_menu
exit 0
;;
*)
echo "Unknown option: $1"
help_menu
exit 1
;;
esac
done
print_tags
if [ "$PRINT_TAGS_ONLY" = false ]; then
build_image
fi