首页 sh

通过shell脚本clone组织下所有仓库

发布于: 2024-04-21

获取github组织下仓库

1
2
3
4
5
curl --silent \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN> \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/orgs/{orgs}/repos

下载代码

1
git clone --mirror git@github.com:{orgs}/{repos}.git

总结完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
orgs="<target-orgs>"
token="<YOUR-TOKEN>"
# JSON数据
repos_json=$(curl --silent \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${token}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/orgs/${orgs}/repos)
# 使用jq将JSON数组转换为行
echo "${repos_json}" | jq -r '.[] | @json' | while IFS= read -r line; do
# 处理每一行(也就是每个JSON对象)
# 解析JSON对象以获取ssh地址和name
ssh_url=$(echo "$line" | jq -r '.ssh_url')
name=$(echo "$line" | jq -r '.name')
dir_pat="${orgs}/${name}"
echo "正在下载 ${ssh_url} 到 ${dir_pat}"
git clone --mirror ${ssh_url} ${dir_pat}
echo "下载完成 ${ssh_url}"
done