在 GitHub Actions 中集成 Repomix:自动化打包代码库供 AI 分析的完整指南
【免费下载链接】repomix📦 Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix
将 Repomix 集成进 GitHub Actions 工作流,可以在每次 push、pull request 或手动触发时自动把整个代码库打包成单一、AI 友好的文件,为持续集成(CI)、代码审查以及为 LLM 工具准备上下文提供稳定输入。本文以官方文档为核心,结合 Repomix 仓库中 Action 的源码实现(.github/actions/repomix/action.yml),完整讲解 Action 的全部输入参数、底层执行原理与可落地的完整工作流示例,读完即可在项目中直接使用。
基本用法
把下面这段 step 添加到你的工作流 YAML 文件中,即可完成对仓库的打包:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.xml该 step 会检出代码后运行 Repomix,默认生成repomix-output.xml文件。如果你只需要最小化配置,这一个 step 就足够了——其余参数都有合理的默认值(详见下文输入参数表)。
内部发生了什么
从 action.yml 的runs定义可以看到,这是一个composite action,底层实际执行了三个连续步骤:
- Setup Node.js:使用
actions/setup-node按node-version输入(默认24)配置 Node 环境; - Install project dependencies:如果目标仓库存在
package.json,会先执行npm install,注释明确说明这是为了"确保repomix.config.ts可以 import 本地源码"——即项目自身依赖 Repomix 或本地配置文件时也能正常工作; - Install Repomix:执行
npm install --global "repomix@${REPOMIX_VERSION}",按repomix-version输入(默认latest)全局安装指定版本的 Repomix 包; - Run Repomix:将各个输入映射为 CLI 参数并执行
repomix命令,最后把输出文件路径写入$GITHUB_OUTPUT(详见下文"输出"部分)。
因此,该 Action 无需任何 Node.js 项目依赖即可独立运行,它通过npm全局安装 Repomix 后再调用 CLI,与你本地执行npx repomix的效果一致。
使用不同输出格式
通过style参数可以指定输出格式,默认值为xml。例如生成 Markdown 格式:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.md style: markdown或生成 JSON 格式:
- name: Pack repository with Repomix (JSON format) uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.json style: jsonstyle可选值包括xml、markdown、json、plain。在源码侧,defaultAction.ts 会对传入的 style 做toLowerCase()归一化处理后映射为输出样式;而四种风格的生成器分别实现在 src/core/output/outputStyles/ 目录下(markdownStyle.ts、plainStyle.ts、xmlStyle.ts),JSON 风格则由 jsonStyle.test.ts 等测试用例覆盖验证。
打包多个目录并开启智能压缩
通过directories可以打包多个目录,配合include/ignore精细控制文件范围,并用compress开启智能压缩:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: directories: src tests include: "**/*.ts,**/*.md" ignore: "**/*.test.ts" output: repomix-output.xml compress: true参数说明:
directories:以空格分隔的目录列表(Action 的 shell 实现用IFS=' '切分,见 action.yml),默认值为.(当前仓库根目录);include:逗号分隔的 glob 包含模式,只打包匹配的文件,映射为 CLI 的--include参数;ignore:逗号分隔的 glob 排除模式,映射为 CLI 的--ignore参数。该模式与.gitignore等内置规则叠加生效——CLI 默认会应用node_modules、.git、构建目录等内置忽略模式(见 cliRun.ts 中的--no-default-patterns等选项说明);compress:布尔值,默认true,等价于 CLI 的--compress,会智能删除注释和多余空白以大幅压缩输出体积。在 defaultAction.ts 中可以看到compress被映射到输出配置的compress字段。
将输出文件作为 Artifact 上传
为了让打包产物可供后续 step 使用或供人下载,可以配合actions/upload-artifact上传:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: directories: src output: repomix-output.xml compress: true - name: Upload Repomix output uses: actions/upload-artifact@v7 with: name: repomix-output path: repomix-output.xml上传后,该产物会在 GitHub Actions 运行结果页面中提供下载,也可供后续 job 通过actions/download-artifact拉取使用(例如将打包文件交给下游的 AI 审查、文档生成等步骤)。
Action 输入参数
| 名称 | 描述 | 默认值 |
|---|---|---|
directories | 要打包的目录(空格分隔) | . |
include | 要包含的 glob 模式(逗号分隔) | "" |
ignore | 要排除的 glob 模式(逗号分隔) | "" |
output | 输出文件路径 | repomix-output.xml |
style | 输出风格(xml、markdown、json、plain) | xml |
compress | 启用智能压缩 | true |
additional-args | 透传给 repomix CLI 的额外原始参数 | "" |
repomix-version | 要安装的 npm 包版本 | latest |
node-version | 使用的 Node.js 版本(补充说明,见 action.yml) | 24 |
其中两个参数值得展开说明:
additional-args:提供了一条直通 CLI 的逃生通道,可以传入上文表格之外的所有 Repomix 参数。例如测试工作流 test-action.yml 中就通过additional-args: "--no-file-summary"关闭了输出中的文件摘要章节。底层实现(action.yml)会先用IFS=' '切分这些额外参数再追加到命令数组中执行。仓库内部工作流 pack-repository.yml 本身也是通过该 Action 打包自身代码库,作为"吃自己的狗粮"的验证。可透传的常用参数还包括--no-security-check、--remove-comments、--token-budget <number>(超出 token 上限即非零退出,适合作为 CI 守卫)等,完整清单见 cliRun.ts;repomix-version:固定 Repomix 版本可以保证 CI 构建的可复现性,建议生产环境指定具体版本号(如1.18.0)而非latest。
Action 输出
| 名称 | 描述 |
|---|---|
output_file | 生成的输出文件的路径 |
该输出来自内部buildstep 的echo "output_file=${INPUT_OUTPUT}" >> "$GITHUB_OUTPUT"(见 action.yml)。在后续 step 中可以通过${{ steps.pack.outputs.output_file }}引用它,从而无需硬编码文件名:
- name: Pack repository with Repomix id: pack uses: yamadashy/repomix/.github/actions/repomix@main with: output: my-output.xml - name: Upload Repomix output uses: actions/upload-artifact@v7 with: name: repomix-output path: ${{ steps.pack.outputs.output_file }}完整工作流示例
下面是一个完整的 GitHub Actions 工作流,覆盖手动触发、push 和 pull_request 三种场景,并在打包后上传产物且保留 30 天:
name: Pack repository with Repomix on: workflow_dispatch: push: branches: [ main ] pull_request: branches: [ main ] jobs: pack-repo: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v7 - name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.xml - name: Upload Repomix output uses: actions/upload-artifact@v7 with: name: repomix-output.xml path: repomix-output.xml retention-days: 30仓库自身的实际示例位于 .github/workflows/pack-repository.yml,其结构与此一致,且额外配置了最小权限permissions: contents: read,并在 checkout 时设置persist-credentials: false——这两项都是值得借鉴的 CI 安全最佳实践。
进阶:多版本矩阵测试与实战建议
仓库中的 test-action.yml 展示了更完整的用法:它通过strategy.matrix在 Node.js 22/24/26 三个版本上分别以"最小配置、基础配置、完整配置"三种组合运行该 Action,完整配置示例同时用到了directories、include、ignore、compress与additional-args,可作为编写复杂场景工作流的参考模板。
几点实战建议:
- 固定版本:将
repomix-version与上游@main固定为具体 tag(如v1.18.0),避免打包行为随上游更新而变化; - 控制范围:仓库体积较大时优先用
directories+include缩小打包范围,或开启compress: true压缩输出; - 配合 token 预算:通过
additional-args传入--token-budget <number>,当打包结果超出 LLM 上下文窗口时让 CI 直接失败,作为上下文超限的自动化防线; - 安全审查:保持默认的 security check(对应 CLI 的敏感信息扫描),不要轻易通过
additional-args传入--no-security-check。
小结
Repomix GitHub Action 将本地 CLI 的完整能力封装成了声明式的 YAML 输入,核心实现不过百行(.github/actions/repomix/action.yml):安装 Node → 安装 Repomix → 将输入映射为 CLI 参数执行。理解这条调用链后,你既可以按表格参数快速接入 CI,也能通过additional-args灵活透传 cliRun.ts 中定义的全部 CLI 选项,让每次提交都自动产出可供 Claude、ChatGPT 等 LLM 直接消费的代码库快照。
【免费下载链接】repomix📦 Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考