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

如何通过Terraform实现Google Cloud Function接入企业版GitHub

结论

首先,Google Cloud Function 原生的 source_repository 配置块仅支持填写 Google Cloud Source Repository 的URL,不支持直接填入第三方GitHub(包括企业版)的仓库地址,所以你示例代码里直接填写企业版GitHub地址的写法无法生效。

符合要求的实现方案(无需使用Cloud Source Repository)

推荐用本地代码打包上传的方案,操作逻辑完全和Cloud Source Repository解耦,具体步骤如下:

  1. 在执行Terraform的环境(本地/CI流水线)中,提前从企业版GitHub拉取函数源代码到本地目录
  2. 用Terraform的archive数据源自动打包代码为zip文件,直接上传到Cloud Function

完整Terraform代码示例

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 4.0"
    }
    archive = {
      source  = "hashicorp/archive"
      version = "~> 2.2"
    }
  }
}

# 打包本地的函数代码目录
data "archive_file" "function_source" {
  type        = "zip"
  source_dir  = "./cf-code" # 替换为你拉取到本地的企业版GitHub代码目录路径
  output_path = "./function_source.zip"
}

resource "google_cloudfunctions_function" "cfcluster" {
  name        = "cfcluster1"
  project     = var.project_id
  region      = "us-central1"
  runtime     = "python39"
  entry_point = "handler" # 替换为你Python代码中的实际入口函数名

  # 直接引用打包好的代码压缩包,无需对接任何代码仓库
  source_archive_file = data.archive_file.function_source.output_path

  # 其他按需配置,以下为示例
  available_memory_mb = 256
  trigger_http        = true
}
注意事项
  • 执行terraform apply前要确保本地代码目录下直接包含入口函数文件,不要嵌套多余层级
  • 如果用CI流水线部署,只需要在执行Terraform命令前添加一步git clone企业版GitHub仓库的步骤即可,企业版GitHub的认证(SSH密钥/PAT)直接配置在CI环境中,不需要写入Terraform代码,安全性更高

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 19:36:02