自动拉取 Gitlab 的所有仓库
每次重装电脑或者一个新的gitlab项目需要拉取,就需要拉仓库
思路
- 获取到自己有权限的所有分组
- 获取到分组下的仓库列表
- 自动创建二级目录并 使用 ssh clone 仓库
使用 gitlab 提供的 rest 接口
- api/v4/groups // 有权限的分组
curl --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" '${GITLAB_HOST}/api/v4/groups?owned=1&per_page=500&page=1' | jq -r '.[] | (.id|tostring) + " " + .path + " "+ .web_url'
- api/v4/groups/{proups_id}/projects // 指定分组下的项目列表
curl --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" '${GITLAB_HOST}/api/v4/groups/${group_id}/projects?per_page=500&page=1' | jq '.[].ssh_url_to_repo' | xargs -I{} git clone {}
#!/bin/env bash
GITLAB_TOKEN=""
GITLAB_HOST=""
# 获取所有有权限的分组,并打印出 id, 路径 ,url 地址, 描述信息
curl --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "${GITLAB_HOST}/api/v4/groups?per_page=500&page=1" | jq -r '.[] | (.id|tostring) + " " + .path + " "+ .web_url' > groups
cat groups | while read group;
do
group_id=`echo $group | cut -d " " -f 1`
group_path=`echo $group | cut -d " " -f 2`
echo $group_id, $group_path
if [ ! -d $group_path ]; then
mkdir $group_path
fi
echo "clone ${group_path} ....................."
sleep 2
cd $group_path
# 循环获取分组下所有的仓库
page=1
while [ true ];
do
projects=`curl --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" "${GITLAB_HOST}/api/v4/groups/${group_id}/projects?per_page=500&page=${page}&owned=true" | jq '.[].ssh_url_to_repo'`
if [ -z "${projects}" ]; then
break
fi
for project_url in $projects; do
echo git clone "$project_url"
git clone "$project_url"
done
page=$((page + 1))
done
cd -
done
标签:Gitlab,group,仓库,GITLAB,拉取,TOKEN,groups,id,page
From: https://www.cnblogs.com/sooooooul/p/17455510.html