Skip to content

Instantly share code, notes, and snippets.

@rahulkj
Created July 22, 2021 21:22
Show Gist options
  • Save rahulkj/e315223ce11694e3b5c404a05f26defc to your computer and use it in GitHub Desktop.
Save rahulkj/e315223ce11694e3b5c404a05f26defc to your computer and use it in GitHub Desktop.
Snippet to clone repos under a group in gitlab
#!/bin/bash -ex
GITLAB_URL=https://gitlab.<hello>.com/
GITLAB_TOKEN=
if [ -z "$GITLAB_URL" ]; then
echo "Missing environment variable: GITLAB_URL (e.g. https://gitlab.com)"
exit 1
fi
if [ -z "$GITLAB_TOKEN" ]; then
echo "Missing environment variable: GITLAB_TOKEN"
echo "See ${GITLAB_URL}profile/account."
exit 1
fi
if [ -z "$1" ]; then
echo "Enter the groupID from gitlab here"
exit 1
fi
if [ -z "$2" ]; then
echo "Enter the directory name to clone the repos into"
exit 1
fi
if [ -z "$3" ]; then
echo "Enter ssh or http for cloning mechanism"
exit 1
fi
if [[ "${3}" == "ssh" ]]; then
REPO_URLS=($(curl -s "$GITLAB_URL/api/v4/groups/$1/projects?private_token=$GITLAB_TOKEN&per_page=999" | jq -r '.[] | .ssh_url_to_repo'))
elif [[ "${3}" == "http" ]]; then
REPO_URLS=($(curl -s "$GITLAB_URL/api/v4/groups/$1/projects?private_token=$GITLAB_TOKEN&per_page=999" | jq -r '.[] | .http_url_to_repo'))
fi
if [ ! -d ${2} ]; then
mkdir -p $2
fi
pushd $2
for repo in "${REPO_URLS[@]}"; do
git clone $repo
echo "Repo: $repo"
done
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment