feat(build): create a base and latest tag while building docker image

This commit is contained in:
2025-06-29 18:22:12 +03:00
parent 68b553382a
commit 42e1edfd66

View File

@ -1,13 +1,14 @@
#!/usr/bin/env bash #!/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: # Usage:
# export GITEA_REPO_URL="https://git.example.com/user/repo" # export GITEA_REPO_URL="https://git.example.com/user/repo"
# ./scripts/deploy.sh <TAG> # ./scripts/deploy.sh <SPECIFIC_TAG>
# #
# Example: # Example:
# ./scripts/deploy.sh v1.0.1 # ./scripts/deploy.sh v0.1.1
# #
set -euo pipefail set -euo pipefail
@ -19,8 +20,15 @@ if [ -z "${GITEA_REPO_URL:-}" ]; then
exit 1 exit 1
fi fi
# Get the image tag from the first script argument, or default to "latest". # Check that a specific tag argument is provided.
readonly IMAGE_TAG="${1:-latest}" 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 ---" 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 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$||') readonly REPO_PATH=$(echo "$GITEA_REPO_URL" | sed -e 's|https://[^/]*\/||' -e 's|\.git$||')
# Construct the full Docker image name and tag. # Construct the base image name (without any tag).
readonly FULL_IMAGE_NAME="${GITEA_REGISTRY}/${REPO_PATH}:${IMAGE_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 --- # --- Print Summary ---
echo "Registry URL: ${GITEA_REGISTRY}" echo "Registry URL: ${GITEA_REGISTRY}"
echo "Repository Path: ${REPO_PATH}" echo "Repository Path: ${REPO_PATH}"
echo "Image Tag: ${IMAGE_TAG}" echo "Base Image Name: ${IMAGE_NAME_BASE}"
echo "Full Image Name: ${FULL_IMAGE_NAME}" echo
echo "Tags to be created:"
echo " - Specific: ${PRIMARY_TAG}"
echo " - Base: ${BASE_TAG}"
echo " - Latest: latest"
echo "--------------------------------" echo "--------------------------------"
# --- Execution --- # --- Execution ---
@ -43,16 +59,24 @@ echo "--------------------------------"
echo "--> Logging in to Docker registry at ${GITEA_REGISTRY}..." echo "--> Logging in to Docker registry at ${GITEA_REGISTRY}..."
docker login "${GITEA_REGISTRY}" docker login "${GITEA_REGISTRY}"
# 2. Build the Docker image with the full name and tag. # 2. Build the Docker image with all the desired tags.
# We pass the root of the repo as the build context. # The `docker build` command can accept multiple -t flags.
echo "--> Building image: ${FULL_IMAGE_NAME}..." echo "--> Building image with multiple tags..."
docker build -t "${FULL_IMAGE_NAME}" -f ./docker/Dockerfile . 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. # 3. Push all tags for the repository to the registry.
echo "--> Pushing image to registry..." # The `--all-tags` flag is the most efficient way to do this.
docker push "${FULL_IMAGE_NAME}" echo "--> Pushing all tags to the registry..."
docker push --all-tags "${IMAGE_NAME_BASE}"
# --- Success Message --- # --- Success Message ---
echo echo
echo "> Success! Image has been pushed." echo "> Success! Image has been pushed with all tags."
echo " You can now pull it using: docker pull ${FULL_IMAGE_NAME}" 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"