#!/usr/bin/env bash # # 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 # # Example: # ./scripts/deploy.sh v0.1.1 # set -euo pipefail # Check that the required GITEA_REPO_URL is set. if [ -z "${GITEA_REPO_URL:-}" ]; then echo "Error: GITEA_REPO_URL environment variable is not set." echo "Please set it to your full repository URL (e.g., https://gitea.com/user/repo)" exit 1 fi # 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 ---" # Extract the registry and repository path from the Gitea URL. 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 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 "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 --- # 1. Log in to the Gitea Docker registry. echo "--> Logging in to Docker registry at ${GITEA_REGISTRY}..." docker login "${GITEA_REGISTRY}" # 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 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 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"