gitlab:使用脚本批量下载项目,实现全项目检索

目的

当需要知道gitlab中所有项目是否存在某段代码时,gitlab免费版只提供了当个项目内的检索,当项目过多时一个个查太过繁琐。下面通过 GitLab API 将指定 Group 下的所有项目克隆到本地。此脚本会自动获取项目列表并逐一克隆它们,再在本地进行检索的方案。

准备工作

当前环境支持一下命令

  • curl:用于调用 GitLab API。

  • jq:解析 JSON 数据。
    *

    bash 复制代码
    # MAC 安装
    brew install jq
  • git:用于克隆项目。

gitlab上生成一个token令牌

  • read_api和read_repository权限

下载项目

sh脚本,替换参数

bash 复制代码
# 创建脚本文件
vim cloneGitlab.sh
bash 复制代码
#!/bin/bash

# 配置部分
GITLAB_URL="http://gitlab.example.com" # 替换为你的 GitLab 实例地址
GROUP_ID="12345"                        # 替换为你的 Group ID
PRIVATE_TOKEN="gitlab_token"      # 替换为你的 GitLab 私有访问令牌
CLONE_DIR="./gitlab-projects"           # 本地存储路径

# 创建存储目录
mkdir -p "$CLONE_DIR"

# 获取项目列表,100个
echo "Fetching project list from GitLab..."
PROJECTS=$(curl --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" \
  --silent \
  "$GITLAB_URL/api/v4/groups/$GROUP_ID/projects?per_page=100" | jq -r '.[].http_url_to_repo')

# 检查是否有项目返回
if [[ -z "$PROJECTS" ]]; then
  echo "No projects found or API request failed. Please check your configuration."
  exit 1
fi

# 克隆项目
echo "Cloning projects..."
cd "$CLONE_DIR" || exit

for PROJECT in $PROJECTS; do
  PROJECT_NAME=$(basename "$PROJECT" .git)
  
  if [[ -d "$PROJECT_NAME" ]]; then
    echo "Project $PROJECT_NAME already exists. Skipping..."
  else
    echo "Cloning $PROJECT..."
    git clone "$PROJECT"
  fi
done

echo "All projects cloned to $CLONE_DIR."
bash 复制代码
# 执行脚本
sh cloneGitlab.sh

检索内容

bash 复制代码
find . -name ".git" -execdir git --no-pager grep -n "your_key" \;
  • 显示检索的项目名称
bash 复制代码
find . -name ".git" -execdir sh -c '
    basename `pwd` && git --no-pager grep -n "bike_riding_order" 
' \;
相关推荐
HIT_Weston1 天前
61、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(五)
前端·ubuntu·gitlab
HIT_Weston1 天前
60、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(四)
前端·ubuntu·gitlab
周杰伦_Jay2 天前
【GOFrame】模块化框架与生产级实践
开发语言·gitlab·github
丫丫学AI2 天前
gitlab-runner注册执行器
gitlab
yuguo.im2 天前
5 分钟快速入门 Gitlab CI/CD
ci/cd·gitlab·github
aoxiang_ywj3 天前
GitLab 子模块(子仓)更新到主仓的完整流程
gitlab
HIT_Weston3 天前
57、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(一)
前端·ubuntu·gitlab
HIT_Weston3 天前
58、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(二)
前端·ubuntu·gitlab
HIT_Weston3 天前
59、【Ubuntu】【Gitlab】拉出内网 Web 服务:Gitlab 配置审视(三)
前端·ubuntu·gitlab
一念一花一世界4 天前
Arbess从基础到实践(5) - 集成GitLab+SonarQube搭建Java项目自动化部署
java·gitlab·sonarqube·cicd·arbess