在 GitHub Actions 中使用 Repomix 自动化代码库打包:完整实践指南
【免费下载链接】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 是用于将整个代码库打包成单一、对 LLM 友好的文件的工具,而 GitHub Actions 则能把这个打包过程变成 CI 流水线中的自动化环节。本文以 Repomix 官方 GitHub Actions 复合操作为核心,讲解如何在工作流中一键生成repomix-output.xml,覆盖输出格式切换、目录筛选、智能压缩、产物上传与全部输入参数,并结合仓库内的 Action 定义源码与 CLI 实现,深入解释每个参数在底层是如何被解析和执行的。
为什么要在 CI 中集成 Repomix
当仓库规模变大后,把整个代码库喂给 Claude、ChatGPT、Gemini 等 LLM 工具前,往往需要先手动运行repomix命令生成打包文件。将这一步放进 GitHub Actions 的好处非常直接:
- AI 分析自动化:每次代码变更后自动生成最新、可复现的代码库快照;
- CI 集成:把打包产物作为后续步骤(如代码审查、文档生成)的输入;
- 产物留存:通过 Artifact 机制让团队成员随时下载打包文件;
- 代码审查辅助:为 PR 场景生成"当前分支代码 vs 主分支代码"的可读摘要,辅助人工审查。
在 Repomix 仓库中,这个 Action 本身就是一个成熟的复合 Action 实现,官方也用它对自己的仓库做持续打包(见 .github/workflows/pack-repository.yml)。
基本使用:一条 step 完成打包
在 workflow YAML 中新增一个 step 即可把仓库打包为 XML 文件:
- name: Pack repository with Repomix uses: yamadashy/repomix/.github/actions/repomix@main with: output: repomix-output.xmluses指向 Repomix 仓库内的复合 Action(composite action),@main表示跟踪主分支最新版本,正式使用时建议锁定到具体 tag 以提升可复现性;output指定打包文件的相对路径,默认值就是repomix-output.xml,因此即使不写该参数也能工作。
Action 内部会依次执行:通过actions/setup-node配置 Node.js(默认版本 24)→ 若工作区存在package.json则先执行npm install(确保repomix.config.ts可以导入本地源码)→ 全局安装repomix@<repomix-version>→ 运行repomix并组装参数。这些步骤都定义在 .github/actions/repomix/action.yml 中。
切换输出格式:style 参数
style参数决定输出格式,默认值为xml,可选值为xml、markdown、json、plain。生成 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: json默认输出文件名与格式是配套的:在 src/config/configSchema.ts 的defaultFilePathMap中,xml → repomix-output.xml、markdown → repomix-output.md、plain → repomix-output.txt、json → repomix-output.json,每种风格都有对应的默认文件名。从源码可见style在 CLI 解析时会被统一转为小写后写入配置(见 src/cli/actions/defaultAction.ts 中的buildCliConfig),因此大小写不敏感。
多目录打包、include/ignore 与智能压缩
一次打包可以覆盖多个目录,并通过 glob 模式精确控制包含与排除范围,还能开启"智能压缩"(smart compression)大幅减小输出体积:
- 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.txt compress: true参数含义与约束:
directories:空格分隔的目录列表,不填时默认打包整个仓库(.);include:逗号分隔的包含 glob 模式,例如**/*.ts只保留 TypeScript 文件;传入后对应 CLI 的--include;ignore:逗号分隔的排除 glob 模式,例如排除全部测试文件**/*.test.ts;对应 CLI 的--ignore;compress:布尔值,是否启用智能压缩,对应 CLI 的--compress。
从 .github/actions/repomix/action.yml 的实现看,directories会被IFS=' ' read -r -a ARGS按空格安全拆分成数组,include/ignore只在非空时才追加--include/--ignore参数,compress仅在值为true时追加--compress,最终以repomix "${ARGS[@]}"的方式执行。这种"仅非空才传参"的策略保证了 Action 与 CLI 参数语义完全一致。
提示:在配置层面,include/ignore最终会进入repomix.config.json中的include数组与ignore.customPatterns数组(示例见仓库根目录的 repomix.config.json),因此 Action 参数与配置文件可以互相配合、按需覆盖。
将打包产物上传为 Artifact
打包文件默认落在工作区,想要供后续步骤使用或供人下载,可以配合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由于该 Action 是复合 Action,打包后的文件就留在工作区中,后续任意 step 都可以直接通过path引用;upload-artifact则把文件存入 Artifact,可设置retention-days控制保留天数。
Action 输入参数一览
下表完整列出 Action 的全部输入(以 .github/actions/repomix/action.yml 为准):
| 名称 | 说明 | 默认值 |
|---|---|---|
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 版本 | 24 |
值得留意两点:
compress的默认值:Action 层默认开启压缩(true),而 CLI 层默认compress为false(见 src/config/configSchema.ts 的repomixConfigDefaultSchema)。原因在于 Action 的定位是"尽量产出小文件",与命令行默认行为并不矛盾,使用时按需显式声明即可;additional-args是万能扩展口:任何 CLI 参数(如--no-file-summary、--include-empty-directories、--top-files-len 3等)都可以通过它原样透传。Action 内部会按空格拆分后追加到参数数组末尾,例如additional-args: "--no-file-summary"最终执行repomix ... --no-file-summary。
Action 输出
Action 对外暴露一个输出变量,便于后续 step 引用:
| 名称 | 说明 |
|---|---|
output_file | 生成的输出文件路径 |
该值由 Action 在repomix执行结束后通过echo "output_file=${INPUT_OUTPUT}" >> "$GITHUB_OUTPUT"写入,供后续 step 用steps.<step-id>.outputs.output_file读取。
完整工作流示例
一个同时覆盖手动触发、push 与 PR 场景的完整工作流如下(源自 .github/workflows/pack-repository.yml):
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/test-action.yml 展示了对该 Action 做矩阵测试的方式——在 Node.js 22/24/26 三种版本下分别以"最小参数(仅 output)""基础参数(directories + include + compress)""完整参数(多目录 + include/ignore + additional-args)"三档调用 Action,并上传结果。这个工作流本身就是很好的参考模板:你可以照搬它的矩阵结构,在多个 Node 版本上验证自己的打包配置。
底层原理:Action 参数如何变成 CLI 参数
把 Action 的输入映射到repomix命令,链路非常清晰:
- Action 声明层(.github/actions/repomix/action.yml):
inputs定义全部入参及默认值,runs.steps定义执行逻辑; - 参数组装层:复合 Action 内的 bash 脚本把
INPUT_DIRECTORIES、INPUT_INCLUDE、INPUT_IGNORE、INPUT_COMPRESS、INPUT_STYLE、INPUT_OUTPUT、INPUT_ADDITIONAL_ARGS按规则拼成参数数组,然后执行repomix "${ARGS[@]}"; - CLI 解析层(src/cli/actions/defaultAction.ts):
buildCliConfig将--include、--ignore、--style、--output、--compress等选项映射进 Repomix 配置对象,其中--include/--ignore通过splitPatterns拆分成数组,--style转为小写后写入output.style; - 配置合并层:最终配置由"默认配置 + 配置文件 + CLI 配置"三级合并而成(
mergeConfigs),CLI 参数优先级最高。
这也解释了为什么 Action 中出现的参数名(include、ignore、style、compress)与repomix命令行参数保持一致——它们走的是同一条配置通路,你在本地 CLI 上验证过的任何组合,都可以原样搬进 Action 的with:里。
小结
通过 Repomix 官方 GitHub Actions,你可以把"为 LLM 打包代码库"这一重复劳动完全自动化:style控制输出格式、directories/include/ignore精确圈定打包范围、compress压缩体积、additional-args透传任意 CLI 选项、output_file与upload-artifact打通产物消费链路。整套配置以 .github/actions/repomix/action.yml 为事实依据,配合 .github/workflows/pack-repository.yml 与 .github/workflows/test-action.yml 两个真实工作流,即可快速在自己的 CI 中落地。
【免费下载链接】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),仅供参考