mirror of
https://github.com/darksaid98/yolks.git
synced 2026-12-04 06:29:00 +00:00
fix(graalvm): allow switchin PAT
This commit is contained in:
1 file changed
+189
-47
+189
-47
@@ -33,69 +33,211 @@ export INTERNAL_IP
|
|||||||
# Switch to the container's working directory
|
# Switch to the container's working directory
|
||||||
cd /home/container || exit 1
|
cd /home/container || exit 1
|
||||||
|
|
||||||
|
CONTAINER_DIR="/home/container"
|
||||||
|
DEFAULT_BRANCH="main"
|
||||||
|
|
||||||
|
# Normalize repository URL
|
||||||
|
normalize_repo_url() {
|
||||||
|
local url="$1"
|
||||||
|
|
||||||
|
# Remove any existing credentials (user:token@)
|
||||||
|
url=$(echo "$url" | sed 's|https://[^@]*@|https://|')
|
||||||
|
|
||||||
|
# Add .git suffix if not present
|
||||||
|
if [[ "$url" != *.git ]]; then
|
||||||
|
url="${url}.git"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "$url"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Construct authenticated URL
|
||||||
|
construct_auth_url() {
|
||||||
|
local repo_url="$1"
|
||||||
|
local username="$2"
|
||||||
|
local token="$3"
|
||||||
|
|
||||||
|
# Remove https:// prefix
|
||||||
|
repo_url="${repo_url#https://}"
|
||||||
|
|
||||||
|
# Construct authenticated URL
|
||||||
|
echo "https://${username}:${token}@${repo_url}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Extract repository info for logging
|
||||||
|
get_repo_info() {
|
||||||
|
local url="$1"
|
||||||
|
local normalized_url=$(normalize_repo_url "$url")
|
||||||
|
|
||||||
|
# Extract owner/repo from URL
|
||||||
|
local repo_path=$(echo "$normalized_url" | sed 's|https://github.com/||' | sed 's|\.git$||')
|
||||||
|
echo "$repo_path"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if current directory is a git repository
|
||||||
|
is_git_repo() {
|
||||||
|
[ -d ".git" ] && [ -f ".git/config" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if repository URLs match (ignores credentials)
|
||||||
|
urls_match() {
|
||||||
|
local url1="$1"
|
||||||
|
local url2="$2"
|
||||||
|
|
||||||
|
local normalized1=$(normalize_repo_url "$url1")
|
||||||
|
local normalized2=$(normalize_repo_url "$url2")
|
||||||
|
|
||||||
|
[ "$normalized1" = "$normalized2" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
# update git remote URL with new credentials (needed for when a user switches PAT's)
|
||||||
|
update_remote_url() {
|
||||||
|
local new_url="$1"
|
||||||
|
echo "Updating remote URL with new credentials..."
|
||||||
|
git remote set-url origin "$new_url"
|
||||||
|
echo "Remote URL updated successfully"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Apply updates for custom repo
|
||||||
|
perform_git_update() {
|
||||||
|
local branch="$1"
|
||||||
|
local repo_info="$2"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Fetching latest changes from remote..."
|
||||||
|
|
||||||
|
# Check if we have a shallow clone
|
||||||
|
local is_shallow=$(git rev-parse --is-shallow-repository 2>/dev/null || echo "false")
|
||||||
|
|
||||||
|
# Fetch based on whether it's shallow or not
|
||||||
|
if [ "$is_shallow" = "true" ]; then
|
||||||
|
echo "Detected shallow repository, fetching with depth 1..."
|
||||||
|
git fetch --depth 1 origin "$branch"
|
||||||
|
else
|
||||||
|
echo "Fetching full history..."
|
||||||
|
git fetch origin "$branch"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Updating local files to match remote branch..."
|
||||||
|
git reset --hard "origin/$branch"
|
||||||
|
|
||||||
|
# Get latest commit info for logging
|
||||||
|
local latest_commit=$(git log -1 --oneline 2>/dev/null || echo "unknown")
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "✅ Successfully updated repository!"
|
||||||
|
echo "Repository: $repo_info"
|
||||||
|
echo "Branch: $branch"
|
||||||
|
echo "Latest commit: $latest_commit"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Clone custom repo
|
||||||
|
perform_git_clone() {
|
||||||
|
local repo_url="$1"
|
||||||
|
local branch="$2"
|
||||||
|
local repo_info="$3"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Cloning repository into container directory..."
|
||||||
|
|
||||||
|
if [ -z "$branch" ]; then
|
||||||
|
echo "Cloning default branch..."
|
||||||
|
git clone --single-branch --depth 1 "$repo_url" .
|
||||||
|
else
|
||||||
|
echo "Cloning branch: $branch"
|
||||||
|
git clone --single-branch --branch "$branch" --depth 1 "$repo_url" .
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get current branch and latest commit for logging
|
||||||
|
local current_branch=$(git branch --show-current 2>/dev/null || echo "unknown")
|
||||||
|
local latest_commit=$(git log -1 --oneline 2>/dev/null || echo "unknown")
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "✅ Successfully cloned repository!"
|
||||||
|
echo "Repository: $repo_info"
|
||||||
|
echo "Branch: $current_branch"
|
||||||
|
echo "Latest commit: $latest_commit"
|
||||||
|
}
|
||||||
|
|
||||||
# Auto update server from git.
|
# Auto update server from git.
|
||||||
if [ "${GIT_ENABLED}" == "true" ] || [ "${GIT_ENABLED}" == "1" ]; then
|
if [ "${GIT_ENABLED}" == "true" ] || [ "${GIT_ENABLED}" == "1" ]; then
|
||||||
|
echo "Git auto-update is enabled"
|
||||||
|
echo ""
|
||||||
|
|
||||||
# Pre git stuff
|
# Validate variables
|
||||||
echo "Wait, preparing to pull or clone from git."
|
if [ -z "${GIT_REPOURL}" ]; then
|
||||||
|
echo "❌ Error: GIT_REPOURL is not specified"
|
||||||
mkdir -p /home/container
|
exit 1
|
||||||
cd /home/container
|
|
||||||
|
|
||||||
# Git stuff
|
|
||||||
if [[ ${GIT_REPOURL} != *.git ]]; then # Add .git at end of URL
|
|
||||||
GIT_REPOURL=${GIT_REPOURL}.git
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -z "${GIT_USERNAME}" ] && [ -z "${GIT_TOKEN}" ]; then # Check for git username & token
|
if [ -z "${GIT_USERNAME}" ] || [ -z "${GIT_TOKEN}" ]; then
|
||||||
echo -e "git Username or git Token was not specified."
|
echo "❌ Error: GIT_USERNAME or GIT_TOKEN is not specified"
|
||||||
else
|
echo " Both are required for private repository access"
|
||||||
GIT_REPOURL="https://${GIT_USERNAME}:${GIT_TOKEN}@$(echo -e ${GIT_REPOURL} | cut -d/ -f3-)"
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$(ls -A /home/container)" ]; then # Files exist in server folder, pull
|
local normalized_repo_url=$(normalize_repo_url "$GIT_REPOURL")
|
||||||
echo -e "Files exist in /home/container/. Attempting to pull from git repository."
|
local auth_repo_url=$(construct_auth_url "$normalized_repo_url" "$GIT_USERNAME" "$GIT_TOKEN")
|
||||||
|
local branch="${GIT_BRANCH:-$DEFAULT_BRANCH}"
|
||||||
|
local repo_info=$(get_repo_info "$GIT_REPOURL")
|
||||||
|
|
||||||
# Get git origin from /home/container/.git/config
|
echo "Git Configuration:"
|
||||||
if [ -d .git ]; then
|
echo " Repository: $repo_info"
|
||||||
if [ -f .git/config ]; then
|
echo " Branch: $branch"
|
||||||
GIT_ORIGIN=$(git config --get remote.origin.url)
|
echo " Target directory: $CONTAINER_DIR"
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if we have a shallow clone and need to maintain it
|
# Check if directory has content
|
||||||
is_shallow=$(git rev-parse --is-shallow-repository 2>/dev/null || echo "false")
|
if [ "$(ls -A .)" ]; then
|
||||||
|
echo ""
|
||||||
|
echo "Container directory is not empty, checking for existing repository..."
|
||||||
|
|
||||||
# If git origin matches the repo specified by user then pull
|
if is_git_repo; then
|
||||||
if [ "${GIT_ORIGIN}" == "${GIT_REPOURL}" ]; then
|
echo "Found existing git repository"
|
||||||
# Override local changes
|
|
||||||
# echo "Overriding local changes..."
|
|
||||||
# git clean -fd
|
|
||||||
|
|
||||||
# Fetch latest changes (shallow if original clone was shallow)
|
# Get current remote URL
|
||||||
echo "Fetching latest changes..."
|
local 'current_remote'=$(git config --get remote.origin.url 2>/dev/null || echo "")
|
||||||
if [ "$is_shallow" = "true" ]; then
|
|
||||||
git fetch --depth 1 origin "${GIT_BRANCH}" && echo "Finished fetching /home/container/ from git." || echo "Failed fetching /home/container/ from git."
|
if [ -n "$current_remote" ]; then
|
||||||
|
echo "Current remote: $(normalize_repo_url "$current_remote" | sed 's|https://github.com/||' | sed 's|\.git$||')"
|
||||||
|
|
||||||
|
# Check if URLs match (ignoring credentials)
|
||||||
|
if urls_match "$current_remote" "$normalized_repo_url"; then
|
||||||
|
echo "Repository URLs match"
|
||||||
|
|
||||||
|
# Update remote URL with new credentials (handles PAT changes)
|
||||||
|
update_remote_url "$auth_repo_url"
|
||||||
|
|
||||||
|
# Perform update
|
||||||
|
perform_git_update "$branch" "$repo_info"
|
||||||
|
else
|
||||||
|
echo "❌ Repository mismatch!"
|
||||||
|
echo " Expected: $repo_info"
|
||||||
|
echo " Found: $(get_repo_info "$current_remote")"
|
||||||
|
echo ""
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
git fetch origin "${GIT_BRANCH}"
|
echo "Could not determine current remote URL"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Force reset to match remote branch (this will override any local edits)
|
|
||||||
echo "Updating to match remote branch..."
|
|
||||||
git reset --hard "origin/${GIT_BRANCH}" && echo "Finished updating /home/container/ from git." || echo "Failed updating /home/container/ from git."
|
|
||||||
else
|
else
|
||||||
echo -e "git repository in /home/container/ does not match user provided configuration. Failed pulling /home/container/ from git."
|
echo "Directory contains files but is not a git repository"
|
||||||
|
perform_git_clone "$auth_repo_url" "$branch" "$repo_info"
|
||||||
fi
|
fi
|
||||||
else # No files exist in server folder, clone
|
else
|
||||||
echo -e "Server directory is empty. Attempting to clone git repository."
|
echo ""
|
||||||
|
echo "Container directory is empty, performing initial clone..."
|
||||||
|
perform_git_clone "$auth_repo_url" "$branch" "$repo_info"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -z ${GIT_BRANCH} ]; then
|
echo ""
|
||||||
echo -e "Cloning default branch into /home/container/."
|
echo "✅ Git auto-update completed successfully!"
|
||||||
git clone --single-branch --depth 1 ${GIT_REPOURL} . && echo "Finished cloning into /home/container/ from git." || echo "Failed cloning into /home/container/ from git."
|
echo "Working directory: $(pwd)"
|
||||||
else
|
|
||||||
echo -e "Cloning ${GIT_BRANCH} branch into /home/container/."
|
# Final verification
|
||||||
git clone --single-branch --branch ${GIT_BRANCH} --depth 1 ${GIT_REPOURL} . && echo "Finished cloning into /home/container/ from git." || echo "Failed cloning into /home/container/ from git."
|
if is_git_repo; then
|
||||||
fi
|
local final_branch=$(git branch --show-current 2>/dev/null || echo "unknown")
|
||||||
|
echo "Current branch: $final_branch"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Post git stuff
|
# Post git stuff
|
||||||
|
|||||||
Reference in new issue
Block a user