如何批量clone gitlab group下的项目?

5,263 阅读3分钟

一、背景

由于公司的项目比较多,同一业务下的项目我们团队都已经放在了 gitlab 的同一个组下面。一般这些项目每个开发人员都要下载下来,因为有时候需要维护一些小功能,或者做新功能的时候参考之前的一些功能。但是第一次 20 多个项目都要手动下载下来,比较麻烦,所以想通过一个脚本能将 gitlab 某个 group 下的项目批量下载下到一个文件夹下。

二、获取 group 下项目的下载 url

访问 gitlab 的项目,打开控制台,会发现一个 children.json 的接口请求,将这下面的数据拷贝出来,然后贴到下面的代码中,然后运行 html,打开控制台的 console,然后将这个 group 下的 url 全部拷贝出来。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <script>
      // 从浏览器接口拷贝过来的json数据
      const groupArray = [
        {
          id: 5511,
          name: "test",
          description: "测试服务",
          visibility: "private",
          full_name: "TEST-UI / test",
          created_at: "2023-05-29T19:59:39.920+08:00",
          updated_at: "2023-07-20T14:17:40.617+08:00",
          avatar_url: null,
          type: "project",
          can_edit: false,
          edit_path: "/test-ui/base/edit",
          relative_path: "/test-ui/test",
          permission: null,
          last_activity_at: "2023-07-20T14:17:40.617+08:00",
          star_count: 0,
          archived: false,
          markdown_description:
            '<p data-sourcepos="1:1-1:12" dir="auto">测试服务</p>',
        },
        {
          id: 5514,
          name: "test2",
          description: "测试服务2",
          visibility: "private",
          full_name: "TEST-UI / test2",
          created_at: "2023-07-18T09:53:24.184+08:00",
          updated_at: "2023-07-19T18:27:10.477+08:00",
          avatar_url: null,
          type: "project",
          can_edit: false,
          edit_path: "/test-ui/test2/edit",
          relative_path: "/test-ui/test2",
          permission: null,
          last_activity_at: "2023-07-19T18:27:10.477+08:00",
          star_count: 0,
          archived: false,
          markdown_description:
            '<p data-sourcepos="1:1-1:10" dir="auto">测试服务2</p>',
        },
      ];
      const groupUrl = [];
      groupArray.forEach((item) => {
        // 这个git地址替换成你实际的地址
        groupUrl.push("https://git.test.com" + item.relative_path + ".git");
      });
      console.log(groupUrl);
    </script>
  </body>
</html>

三、编写 clone 的脚本

#!/bin/bash

# 定义一个包含多个仓库URL的列表
repos=(
    "https://git.test.com/test-ui/test.git"
    "https://git.test.com/test-ui/test2.git"
)

# 遍历数组中的每个URL,并执行 git clone
for repo in "${repos[@]}"
do
  git clone "$repo"
done

四、执行脚本

先给脚本添加权限:

chmod +x your_name.sh

然后在你要下载的文件夹下面执行脚本,路径可以自己定义,可以使用全路径,也可以使用相对路径。

./your.sh

这样就能一次性将一个gitlab group下的项目全部clone下来了,等脚本自己跑就行,你可以去喝杯咖啡或在群里吹吹水了,等你摸完,所有的项目都已经下载好了。

五、扩展

既然脚本可以进行项目的批量clone,当然也可以做别的,例如你这些项目都有某个分支,你可以批量切换到这个分支,并执行安装依赖、启动、打包等操作,这样就能一次性将所有项目都跑起来或打包好了,这样也能节省很多时间,你又可以喝咖啡和吹水了,所以说脚本真是个好东西。下面是批量切换分支,安装依赖,打包的脚本:

#!/bin/bash

# 指定你想切换到的分支名
target_branch="feature-test"

# 遍历当前目录下的所有文件和文件夹
for item in ./*; do
  # 如果当前项是一个目录,并且该目录下有一个 .git 文件或文件夹(即,它是一个 git 仓库)
  if [ -d "$item" ] && [ -d "$item/.git" ]; then
    # 进入该目录
    cd "$item"
    # 切换到目标分支
    git checkout "$target_branch"
    # 拉取最新的更改
    git pull
    # 执行 yarn 命令
    yarn
    # 执行build命令
    yarn build
    # 切回到原始目录,以便处理下一个仓库
    cd ..
  fi
done