Skip to content

Instantly share code, notes, and snippets.

@cl4u2
Forked from JonasGroeger/sync-projects
Last active September 27, 2018 14:49
Show Gist options
  • Save cl4u2/4b9f6cbe006d6167895ba44f1537ebc8 to your computer and use it in GitHub Desktop.
Save cl4u2/4b9f6cbe006d6167895ba44f1537ebc8 to your computer and use it in GitHub Desktop.
Gitlab: Clone / Pull all projects in a group
#!/usr/bin/env bash
# Documentation
# https://docs.gitlab.com/ce/api/projects.html#list-projects
BASE_PATH="https://gitlab.example.com/"
PROJECT_SEARCH_PARAM=""
PROJECT_SELECTION="."
PROJECT_PROJECTION="{ "path": .path_with_namespace, "git": .ssh_url_to_repo, "namespace": .namespace.full_path }
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"
trap "{ rm -f $FILENAME; }" EXIT
curl -s "${BASE_PATH}api/v3/projects?private_token=$GITLAB_PRIVATE_TOKEN&search=$PROJECT_SEARCH_PARAM&per_page=999" \
| jq --raw-output --compact-output ".[] | $PROJECT_SELECTION | $PROJECT_PROJECTION" > "$FILENAME"
while read repo; do
THEPATH=$(echo "$repo" | jq -r ".path")
THEDIR=$(echo "$repo" | jq -r ".namespace")
GIT=$(echo "$repo" | jq -r ".git")
if [ ! -d "${THEPATH}.git" ]; then
echo "Cloning $THEPATH ( $GIT )"
mkdir -p "$THEDIR"
git -C "$THEDIR" clone "$GIT"
mv "$THEPATH" "${THEPATH}.git"
else
echo "Pulling $THEPATH"
git -C "${THEPATH}.git" pull
fi
done < "$FILENAME"
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment