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

使用Terraform创建Cloud Build触发器时遇github块报错问题

Terraform创建Cloud Build触发器报错:Blocks of type "github" are not expected here.

问题详情

尝试通过Terraform创建Cloud Build触发器时收到错误:Blocks of type "github" are not expected here.,相关代码如下:

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "3.5.0"
    }
  }
}

provider "google" {
  credentials = file("key-file.json")

  project = var.project_id
}

resource "google_cloudbuild_trigger" "github-trigger" {
  name = "github-trigger"
  filename = "cloudbuild.yaml"
  location = "europe-west1"
  github {
    owner = <owner>
    name = <name>
    push {
      branch = <branch>
    }
  }
    
} 

移除github块和location参数后,代码可正常运行并创建触发器,编码为UTF-8,运行环境为VS Code。

错误原因

你使用的Terraform Google Provider版本(3.5.0)过于老旧,github嵌套块是在后续更新的版本中才被引入到google_cloudbuild_trigger资源中的。在3.5.0版本里,配置GitHub源的Cloud Build触发器需要采用其他语法,并非直接使用github块。

同时,location参数在该旧版本的google_cloudbuild_trigger资源中也未被支持,这也是移除该参数后代码能正常运行的原因之一。

解决建议

升级Terraform Google Provider到较新的稳定版本(比如4.x及以上),之后即可正常使用github块配置触发器。修改后的terraform块示例如下:

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 4.0" # 可根据需求选择更高的稳定版本
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 06:15:37