十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

tf-summarize 输出 Markdown 表格:3 步让 PR 自动评论 Terraform 变更

tf-summarize 输出 Markdown 表格:3 步让 PR 自动评论 Terraform 变更 tf-summarize 输出 Markdown 表格3 步让 PR 自动评论 Terraform 变更【免费下载链接】tf-summarizeA command-line utility to print the summary of the terraform plan项目地址: https://gitcode.com/gh_mirrors/tf/tf-summarizetf-summarize 是一款命令行工具用于快速输出 terraform plan 的变更摘要。每次跑terraform plan都面对几百行输出把变更摘要以 Markdown 表格形式自动评论到 PR 上团队评审效率直接翻倍。本文将用 3 步教你完成配置全程零门槛新手也能 10 分钟搞定。tf-summarize 是什么Terraform 计划摘要神器tf-summarize 是 GitHub 加速计划 / tf 下的开源项目核心功能只有一个把冗长的 terraform plan 浓缩成一眼可读的摘要。它支持表格、树状图、JSON、HTML 等多种输出格式其中-md参数能直接生成 Markdown 表格天然适合发布到 GitHub PR 评论区。相比人工翻看 plan 输出用 tf-summarize 有三大好处✅省时间资源变更按 add / delete / update / recreate 分组统计一眼看清影响范围✅易协作Markdown 表格可以直接贴进 PR 评论评审人不用自己跑命令✅可自动化输出是纯文本/纯 Markdown能无缝嵌入 GitHub Actions、Codefresh 等 CI 流水线表格输出由 writer/table.go 实现命令行参数定义在 main.go想了解实现细节可以顺手翻一翻。准备工作安装 tf-summarize 的最快方法方式一HomebrewmacOS 最省心brew install tf-summarize方式二Docker无需安装任何依赖alias tf-summarizedocker run -v $PWD:/workspace -w /workspace ghcr.io/dineshba/tf-summarize方式三Go 一键安装go install github.com/dineshba/tf-summarizelatest装完后用tf-summarize -h验证一下看到帮助信息就说明环境 OK 了。✅第 1 步生成 Terraform Plan 并导出 JSONtf-summarize 的输入是terraform plan 的 JSON 格式所以需要两步转换。在项目目录下执行terraform plan -outtfplan terraform show -json tfplan tfplan.json 小提示tf-summarize也能直接读取二进制tfplan文件但走 JSON 管道更通用CI 场景下强烈推荐。一个典型的 GitHub provider 示例配置可以参考 example/github/main.tf跑完上面的命令你会得到包含全部变更信息的tfplan.json。第 2 步用 tf-summarize 输出 Markdown 表格一行命令表格即出tf-summarize -md tfplan.json输出效果长这样ChangeResourceadd (5)github_repository.terraform_plan_summarydelete (1)module.github[terraform-plan-summary].github_branch.trailupdate (1)module.github[terraform-plan-summary].github_repository.repository也可以直接从标准输入读取少一个中间文件terraform show -json tfplan | tf-summarize -md保存为文件方便复用tf-summarize -md -outsummary.md tfplan.json-out参数会把摘要写入文件这正是后面 PR 自动评论要用的素材。第 3 步让 PR 自动评论 Terraform 变更最后一步把上面的命令装进 GitHub Actions每次提交 PR 自动评论。在仓库.github/workflows/下新建terraform-plan-comment.ymlname: Terraform Plan Comment on: pull_request: paths: [**.tf] permissions: contents: read pull-requests: write jobs: plan: runs-on: ubuntu-latest steps: - uses: actions/checkoutv4 - uses: hashicorp/setup-terraformv3 with: terraform_wrapper: false # 安装 tf-summarize - uses: kishaningithub/setup-tf-summarizev1 - name: Generate Terraform plan summary id: plan run: | terraform init terraform plan -outtfplan terraform show -json tfplan | tf-summarize -md summary.md - name: Comment PR uses: actions/github-scriptv7 with: script: | const fs require(fs); const body fs.readFileSync(summary.md, utf8); github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body });这份工作流的完整流程解析触发条件pull_request事件 限定.tf文件变更避免无关 PR 浪费资源生成摘要terraform show -json tfplan | tf-summarize -md输出 Markdown 表格并存入summary.md自动评论用github-script把文件内容发到当前 PR 评论区⚠️ 注意使用hashicorp/setup-terraform时务必设置terraform_wrapper: false否则会包一层 wrapper导致tf-summarize从标准输入读不到数据。如果你用的是 Codefresh 而不是 GitHub Actions参考 example/codefresh/codefresh.yaml思路完全一致plan 阶段出 JSONshow 阶段跑 tf-summarize。更多玩法树状图、JSON、HTML 输出PR 评论之外tf-summarize 还有不少实用姿势树状图tf-summarize -tree tfplan.json按模块层级展示变更加-draw还能画出 2D 关系树JSONtf-summarize -json tfplan.json配合fx等工具做交互式审查HTMLtf-summarize -html tfplan.json生成可视化报告纯摘要tf-summarize -json-sum tfplan.json只输出 add / delete / update 的统计数量输出格式的优先级与分流逻辑在 writer/writer.go 中一目了然想二次开发或扩展格式的同学可以直接从这儿入手。小结从「人工翻 plan」到「PR 自动评论 Terraform 变更」只需要 3 步生成 JSONterraform show -json tfplan tfplan.json输出表格tf-summarize -md tfplan.json接入 CIGitHub Actions 里跑摘要并自动评论一次配置团队永续受益。评审人不用再复制粘贴命令每次 PR 都能看到清晰的变更清单谁改了什么、删了什么、动了哪块资源一目了然。这就是 tf-summarize 输出 Markdown 表格的最大价值——把 Terraform 变更从「黑盒」变成「白纸黑字」。✨【免费下载链接】tf-summarizeA command-line utility to print the summary of the terraform plan项目地址: https://gitcode.com/gh_mirrors/tf/tf-summarize创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表