Files
loc_az_hci/scripts/lib/git_helpers.sh
defiQUG c39465c2bd
Some checks failed
Test / test (push) Has been cancelled
Initial commit: loc_az_hci (smom-dbis-138 excluded via .gitignore)
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 09:04:46 -08:00

78 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Git/Gitea Helper Functions
# Loads credentials from .env file for automated Git operations
set -euo pipefail
# Load Git credentials from .env file
load_git_credentials() {
local env_file="${1:-${PROJECT_ROOT:-.}/.env}"
if [ -f "$env_file" ]; then
# Source Gitea credentials
export GITEA_URL="${GITEA_URL:-http://192.168.1.121:3000}"
export GITEA_USERNAME="${GITEA_USERNAME:-pandoramannli}"
export GITEA_PASSWORD="${GITEA_PASSWORD:-admin123}"
export GITEA_TOKEN="${GITEA_TOKEN:-}"
export GITEA_REPO_OWNER="${GITEA_REPO_OWNER:-pandoramannli}"
export GITEA_REPO_NAME="${GITEA_REPO_NAME:-gitops}"
export GITEA_REPO_URL="${GITEA_REPO_URL:-http://192.168.1.121:3000/pandoramannli/gitops.git}"
export GITEA_SSH_URL="${GITEA_SSH_URL:-ssh://git@192.168.1.121:2222/pandoramannli/gitops.git}"
export GIT_USER_NAME="${GIT_USER_NAME:-Admin}"
export GIT_USER_EMAIL="${GIT_USER_EMAIL:-admin@hc-stack.local}"
# Override with .env values if present
set -a
source <(grep -E "^GITEA_|^GIT_USER_" "$env_file" 2>/dev/null | grep -v "^#" || true)
set +a
fi
}
# Get Git remote URL with credentials
get_git_remote_with_auth() {
load_git_credentials
if [ -n "${GITEA_TOKEN:-}" ]; then
# Use token authentication (preferred)
echo "http://oauth2:${GITEA_TOKEN}@${GITEA_URL#http://}/$(echo ${GITEA_REPO_URL} | sed 's|.*://[^/]*/||')"
else
# Use username/password authentication
echo "http://${GITEA_USERNAME}:${GITEA_PASSWORD}@${GITEA_URL#http://}/$(echo ${GITEA_REPO_URL} | sed 's|.*://[^/]*/||')"
fi
}
# Configure Git with credentials
configure_git_credentials() {
load_git_credentials
git config user.name "${GIT_USER_NAME}"
git config user.email "${GIT_USER_EMAIL}"
# Set up credential helper for this repo
local repo_url="${GITEA_REPO_URL}"
if [[ "$repo_url" == http* ]]; then
if [ -n "${GITEA_TOKEN:-}" ]; then
# Use token authentication (preferred)
git remote set-url origin "http://oauth2:${GITEA_TOKEN}@${repo_url#http://}"
else
# Use username/password authentication
git remote set-url origin "http://${GITEA_USERNAME}:${GITEA_PASSWORD}@${repo_url#http://}"
fi
fi
}
# Push to Gitea repository
push_to_gitea() {
local repo_path="${1:-.}"
local branch="${2:-main}"
load_git_credentials
configure_git_credentials
cd "$repo_path"
git add -A
git commit -m "${3:-Update GitOps manifests}" || true
git push origin "$branch" 2>&1
}