From 9335d69d888c5e764d654522d9b2396fad853102 Mon Sep 17 00:00:00 2001 From: Timofey Gelazoniya Date: Sun, 29 Jun 2025 13:28:37 +0300 Subject: [PATCH] build: add script to deploy docker container to gitea registry --- .dockerignore | 3 +++ scripts/deploy.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100755 scripts/deploy.sh diff --git a/.dockerignore b/.dockerignore index ec4abb3..2645de1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -12,6 +12,9 @@ # Ignore local log directories if they are created. /log +# Ignore developer tooling scripts +/scripts + # Ignore git history and local git configuration. .git .gitignore diff --git a/scripts/deploy.sh b/scripts/deploy.sh new file mode 100755 index 0000000..e2c9e48 --- /dev/null +++ b/scripts/deploy.sh @@ -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 +# +# 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}" \ No newline at end of file