Kiro CLI Headless 模式:在 CICD 里用 AI 自动 Review 代码
· 7 分钟阅读
Kiro CLI 能在 CICD 里跑吗?
能。Kiro CLI 2.0 引入了 headless 模式,专门为无人值守环境设计。核心命令:
kiro-cli chat --no-interactive --trust-all-tools "你的指令"
三个关键参数:
| 参数 | 作用 |
|---|---|
--no-interactive | 不等待用户输入 |
--trust-all-tools | 跳过所有工具确认提示 |
--agent <name> | 指定用哪个 agent |
CI 环境用 KIRO_API_KEY 认证,不需要浏览器登录。
怎么在 GitHub Actions 里集成?
完整的 PR 自动 review 配置:
# .github/workflows/kiro-review.yml
name: Kiro Code Review
on:
pull_request:
branches: [main]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Install Kiro CLI
run: curl -fsSL https://kiro.dev/install.sh | bash
- name: Run review
env:
KIRO_API_KEY: ${{ secrets.KIRO_API_KEY }}
run: |
CHANGED=$(git diff --name-only origin/main...HEAD | grep -E '\.(ts|js|py)$')
if [ -z "$CHANGED" ]; then exit 0; fi
kiro-cli chat --no-interactive --trust-all-tools \
"Review these files for bugs and security issues: $CHANGED"
还能做什么?
安全扫描
kiro-cli chat --no-interactive --trust-all-tools \
"Scan src/ for hardcoded secrets, SQL injection, and unvalidated input. Output JSON."
Changelog 生成
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "HEAD~20")
kiro-cli chat --no-interactive --trust-all-tools \
"Generate changelog from these commits: $(git log $LAST_TAG..HEAD --oneline)"
批量加文档注释
find src -name "*.ts" -not -name "*.test.*" | while read file; do
kiro-cli chat --no-interactive --trust-all-tools \
"Add JSDoc to exported functions in $file that are missing it."
done
怎么创建 CI 专用 Agent?
创建 .kiro/agents/ci-reviewer.json:
{
"name": "ci-reviewer",
"description": "CI code reviewer - strict, concise",
"prompt": "You are a strict code reviewer. Focus on bugs, security, and performance. Be concise. Output markdown.",
"tools": ["fs_read", "grep", "glob", "code"],
"allowedTools": ["fs_read", "grep", "glob", "code"]
}
使用:
kiro-cli chat --no-interactive --trust-all-tools --agent ci-reviewer "Review src/"
Agent 文件提交到 git,团队统一使用同一个 reviewer 规则。
注意事项
- API Key 不要写死在代码里。 用 GitHub Secrets 或 CI 变量传入。
- 输出可能很长。 用
| tee report.md保存到文件,方便后续处理。 - 控制成本。 只对变更文件跑,不要每次全仓库扫描。
--require-mcp-startup加上这个参数后,MCP 服务启动失败会直接报错退出,不会静默跳过。