Created
April 9, 2025 12:14
-
-
Save p4block/41c642851a715586c12ce238650aeb86 to your computer and use it in GitHub Desktop.
Clones an entire GitLab instance. Useful to grep in ALL repos in a selfhosted non-premium free GitLab.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variables | |
GITLAB_URL="https://gitlab.com" | |
ACCESS_TOKEN="" | |
PAGE=1 | |
CLONE_DEPTH=1 | |
REPOS=() | |
# Loop through pages until no more results are returned | |
while : ; do | |
# Fetch a page of repositories | |
repos=$(curl --silent --header "PRIVATE-TOKEN: $ACCESS_TOKEN" "$GITLAB_URL/api/v4/projects?per_page=$PER_PAGE&page=$PAGE") | |
# If no repositories were returned, stop the loop | |
if [[ -z "$repos" || "$repos" == "[]" ]]; then | |
break | |
fi | |
# Extract and append the URLs of the repositories | |
REPOS+=($(echo "$repos" | jq -r '.[].http_url_to_repo')) | |
# Increment page number | |
PAGE=$((PAGE + 1)) | |
done | |
# Now REPOS contains all repository URLs | |
for repo in "${REPOS[@]}"; do | |
echo "Found repo: $repo" | |
repo_name=$(basename "$repo" .git) | |
# Clone using token as part of the password in the URL | |
git clone --depth=$CLONE_DEPTH "https://${repo#https://}" "$repo_name" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment