-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild-docker.sh
More file actions
186 lines (158 loc) Β· 4.95 KB
/
Copy pathbuild-docker.sh
File metadata and controls
186 lines (158 loc) Β· 4.95 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# CodeMedic Docker Build Script (Bash version)
# Builds the CodeMedic Docker container with versioning from Nerd Bank Git Versioning
set -e
REGISTRY=""
PUSH=false
BUILD_CONFIGURATION="Release"
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
-r|--registry)
REGISTRY="$2"
shift 2
;;
-p|--push)
PUSH=true
shift
;;
-c|--configuration)
BUILD_CONFIGURATION="$2"
shift 2
;;
-h|--help)
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -r, --registry REGISTRY Docker registry prefix (e.g., ghcr.io/owner)"
echo " -p, --push Push images to registry after build"
echo " -c, --configuration CONFIG Build configuration (Release or Debug)"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0"
echo " $0 --registry ghcr.io/csharpfritz --push"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
echo "π₯ CodeMedic Docker Build Script"
echo "================================="
echo ""
# Change to script directory
cd "$(dirname "$0")"
# Check if Docker is available
if ! command -v docker &> /dev/null; then
echo "β Error: Docker is not installed or not in PATH"
exit 1
fi
# Check if nbgv is available, if not install it
if ! command -v nbgv &> /dev/null; then
echo "π¦ Installing Nerd Bank Git Versioning CLI tool..."
dotnet tool install -g nbgv
if [ $? -ne 0 ]; then
echo "β Error: Failed to install nbgv"
exit 1
fi
# Add to PATH for current session
export PATH="$HOME/.dotnet/tools:$PATH"
fi
# Get version information from NBGv2
echo "π’ Retrieving version information from NBGv2..."
VERSION_JSON=$(nbgv get-version -f json)
if [ $? -ne 0 ]; then
echo "β Error: Failed to retrieve version information from NBGv2"
exit 1
fi
VERSION=$(echo "$VERSION_JSON" | grep -o '"Version":"[^"]*"' | cut -d'"' -f4)
SEMVER2=$(echo "$VERSION_JSON" | grep -o '"SemVer2":"[^"]*"' | cut -d'"' -f4)
ASSEMBLY_VERSION=$(echo "$VERSION_JSON" | grep -o '"AssemblyVersion":"[^"]*"' | cut -d'"' -f4)
FILE_VERSION=$(echo "$VERSION_JSON" | grep -o '"AssemblyFileVersion":"[^"]*"' | cut -d'"' -f4)
INFORMATIONAL_VERSION=$(echo "$VERSION_JSON" | grep -o '"AssemblyInformationalVersion":"[^"]*"' | cut -d'"' -f4)
VERSION_MAJOR=$(echo "$VERSION_JSON" | grep -o '"VersionMajor":[0-9]*' | cut -d':' -f2)
VERSION_MINOR=$(echo "$VERSION_JSON" | grep -o '"VersionMinor":[0-9]*' | cut -d':' -f2)
COMMIT=$(echo "$VERSION_JSON" | grep -o '"GitCommitIdShort":"[^"]*"' | cut -d'"' -f4)
MAJOR_MINOR="${VERSION_MAJOR}.${VERSION_MINOR}"
echo " Version: $VERSION"
echo " SemVer2: $SEMVER2"
echo " Assembly Version: $ASSEMBLY_VERSION"
echo " File Version: $FILE_VERSION"
echo " Informational: $INFORMATIONAL_VERSION"
echo " Commit: $COMMIT"
echo ""
# Construct image name
IMAGE_NAME="codemedic"
if [ -n "$REGISTRY" ]; then
IMAGE_NAME="$REGISTRY/$IMAGE_NAME"
fi
# Build tags
TAGS=(
"${IMAGE_NAME}:${SEMVER2}"
"${IMAGE_NAME}:${MAJOR_MINOR}"
"${IMAGE_NAME}:latest"
"${IMAGE_NAME}:${SEMVER2}-${COMMIT}"
)
echo "π³ Building Docker image..."
echo " Image name: $IMAGE_NAME"
echo " Tags:"
for tag in "${TAGS[@]}"; do
echo " - $tag"
done
echo ""
# Build the Docker image
BUILD_ARGS=(
"build"
"-f" "Dockerfile"
"--build-arg" "BUILD_CONFIGURATION=$BUILD_CONFIGURATION"
"--build-arg" "VERSION=$SEMVER2"
"--build-arg" "ASSEMBLY_VERSION=$ASSEMBLY_VERSION"
"--build-arg" "FILE_VERSION=$FILE_VERSION"
"--build-arg" "INFORMATIONAL_VERSION=$INFORMATIONAL_VERSION"
)
for tag in "${TAGS[@]}"; do
BUILD_ARGS+=("-t" "$tag")
done
BUILD_ARGS+=(".")
echo "π¨ Executing: docker ${BUILD_ARGS[*]}"
docker "${BUILD_ARGS[@]}"
if [ $? -ne 0 ]; then
echo "β Error: Docker build failed"
exit 1
fi
echo ""
echo "β
Docker image built successfully!"
echo ""
# Push if requested
if [ "$PUSH" = true ]; then
if [ -z "$REGISTRY" ]; then
echo "β Error: Cannot push without specifying a registry. Use --registry parameter."
exit 1
fi
echo "π€ Pushing images to registry..."
for tag in "${TAGS[@]}"; do
echo " Pushing: $tag"
docker push "$tag"
if [ $? -ne 0 ]; then
echo "β Error: Failed to push $tag"
exit 1
fi
done
echo ""
echo "β
All images pushed successfully!"
echo ""
fi
# Display usage examples
echo "π Usage Examples:"
echo " Run health check:"
echo " docker run --rm -v \$(pwd):/repo ${TAGS[0]} health /repo"
echo ""
echo " Show help:"
echo " docker run --rm ${TAGS[0]} --help"
echo ""
echo " Interactive shell:"
echo " docker run --rm -it --entrypoint /bin/bash ${TAGS[0]}"
echo ""