Skip to content

Instantly share code, notes, and snippets.

@jamesbrink
Created January 3, 2019 16:13
Show Gist options
  • Save jamesbrink/cef48b7caab224778ebf9020e0a7ab49 to your computer and use it in GitHub Desktop.
Save jamesbrink/cef48b7caab224778ebf9020e0a7ab49 to your computer and use it in GitHub Desktop.
Pull all repos from private gitlab server
#!/usr/bin/env bash
BASE_PATH="https://yourgitlab.server/"
GROUP_OUTPUT="{ \"group_name\": .full_path, \"group_id\": .id }"
PROJECT_OUTPUT="{ \"project_name\": .path, \"project_url\": .ssh_url_to_repo }"
if [ -z "$GITLAB_PRIVATE_TOKEN" ]; then
echo "Please set the environment variable GITLAB_PRIVATE_TOKEN"
echo "See ${BASE_PATH}profile/account"
exit 1
fi
gitlab_groups=$(curl -s "${BASE_PATH}api/v4/groups?private_token=$GITLAB_PRIVATE_TOKEN&per_page=9999" | jq --raw-output --compact-output ".[] | $GROUP_OUTPUT")
for gitlab_group in $gitlab_groups
do
group_name=$(echo "$gitlab_group"| jq -r ".group_name")
group_id=$(echo "$gitlab_group"| jq -r ".group_id")
echo "Pulling projects from group: $group_name"
mkdir -p "$group_name"
projects=$(curl -s "${BASE_PATH}api/v4/groups/$group_id/projects?private_token=$GITLAB_PRIVATE_TOKEN&per_page=9999" | jq --raw-output --compact-output ".[] | $PROJECT_OUTPUT")
for project in $projects
do
project_name=$(echo "$project" | jq -r ".project_name")
project_url=$(echo "$project" | jq -r ".project_url")
echo -e "\tPulling repo: $project_name"
destination_path="$group_name/$project_name"
if [ -d "$destination_path" ];
then
echo -e "\t$destination_path already exists, fetching updates."
cd "$destination_path" || exit 1
git fetch --all
cd - || exit 1
else
until git clone "$project_url" "$destination_path" --mirror
do
echo -e "\tFailed to clone $project_name"
rm -rf "$destination_path"
sleep 5
done
if ! git -C "$destination_path" show-ref
then
rm -rf "$destination_path"
fi
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment