build: add script to deploy docker container to gitea registry

This commit is contained in:
2025-06-29 13:28:37 +03:00
parent be3cee8e3c
commit 9335d69d88
2 changed files with 61 additions and 0 deletions

View File

@ -12,6 +12,9 @@
# Ignore local log directories if they are created. # Ignore local log directories if they are created.
/log /log
# Ignore developer tooling scripts
/scripts
# Ignore git history and local git configuration. # Ignore git history and local git configuration.
.git .git
.gitignore .gitignore

58
scripts/deploy.sh Executable file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env bash
#
# A script to build and push the Docker image to a Gitea container registry.
#
# Usage:
# export GITEA_REPO_URL="https://git.example.com/user/repo"
# ./scripts/deploy.sh <TAG>
#
# Example:
# ./scripts/deploy.sh v1.0.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
# Get the image tag from the first script argument, or default to "latest".
readonly IMAGE_TAG="${1:-latest}"
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 full Docker image name and tag.
readonly FULL_IMAGE_NAME="${GITEA_REGISTRY}/${REPO_PATH}:${IMAGE_TAG}"
# --- 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 "--------------------------------"
# --- 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 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}" .
# 3. Push the built image to the Gitea registry.
echo "--> Pushing image to registry..."
docker push "${FULL_IMAGE_NAME}"
# --- Success Message ---
echo
echo "> Success! Image has been pushed."
echo " You can now pull it using: docker pull ${FULL_IMAGE_NAME}"