You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab中Terraform Plan报告在Merge Request中无法加载求助

GitLab Terraform MR计划小组件加载失败排查

问题背景

在GitLab仓库中使用Terraform管理基础设施,通过CI/CD部署,采用官方多环境管理方案。创建Merge Request(MR)时流水线运行无报错,但MR概览中的Terraform Plan小组件加载失败。已确认artifacts可在GitLab门户中找到。

Build阶段CI配置

.build:
  extends: .before_script_template
  stage: build
  script:
    # - jq --version
    - terraform plan -out="plan.tfplan"
    - terraform show -json $PLAN | jq -r '([.resource_changes[]?.change.actions?]|flatten)|{"create":(map(select(.=="create"))|length),"update":(map(select(.=="update"))|length),"delete":(map(select(.=="delete"))|length)}' > $PLAN_JSON
  artifacts:
    name: plan
    paths:
      - ${TF_ROOT}/$PLAN
      - ${TF_ROOT}/$PLAN_JSON
    reports:
      terraform: ${TF_ROOT}/$PLAN_JSON

生成的plan.json内容

{
  "create": 1,
  "update": 0,
  "delete": 0
}

流水线关键日志片段

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:

  • create
    Terraform will perform the following actions:
azurerm_resource_group.lab-rg-dev-cicd will be created
  • resource "azurerm_resource_group" "lab-rg-dev-cicd" {
    • id = (known after apply)
    • location = "westeurope"
    • name = "rg-dev-cicd"
      }
      Plan: 1 to add, 0 to change, 0 to destroy.
      ─────────────────────────────────────────────────────────────────────────────
      Saved the plan to: plan.tfplan
      ...
      Uploading artifacts...
      Runtime platform arch=amd64 os=windows pid=7816 revision=e0218c92 version=14.3.2
      ./environments/dev/plan.json: found 1 matching files and directories
      Uploading artifacts as "terraform" to coordinator... ok id=162303037 responseStatus=201 Created token=glcbt-64
      ...
      Job succeeded
## 排查建议

### 1. 检查GitLab版本兼容性
你使用的GitLab版本是14.3.2,Terraform报告在MR中显示的功能在该版本存在已知bug。建议升级到14.5及以上版本,该版本修复了多个Terraform报告解析与显示的问题。

### 2. 修正报告格式完整性
当前生成的`plan.json`仅包含变更计数,但GitLab的Terraform小组件需要更完整的变更元数据(如资源类型、名称、具体动作)。替换生成报告的命令为:
```bash
terraform show -json $PLAN | jq -r '{"terraform_version": .terraform_version, "planned_values": .planned_values, "resource_changes": .resource_changes}' > $PLAN_JSON

或者直接使用GitLab官方提供的gitlab-terraform工具自动生成合规报告:

gitlab-terraform plan
gitlab-terraform plan-json

3. 验证路径变量正确性

确认${TF_ROOT}变量指向的路径是CI环境中的正确相对路径。Windows环境下的路径分隔符(\)可能导致GitLab后端解析异常,可尝试直接写死路径测试:

reports:
  terraform: ./environments/dev/plan.json

4. 确认MR与流水线的关联

确保MR关联的是最新的成功流水线,且该流水线的build阶段确实完成了报告上传。若分支更新后未重新触发流水线,MR可能关联旧的无报告记录的流水线。

5. 检查GitLab后台解析日志

若以上操作无效,可联系GitLab管理员查看后台日志,确认是否存在报告JSON格式校验失败、权限不足等解析层面的错误。


内容的提问来源于stack exchange,提问作者Nc_Sr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 05:41:24