From 9b891d9f671973dba58dc044dc9583d68cf59e79 Mon Sep 17 00:00:00 2001 From: Jay Rogers Date: Thu, 12 Dec 2024 18:20:33 -0600 Subject: [PATCH] Enhance deploy script: add cleanup for unused Docker images, update image tagging to use timestamp, and improve variable naming for clarity. --- lib/actions/deploy.sh | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/actions/deploy.sh b/lib/actions/deploy.sh index e2518d78..29bc614a 100755 --- a/lib/actions/deploy.sh +++ b/lib/actions/deploy.sh @@ -51,6 +51,14 @@ deploy_docker_stack() { docker -H "$docker_host" stack deploy "${compose_args[@]}" --detach=false --prune "$spin_project_name-$deployment_environment" if [ $? -eq 0 ]; then echo "${BOLD}${BLUE}๐ŸŽ‰ Successfully deployed Docker stack on $manager_host.${RESET}" + + # Clean up unused images + echo "${BOLD}${BLUE}๐Ÿงน Cleaning up unused Docker images on $manager_host...${RESET}" + if docker -H "$docker_host" image prune -f; then + echo "${BOLD}${BLUE}โœจ Successfully cleaned up unused Docker images.${RESET}" + else + echo "${BOLD}${YELLOW}โš ๏ธ Warning: Failed to clean up unused Docker images.${RESET}" + fi else echo "${BOLD}${RED}โŒ Failed to deploy Docker stack on $manager_host.${RESET}" exit 1 @@ -187,7 +195,7 @@ action_deploy() { registry_port="${SPIN_REGISTRY_PORT:-5080}" build_platform="${SPIN_BUILD_PLATFORM:-"linux/amd64"}" image_prefix="${SPIN_BUILD_IMAGE_PREFIX:-"localhost:$registry_port"}" - image_tag="${SPIN_BUILD_TAG:-"latest"}" + image_tag="${SPIN_BUILD_TAG:-$(date +%Y%m%d%H%M%S)}" ssh_port="${SPIN_SSH_PORT:-22}" ssh_user="${SPIN_SSH_USER:-"deploy"}" spin_project_name="${SPIN_PROJECT_NAME:-"spin"}" @@ -208,26 +216,32 @@ action_deploy() { # Start the registry with the correct user and group ID echo "${BOLD}${BLUE}๐Ÿš€ Starting local Docker registry...${RESET}" - 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" + 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" fi # Build and push each Dockerfile for dockerfile in "${dockerfiles[@]}"; do # Generate variable name based on Dockerfile name - var_name=$(echo "$dockerfile" | tr '[:lower:].' '[:upper:]_') - var_name="SPIN_IMAGE_${var_name}" + spin_image_var_name=$(echo "$dockerfile" | tr '[:lower:].' '[:upper:]_') + spin_image_var_name="SPIN_IMAGE_${spin_image_var_name}" # Set and export image name - image_name="${image_prefix}/$(echo "$dockerfile" | tr '[:upper:]' '[:lower:]'):${image_tag}" - export "$var_name=$image_name" - - # Build the Docker image - echo "${BOLD}${BLUE}๐Ÿณ Building Docker image '$image_name' from '$dockerfile'...${RESET}" - docker buildx build --push --platform "$build_platform" -t "$image_name" -f "$dockerfile" . - if [ $? -eq 0 ]; then - echo "${BOLD}${BLUE}๐Ÿ“ฆ Successfully built '$image_name' from '$dockerfile'...${RESET}" + full_docker_image_name="${image_prefix}/$(echo "$dockerfile" | tr '[:upper:]' '[:lower:]')" + versioned_image="${full_docker_image_name}:${image_tag}" + latest_image="${full_docker_image_name}:latest" + + # Export the versioned image name for other scripts to use + export "$spin_image_var_name=$versioned_image" + + # Build and tag the Docker image with both tags + echo "${BOLD}${BLUE}๐Ÿณ Building Docker image '$versioned_image' from '$dockerfile'...${RESET}" + if docker buildx build --push --platform "$build_platform" \ + -t "$versioned_image" \ + -t "$latest_image" \ + -f "$dockerfile" .; then + echo "${BOLD}${BLUE}๐Ÿ“ฆ Successfully built '$versioned_image' from '$dockerfile'...${RESET}" else - echo "${BOLD}${RED}โŒ Failed to build '$image_name' from '$dockerfile'.${RESET}" + echo "${BOLD}${RED}โŒ Failed to build '$versioned_image' from '$dockerfile'.${RESET}" exit 1 fi done @@ -255,7 +269,7 @@ action_deploy() { # Read the file content and escape newlines for Docker AUTHORIZED_KEYS=$(awk 1 ORS='\\n' "$SPIN_CI_FOLDER/AUTHORIZED_KEYS" | sed 's/\\n$//') export AUTHORIZED_KEYS - echo "${BOLD}${BLUE}๐Ÿ”‘ Authorized keys loaded and exported as AUTHORIZED_KEYS${RESET}" + echo "${BOLD}${BLUE} Authorized keys loaded and exported as AUTHORIZED_KEYS${RESET}" else echo "${BOLD}${YELLOW}โš ๏ธ Warning: No AUTHORIZED_KEYS file found in $SPIN_CI_FOLDER${RESET}" fi