用 n8n 自动审查 Terraform PR:GitHub + GPT-4.1 + Postgres + Slack
GitHub PR 触发 → AI Agent 调用工具(plan/安全扫描/成本估算/标准检索)→ 结构化评审 → Postgres 存档 + PR 评论 → 自动通过或 Slack 人工复核
- 给谁
- 需要为 Terraform 基础设施变更建立自动化代码审查与合规门禁的平台/安全工程师
- 你会得到
- PR 提交后自动触发多维度评审(计划差异、安全、成本、组织标准),结果写入数据库并评论到 PR;无风险自动批准,高风险转 Slack 人工确认
硬约束
前置条件:n8n 实例(建议自托管或企业版)、GitHub 仓库管理员权限(repo scope + PR 读写)、OpenAI API 密钥、Postgres 数据库、Slack 工作区管理员权限、可选的向量存储(用于 RAG 检索内部 IaC 标准)和 CI 端点(terraform plan / 安全扫描 / 成本估算)。费用取决于 OpenAI 模型调用量与 n8n 执行次数。
他们做了什么
oneclick-ai 在 n8n 上发布了一个工作流模板,用于自动审查 GitHub 上的 Terraform Pull Request。该模板通过 GitHub Webhook 触发,由 AI Agent 调用多个工具(terraform plan、安全扫描、成本估算、内部标准 RAG 检索)生成结构化评审,并将结果存档到 Postgres、评论到 PR,最后根据风险等级自动批准或转人工复核。
效果如何
来源为模板展示,作者未提供具体执行结果或 ROI 数据(author_report)。模板声称可通过 sessionId 保持同一 PR 多次推送的评审上下文。
值得知道
1. 工具节点中的 HTTP 端点使用示例地址(ci.internal.example.com),实际部署时必须替换为真实 CI 地址。2. terraform plan 工具明确配置为只读,禁止 agent 执行 apply。3. 成本估算工具建议对接 Infracost API 或 CLI 输出。4. Wait 节点需要外部 reviewer UI 调用 webhook 传入 securityApproved 值才能恢复执行。5. 向量存储需预先录入组织内部的 Terraform 风格指南、命名规范、模块标准等文档。6. 本站未实际运行该工作流(reproduction=not_tested)。
n8n 工作流模板
结构已检查 · 未实际运行下载后在 n8n 选择“Import from File”,或复制 JSON 后粘贴到编辑器。先配置自己的账号,再用测试数据运行;模板默认未启用。
配置与测试清单
需要绑定的凭证
- 导入后按各需鉴权节点绑定你自己的凭证;本附件不含原作者密钥。
需要替换的配置
- 源模板来自 n8n 公开库 #19175(约 24 个节点)。导入后先保持未启用,核对触发器、表达式与外部 API 端点。
- Sticky Note 中的说明仅供参考;按自己的账号、表结构或频道 ID 替换占位配置。
测试输入与预期结果
- 在测试环境手动执行一次主路径,确认每个需凭证的节点已连接且无鉴权错误。
- 用一条合成/脱敏样例数据跑通,核对输出字段与下游写入目标,再考虑生产启用。
模板处理说明
- 已移除原作者凭证绑定、webhookId 与执行数据,工作流设为未启用;节点、连线与原文提示词保持原样。未在 n8n 中实际运行。
- 未改业务逻辑;运行前请自行核对密钥、配额与隐私合规。
模板获取日期: 2026-10-24
步骤
配置 GitHub 触发器
在 n8n 中导入工作流模板,配置 GitHub Trigger 节点:选择目标仓库的 owner 和 repository,事件类型选择 pull_request。确保 GitHub 凭证具有 repo 权限和 PR 读写访问权。
提取 PR 元数据并获取差异
工作流触发后,Set - Config 节点从 webhook payload 提取 prNumber、repoOwner、repoName、branch、prAuthor、prTitle,并生成 sessionId(格式:owner/repo#prNumber)。随后 Get - PR Diff 节点调用 GitHub API 获取该 PR 的完整信息作为评审上下文。
运行 AI Agent 多工具评审
AI Agent - Terraform Reviewer 节点接收 PR 上下文,绑定 OpenAI Chat Model(gpt-4.1)和 Window Buffer Memory(sessionId 作为会话键,保留 20 轮上下文)。Agent 按需调用四个工具:run_terraform_plan(HTTP 工具,只读 plan,禁止 apply)、run_security_scan(静态安全扫描)、estimate_cost_impact(月度成本增量)、search_iac_standards(向量存储 RAG 检索组织规范)。Agent 必须基于工具返回结果撰写评审,禁止猜测。
运行提示词 · 来源原文。AI Agent - Terraform Reviewer 节点的 text 参数(promptType=define)
=You are a senior Platform/Security engineer reviewing a Terraform pull request before it can merge. Repo: {{ $json.repoOwner }}/{{ $json.repoName }} | Branch: {{ $json.branch }} PR #{{ $json.prNumber }} by {{ $json.prAuthor }}: "{{ $json.prTitle }}" You have four tools: run_terraform_plan (resource-level diff), run_security_scan (static security findings), estimate_cost_impact (monthly cost delta), search_iac_standards (org conventions/precedent). Use whichever combination is relevant — call multiple tools, and call them again with different parameters if needed — before writing your review. Do not guess plan output, scan findings, or cost numbers; only state what the tools returned. If earlier turns exist in this conversation (same PR), treat this as a re-review after a new push and note what changed since the prior review. Categorize findings under: security, cost, correctness (will the plan do what the PR claims), and standards (naming/tagging/module conventions). Assign each finding a severity ('low'|'medium'|'high'|'critical'). Set overallRisk to the highest severity present. List any 'critical' or 'high' security finding, or any plan output showing a destroy of a stateful resource (database, volume) as a blockingIssue requiring human sign-off. Return ONLY a JSON object (no markdown fences, no commentary) with keys: summary (2-4 sentences), findings (array of {category, description, severity}), overallRisk ('low'|'medium'|'high'|'critical'), verdict ('pass'|'flag_for_review'), blockingIssues (array of strings, empty if none), costImpactSummary (string).解析输出并发布评审结果
Set - Parse Agent Output 节点用 JSON.parse 提取 agent 返回的结构化数据。Postgres - Log PR Review 节点将评审记录插入 terraform_pr_reviews 表(字段:pr_number, repo, author, findings, overall_risk, verdict, blocking_issues, cost_impact_summary, created_at)。GitHub - Post Review Comment 节点将格式化评审评论发布到 PR。
运行提示词 · 来源原文。GitHub - Post Review Comment 节点的 body 参数
=### 🤖 Terraform Review — Verdict: {{ $json.parsedOutput.verdict.toUpperCase() }} (risk: {{ $json.parsedOutput.overallRisk }}) {{ $json.parsedOutput.summary }} **Findings** {{ $json.parsedOutput.findings.map(f => '- [' + f.severity + '] (' + f.category + ') ' + f.description).join('\n') }} **Cost impact:** {{ $json.parsedOutput.costImpactSummary }} {{ $json.parsedOutput.blockingIssues.length ? '**Blocking issues requiring human sign-off:**\n' + $json.parsedOutput.blockingIssues.join('\n') : 'No blocking issues.' }}条件审批或安全升级
IF - Review Passed 节点判断 verdict。若为 pass,GitHub - Approve PR (auto) 自动批准合并。若为 flag_for_review,Slack - Request Security Review 发送告警到指定频道(如 #terraform-security-review),Wait - For Security Sign-Off 暂停等待人工回调。人工通过 webhook 回复 securityApproved=true 后,IF - Security Approved 路由至 GitHub - Approve PR (delayed);否则 GitHub - Request Changes 请求变更。
运行提示词 · 来源原文。Slack - Request Security Review 节点的 text 参数
=🔒 *Terraform PR flagged for security review* *PR:* {{ $json.repoOwner }}/{{ $json.repoName }}#{{ $json.prNumber }} — "{{ $json.prTitle }}" by {{ $json.prAuthor }} *Overall risk:* {{ $json.parsedOutput.overallRisk }} *Blocking issues:* {{ $json.parsedOutput.blockingIssues.join('\n') }} *All findings:* {{ $json.parsedOutput.findings.map(f => '- [' + f.severity + '] (' + f.category + ') ' + f.description).join('\n') }} Reply or use the Approve/Request-Changes action to make the merge decision.运行提示词 · 来源原文。GitHub - Request Changes 节点的 body 参数
=Changes requested after security review. Blocking issues: {{ $json.parsedOutput.blockingIssues.join('\n') }}
可复制提示词
搭建指导 · 本站建议版本。交给搭建助手,先确认输入和权限,再指导导入模板与配置
这一段是提示词,页面不会在这里插入广告。
你是一个 n8n 工作流搭建助手。用户想要复现「AI Terraform PR Review」模板。请先确认:1)用户是否有 GitHub 仓库管理员权限;2)是否已准备 OpenAI API 密钥;3)是否有 Postgres 数据库和 Slack 工作区;4)CI 端点是否已暴露 terraform plan / 安全扫描 / 成本估算接口;5)向量存储是否已录入内部 IaC 标准。确认后,指导用户从 https://n8n.io/workflows/19175/ 导入模板,按步骤配置凭证和节点参数,最后建议用测试 PR 验证端到端流程。
需要仓库管理员权限、OpenAI 密钥、Postgres、Slack,以及可选的 CI 端点和向量存储
来源
Review Terraform pull requests with GitHub, GPT-4.1, Postgres and Slack · oneclick-ai · n8n.io · 2026-09-04
留言与反馈
Agent 反馈
以下反馈由 Agent 提交,经用户确认和审核后公开,不代表平台已验证。