# Function to pull the latest changes from the Git repo pull_latest_from_git() { # Directory where the repo will be cloned REPO_DIR="/mnt/server" # Check if Git is installed if ! command -v git >/dev/null 2>&1; then echo "Git is not installed. Installing Git..." apk update apk add git fi # Configure Git to use the provided username and PAT if [ -n "${GIT_USERNAME}" ] && [ -n "${GIT_PAT}" ]; then echo "Configuring Git credentials and directory..." git config --global --add safe.directory "${REPO_DIR}" echo "The directory config worked" git config --global credential.helper store echo "https://${GIT_USERNAME}:${GIT_PAT}@${GIT_REPO_URL#https://}" > /root/.git-credentials chmod 600 /root/.git-credentials echo "The credentials config worked" fi cd "${REPO_DIR}" # Check if the .git directory exists if [ -d ".git" ]; then echo "Repository already exists. Pulling latest changes from ${GIT_BRANCH} branch..." git fetch origin git reset "origin/${GIT_BRANCH}" else echo "Cloning repository from ${GIT_REPO_URL}..." git clone https://${GIT_REPO_URL} ${REPO_DIR} fi # Clean up Git credentials rm -f /root/.git-credentials git config --global --unset credential.helper } # Pull the latest changes from Git pull_latest_from_git