Skip to content

Instantly share code, notes, and snippets.

@Julienraptor01
Created November 25, 2024 08:01
Show Gist options
  • Select an option

  • Save Julienraptor01/3c73627d768e52bf1c7057ec4b41bdc0 to your computer and use it in GitHub Desktop.

Select an option

Save Julienraptor01/3c73627d768e52bf1c7057ec4b41bdc0 to your computer and use it in GitHub Desktop.
Clone/Update Repos from multiple GitLab Groups
#!/bin/bash
# Go To https://gitlab.com/-/user_settings/personal_access_tokens and create a token with the API permissions
TOKEN="PERSONAL_ACCESS_TOKEN"
GROUP_IDS=("GROUP_ID_1" "GROUP_ID_2")
BASE_API_URL="https://gitlab.com/api/v4/groups"
# Check if TOKEN is still set to the default value
if [[ "$TOKEN" == "PERSONAL_ACCESS_TOKEN" ]]; then
echo "ERROR: Please edit this script to set the Access Token"
exit 42069
fi
# Check if GROUP_IDS is still set to the default value
if [[ "${GROUP_IDS[*]}" == "GROUP_ID_1 GROUP_ID_2" ]]; then
echo "ERROR: Please edit this script to set the Group ID(s)"
exit 69420
fi
# Loop through each group ID
for GROUP_ID in "${GROUP_IDS[@]}"; do
API_URL="$BASE_API_URL/$GROUP_ID/projects"
GROUP_FOLDER="$GROUP_ID"
# Create folder for the group if it doesn't exist
if [[ ! -d "$GROUP_FOLDER" ]]; then
mkdir "$GROUP_FOLDER"
fi
cd "$GROUP_FOLDER"
# Fetch repositories list
echo "Fetching repositories from '$API_URL'..."
REPOS=$(curl --silent --header "Private-Token: $TOKEN" "$API_URL" | grep -oP '"http_url_to_repo":\s*"\K[^"]+')
# Clone or update repositories
for REPO in $REPOS; do
REPO_NAME=$(basename -s .git "$REPO")
if [[ -d "$REPO_NAME" ]]; then
cd "$REPO_NAME"
echo "Fetching updates for '$REPO_NAME'..."
git fetch
LOCAL=$(git rev-parse HEAD)
REMOTE=$(git rev-parse @{u})
if [[ "$LOCAL" == "$REMOTE" ]]; then
echo "'$REPO_NAME' is already up to date."
else
echo "Pulling updates for '$REPO_NAME'..."
git pull
fi
cd ..
else
echo "Cloning '$REPO_NAME'..."
git clone "$REPO"
fi
done
# Return to the base directory
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment