
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 上團(tuán)隊評審效率直接翻倍。本文將用 3 步教你完成配置全程零門檻新手也能 10 分鐘搞定。tf-summarize 是什么Terraform 計劃摘要神器tf-summarize 是 GitHub 加速計劃 / tf 下的開源項目核心功能只有一個把冗長的 terraform plan 濃縮成一眼可讀的摘要。它支持表格、樹狀圖、JSON、HTML 等多種輸出格式其中-md參數(shù)能直接生成 Markdown 表格天然適合發(fā)布到 GitHub PR 評論區(qū)。相比人工翻看 plan 輸出用 tf-summarize 有三大好處?省時間資源變更按 add / delete / update / recreate 分組統(tǒng)計一眼看清影響范圍?易協(xié)作Markdown 表格可以直接貼進(jìn) PR 評論評審人不用自己跑命令?可自動化輸出是純文本/純 Markdown能無縫嵌入 GitHub Actions、Codefresh 等 CI 流水線表格輸出由 writer/table.go 實現(xiàn)命令行參數(shù)定義在 main.go想了解實現(xiàn)細(xì)節(jié)可以順手翻一翻。準(zhǔn)備工作安裝 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驗證一下看到幫助信息就說明環(huán)境 OK 了。?第 1 步生成 Terraform Plan 并導(dǎo)出 JSONtf-summarize 的輸入是terraform plan 的 JSON 格式所以需要兩步轉(zhuǎn)換。在項目目錄下執(zhí)行terraform plan -outtfplan terraform show -json tfplan tfplan.json 小提示tf-summarize也能直接讀取二進(jìn)制tfplan文件但走 JSON 管道更通用CI 場景下強(qiáng)烈推薦。一個典型的 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也可以直接從標(biāo)準(zhǔn)輸入讀取少一個中間文件terraform show -json tfplan | tf-summarize -md保存為文件方便復(fù)用tf-summarize -md -outsummary.md tfplan.json-out參數(shù)會把摘要寫入文件這正是后面 PR 自動評論要用的素材。第 3 步讓 PR 自動評論 Terraform 變更最后一步把上面的命令裝進(jìn) 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 });這份工作流的完整流程解析觸發(fā)條件pull_request事件 限定.tf文件變更避免無關(guān) PR 浪費資源生成摘要terraform show -json tfplan | tf-summarize -md輸出 Markdown 表格并存入summary.md自動評論用github-script把文件內(nèi)容發(fā)到當(dāng)前 PR 評論區(qū)?? 注意使用hashicorp/setup-terraform時務(wù)必設(shè)置terraform_wrapper: false否則會包一層 wrapper導(dǎo)致tf-summarize從標(biāo)準(zhǔn)輸入讀不到數(shù)據(jù)。如果你用的是 Codefresh 而不是 GitHub Actions參考 example/codefresh/codefresh.yaml思路完全一致plan 階段出 JSONshow 階段跑 tf-summarize。更多玩法樹狀圖、JSON、HTML 輸出PR 評論之外tf-summarize 還有不少實用姿勢樹狀圖tf-summarize -tree tfplan.json按模塊層級展示變更加-draw還能畫出 2D 關(guān)系樹JSONtf-summarize -json tfplan.json配合fx等工具做交互式審查HTMLtf-summarize -html tfplan.json生成可視化報告純摘要tf-summarize -json-sum tfplan.json只輸出 add / delete / update 的統(tǒng)計數(shù)量輸出格式的優(yōu)先級與分流邏輯在 writer/writer.go 中一目了然想二次開發(fā)或擴(kuò)展格式的同學(xué)可以直接從這兒入手。小結(jié)從「人工翻 plan」到「PR 自動評論 Terraform 變更」只需要 3 步生成 JSONterraform show -json tfplan tfplan.json輸出表格tf-summarize -md tfplan.json接入 CIGitHub Actions 里跑摘要并自動評論一次配置團(tuán)隊永續(xù)受益。評審人不用再復(fù)制粘貼命令每次 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創(chuàng)作聲明:本文部分內(nèi)容由AI輔助生成(AIGC),僅供參考