Skip to content

Commit 9b891d9

Browse files
committed
Enhance deploy script: add cleanup for unused Docker images, update image tagging to use timestamp, and improve variable naming for clarity.
1 parent 3be5591 commit 9b891d9

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

lib/actions/deploy.sh

+28-14
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ deploy_docker_stack() {
5151
docker -H "$docker_host" stack deploy "${compose_args[@]}" --detach=false --prune "$spin_project_name-$deployment_environment"
5252
if [ $? -eq 0 ]; then
5353
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
5462
else
5563
echo "${BOLD}${RED}❌ Failed to deploy Docker stack on $manager_host.${RESET}"
5664
exit 1
@@ -187,7 +195,7 @@ action_deploy() {
187195
registry_port="${SPIN_REGISTRY_PORT:-5080}"
188196
build_platform="${SPIN_BUILD_PLATFORM:-"linux/amd64"}"
189197
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)}"
191199
ssh_port="${SPIN_SSH_PORT:-22}"
192200
ssh_user="${SPIN_SSH_USER:-"deploy"}"
193201
spin_project_name="${SPIN_PROJECT_NAME:-"spin"}"
@@ -208,26 +216,32 @@ action_deploy() {
208216

209217
# Start the registry with the correct user and group ID
210218
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"
212220
fi
213221

214222
# Build and push each Dockerfile
215223
for dockerfile in "${dockerfiles[@]}"; do
216224
# 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}"
219227

220228
# 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}"
229243
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}"
231245
exit 1
232246
fi
233247
done
@@ -255,7 +269,7 @@ action_deploy() {
255269
# Read the file content and escape newlines for Docker
256270
AUTHORIZED_KEYS=$(awk 1 ORS='\\n' "$SPIN_CI_FOLDER/AUTHORIZED_KEYS" | sed 's/\\n$//')
257271
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}"
259273
else
260274
echo "${BOLD}${YELLOW}⚠️ Warning: No AUTHORIZED_KEYS file found in $SPIN_CI_FOLDER${RESET}"
261275
fi

0 commit comments

Comments
 (0)