Skip to content

Instantly share code, notes, and snippets.

@exceptionplayer
Forked from JonasGroeger/sync-projects
Last active March 24, 2023 02:42
Show Gist options
  • Save exceptionplayer/d0618bbea17e9d8e2b4cb1d6dd9f01e1 to your computer and use it in GitHub Desktop.
Save exceptionplayer/d0618bbea17e9d8e2b4cb1d6dd9f01e1 to your computer and use it in GitHub Desktop.
Gitlab: Clone / Pull all projects in a group in gitlab(批量下载gitlab中的项目)
#!/usr/bin/env bash
# should install jq first, for mac:brew install jq
# Documentation
# https://docs.gitlab.com/ce/api/projects.html#list-projects
NAMESPACE="your group name in gitlab"
BASE_PATH="https://gitlab.example.com/"
PROJECT_SEARCH_PARAM=""
PROJECT_SELECTION="select(.namespace.name == \"$NAMESPACE\")"
#below use git@xxx to git clone
#PROJECT_PROJECTION="{ "path": .path, "git": .ssh_url_to_repo }"
#below use http:// to git clone
PROJECT_PROJECTION="{ "path": .path, "git": .http_url_to_repo }"
GITLAB_PRIVATE_TOKEN="your_private_token_in_gitlab"
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"
# for debug
# curl -s "https://git.example.com/api/v3/projects?private_token=xxxxxxx" |jq --raw-output --compact-output ".[]|select(.namespace.name == \"live\")|{ \"path\": .path, \"git\": .http_url_to_repo}" > tmp
while read repo; do
THEPATH=$(echo "$repo" | jq -r ".path")
GIT=$(echo "$repo" | jq -r ".git")
if [ ! -d "$THEPATH" ]; then
echo "Cloning $THEPATH ( $GIT )"
git clone "$GIT" --quiet &
else
echo "Pulling $THEPATH"
(cd "$THEPATH" && git pull --quiet) &
fi
done < "$FILENAME"
wait
@leoshenliang
Copy link

thankyou!very goods!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment