@@ -51,6 +51,14 @@ deploy_docker_stack() {
51
51
docker -H " $docker_host " stack deploy " ${compose_args[@]} " --detach=false --prune " $spin_project_name -$deployment_environment "
52
52
if [ $? -eq 0 ]; then
53
53
echo " ${BOLD}${BLUE} 🎉 Successfully deployed Docker stack on $manager_host .${RESET} "
54
+
55
+ # Clean up unused images
56
+ echo " ${BOLD}${BLUE} 🧹 Cleaning up unused Docker images on $manager_host ...${RESET} "
57
+ if docker -H " $docker_host " image prune -f; then
58
+ echo " ${BOLD}${BLUE} ✨ Successfully cleaned up unused Docker images.${RESET} "
59
+ else
60
+ echo " ${BOLD}${YELLOW} ⚠️ Warning: Failed to clean up unused Docker images.${RESET} "
61
+ fi
54
62
else
55
63
echo " ${BOLD}${RED} ❌ Failed to deploy Docker stack on $manager_host .${RESET} "
56
64
exit 1
@@ -187,7 +195,7 @@ action_deploy() {
187
195
registry_port=" ${SPIN_REGISTRY_PORT:- 5080} "
188
196
build_platform=" ${SPIN_BUILD_PLATFORM:- " linux/amd64" } "
189
197
image_prefix=" ${SPIN_BUILD_IMAGE_PREFIX:- " localhost:$registry_port " } "
190
- image_tag=" ${SPIN_BUILD_TAG:- " latest " } "
198
+ image_tag=" ${SPIN_BUILD_TAG:- $(date + % Y % m % d % H % M % S) } "
191
199
ssh_port=" ${SPIN_SSH_PORT:- 22} "
192
200
ssh_user=" ${SPIN_SSH_USER:- " deploy" } "
193
201
spin_project_name=" ${SPIN_PROJECT_NAME:- " spin" } "
@@ -208,26 +216,32 @@ action_deploy() {
208
216
209
217
# Start the registry with the correct user and group ID
210
218
echo " ${BOLD}${BLUE} 🚀 Starting local Docker registry...${RESET} "
211
- docker run --rm -d -p " $registry_port :5000" --user " ${SPIN_USER_ID} :${SPIN_GROUP_ID} " -v " $SPIN_CACHE_DIR /registry:/var/lib/registry" --name $spin_registry_name " $registry_image "
219
+ docker run --rm -d -p " $registry_port :5000" --user " ${SPIN_USER_ID} :${SPIN_GROUP_ID} " -v " $SPIN_CACHE_DIR /registry:/var/lib/registry" --name " $spin_registry_name " " $registry_image "
212
220
fi
213
221
214
222
# Build and push each Dockerfile
215
223
for dockerfile in " ${dockerfiles[@]} " ; do
216
224
# Generate variable name based on Dockerfile name
217
- var_name =$( echo " $dockerfile " | tr ' [:lower:].' ' [:upper:]_' )
218
- var_name =" SPIN_IMAGE_${var_name } "
225
+ spin_image_var_name =$( echo " $dockerfile " | tr ' [:lower:].' ' [:upper:]_' )
226
+ spin_image_var_name =" SPIN_IMAGE_${spin_image_var_name } "
219
227
220
228
# Set and export image name
221
- image_name=" ${image_prefix} /$( echo " $dockerfile " | tr ' [:upper:]' ' [:lower:]' ) :${image_tag} "
222
- export " $var_name =$image_name "
223
-
224
- # Build the Docker image
225
- echo " ${BOLD}${BLUE} 🐳 Building Docker image '$image_name ' from '$dockerfile '...${RESET} "
226
- docker buildx build --push --platform " $build_platform " -t " $image_name " -f " $dockerfile " .
227
- if [ $? -eq 0 ]; then
228
- echo " ${BOLD}${BLUE} 📦 Successfully built '$image_name ' from '$dockerfile '...${RESET} "
229
+ full_docker_image_name=" ${image_prefix} /$( echo " $dockerfile " | tr ' [:upper:]' ' [:lower:]' ) "
230
+ versioned_image=" ${full_docker_image_name} :${image_tag} "
231
+ latest_image=" ${full_docker_image_name} :latest"
232
+
233
+ # Export the versioned image name for other scripts to use
234
+ export " $spin_image_var_name =$versioned_image "
235
+
236
+ # Build and tag the Docker image with both tags
237
+ echo " ${BOLD}${BLUE} 🐳 Building Docker image '$versioned_image ' from '$dockerfile '...${RESET} "
238
+ if docker buildx build --push --platform " $build_platform " \
239
+ -t " $versioned_image " \
240
+ -t " $latest_image " \
241
+ -f " $dockerfile " . ; then
242
+ echo " ${BOLD}${BLUE} 📦 Successfully built '$versioned_image ' from '$dockerfile '...${RESET} "
229
243
else
230
- echo " ${BOLD}${RED} ❌ Failed to build '$image_name ' from '$dockerfile '.${RESET} "
244
+ echo " ${BOLD}${RED} ❌ Failed to build '$versioned_image ' from '$dockerfile '.${RESET} "
231
245
exit 1
232
246
fi
233
247
done
@@ -255,7 +269,7 @@ action_deploy() {
255
269
# Read the file content and escape newlines for Docker
256
270
AUTHORIZED_KEYS=$( awk 1 ORS=' \\n' " $SPIN_CI_FOLDER /AUTHORIZED_KEYS" | sed ' s/\\n$//' )
257
271
export AUTHORIZED_KEYS
258
- echo " ${BOLD}${BLUE} 🔑 Authorized keys loaded and exported as AUTHORIZED_KEYS${RESET} "
272
+ echo " ${BOLD}${BLUE} Authorized keys loaded and exported as AUTHORIZED_KEYS${RESET} "
259
273
else
260
274
echo " ${BOLD}${YELLOW} ⚠️ Warning: No AUTHORIZED_KEYS file found in $SPIN_CI_FOLDER ${RESET} "
261
275
fi
0 commit comments