From 42e1edfd6611390c6e1d948bd00a4af7b2b5c1fa Mon Sep 17 00:00:00 2001 From: Timofey Gelazoniya Date: Sun, 29 Jun 2025 18:22:12 +0300 Subject: [PATCH] feat(build): create a base and latest tag while building docker image --- scripts/deploy.sh | 64 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 0f5508d..a623230 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,13 +1,14 @@ #!/usr/bin/env bash # -# A script to build and push the Docker image to a Gitea container registry. +# A script to build and push a Docker image with multiple tags to a Gitea container registry. +# It automatically creates a 'base' version tag (e.g., v0.1.1 from v0.1.1-2) and a 'latest' tag. # # Usage: # export GITEA_REPO_URL="https://git.example.com/user/repo" -# ./scripts/deploy.sh +# ./scripts/deploy.sh # # Example: -# ./scripts/deploy.sh v1.0.1 +# ./scripts/deploy.sh v0.1.1 # set -euo pipefail @@ -19,8 +20,15 @@ if [ -z "${GITEA_REPO_URL:-}" ]; then exit 1 fi -# Get the image tag from the first script argument, or default to "latest". -readonly IMAGE_TAG="${1:-latest}" +# Check that a specific tag argument is provided. +if [ -z "${1:-}" ]; then + echo "Error: You must provide a specific version tag as an argument." + echo "Example: ./scripts/deploy.sh v0.1.1-2" + exit 1 +fi + +# The specific version tag from the script argument (e.g., v0.1.1-2). +readonly PRIMARY_TAG="$1" echo "--- Preparing for deployment ---" @@ -28,14 +36,22 @@ echo "--- Preparing for deployment ---" readonly GITEA_REGISTRY=$(echo "$GITEA_REPO_URL" | sed -e 's|https://||' -e 's|/.*$||') readonly REPO_PATH=$(echo "$GITEA_REPO_URL" | sed -e 's|https://[^/]*\/||' -e 's|\.git$||') -# Construct the full Docker image name and tag. -readonly FULL_IMAGE_NAME="${GITEA_REGISTRY}/${REPO_PATH}:${IMAGE_TAG}" +# Construct the base image name (without any tag). +readonly IMAGE_NAME_BASE="${GITEA_REGISTRY}/${REPO_PATH}" + +# Derive the base tag by removing the build number (e.g., "-2") from the primary tag. +# This command removes a hyphen followed by numbers from the end of the string. +readonly BASE_TAG=$(echo "$PRIMARY_TAG" | sed 's/-[0-9]\+$//') # --- Print Summary --- -echo "Registry URL: ${GITEA_REGISTRY}" -echo "Repository Path: ${REPO_PATH}" -echo "Image Tag: ${IMAGE_TAG}" -echo "Full Image Name: ${FULL_IMAGE_NAME}" +echo "Registry URL: ${GITEA_REGISTRY}" +echo "Repository Path: ${REPO_PATH}" +echo "Base Image Name: ${IMAGE_NAME_BASE}" +echo +echo "Tags to be created:" +echo " - Specific: ${PRIMARY_TAG}" +echo " - Base: ${BASE_TAG}" +echo " - Latest: latest" echo "--------------------------------" # --- Execution --- @@ -43,16 +59,24 @@ echo "--------------------------------" echo "--> Logging in to Docker registry at ${GITEA_REGISTRY}..." docker login "${GITEA_REGISTRY}" -# 2. Build the Docker image with the full name and tag. -# We pass the root of the repo as the build context. -echo "--> Building image: ${FULL_IMAGE_NAME}..." -docker build -t "${FULL_IMAGE_NAME}" -f ./docker/Dockerfile . +# 2. Build the Docker image with all the desired tags. +# The `docker build` command can accept multiple -t flags. +echo "--> Building image with multiple tags..." +docker build \ + -t "${IMAGE_NAME_BASE}:${PRIMARY_TAG}" \ + -t "${IMAGE_NAME_BASE}:${BASE_TAG}" \ + -t "${IMAGE_NAME_BASE}:latest" \ + -f ./docker/Dockerfile . -# 3. Push the built image to the Gitea registry. -echo "--> Pushing image to registry..." -docker push "${FULL_IMAGE_NAME}" +# 3. Push all tags for the repository to the registry. +# The `--all-tags` flag is the most efficient way to do this. +echo "--> Pushing all tags to the registry..." +docker push --all-tags "${IMAGE_NAME_BASE}" # --- Success Message --- echo -echo "> Success! Image has been pushed." -echo " You can now pull it using: docker pull ${FULL_IMAGE_NAME}" \ No newline at end of file +echo "> Success! Image has been pushed with all tags." +echo " You can pull it using:" +echo " docker pull ${IMAGE_NAME_BASE}:${PRIMARY_TAG}" +echo " docker pull ${IMAGE_NAME_BASE}:${BASE_TAG}" +echo " docker pull ${IMAGE_NAME_BASE}:latest" \ No newline at end of file