feat(build): create a base and latest tag while building docker image
This commit is contained in:
@ -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 <TAG>
|
||||
# ./scripts/deploy.sh <SPECIFIC_TAG>
|
||||
#
|
||||
# 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}"
|
||||
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"
|
Reference in New Issue
Block a user