Skip to content

Instantly share code, notes, and snippets.

@k7faq
Created March 7, 2019 16:29
Show Gist options
  • Select an option

  • Save k7faq/ecc143b52b5a996c23be144f30db6a05 to your computer and use it in GitHub Desktop.

Select an option

Save k7faq/ecc143b52b5a996c23be144f30db6a05 to your computer and use it in GitHub Desktop.
Sync GitLab Repos (filtered by membership)
#!/usr/bin/env bash
# Documentation
# https://docs.gitlab.com/ce/api/projects.html#list-projects
#
# What does this script do?
# This script will clone ( or pull existing ) ALL of the repos you have access to in GitLab.
# This script does not pull other peoples/entities repos.
#
# INSTRUCTIONS FOR USE
# You will need to create a Private Token in your GitLab Account. MAKE SURE !!! to document your token
# you will not be able to "see it" again in GitLab!!!
# https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
#
# Before exuting this script you will need to "export" your Access Token in your environment
# export GITLAB_PRIVATE_TOKEN="<< Your access token >>"
#
# For convienence you may elect to add this to your ~/.bash_profile (in mac) so it is set when you login
BASE_PATH="https://gitlab.com/"
PROJECT_PROJECTION="{ "path": .path_with_namespace, "git": .ssh_url_to_repo }"
WD="${PWD}"
if [ -z "$GITLAB_PRIVATE_TOKEN" ]; then
echo "Please set the environment variable GITLAB_PRIVATE_TOKEN"
echo "See ${BASE_PATH}profile/account"
exit 1
fi
FILENAME="repos.json"
curl -s "${BASE_PATH}api/v4/projects?private_token=$GITLAB_PRIVATE_TOKEN&membership=true&per_page=999" \
| jq --raw-output --compact-output ".[] | $PROJECT_PROJECTION" > "$FILENAME"
while read repo; do
THEPATH=$(echo "$repo" | jq -r ".path")
GIT=$(echo "$repo" | jq -r ".git")
cd "${WD}"
if [ ! -d "$THEPATH" ]; then
mkdir -p "$THEPATH"; cd $_
git clone "$GIT" . --quiet &
else
(cd "$THEPATH" && git pull --quiet) &
fi
done < "$FILENAME"
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment