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

使用GitHub Actions执行Terraform(GCP)时遇文件找不到错误求助

问题:GitHub Actions中Terraform无法找到terra2.zip文件

问题详情

作为Terraform(GCP)新手,在通过GitHub Actions实现CI/CD时触发错误:

Error: open ./terra2.zip: no such file or directory

已尝试两种方案但均未解决:

  • 将文件上传至GCP Bucket,使用gs://链接作为source值
  • 将文件上传至GitHub仓库,尝试./terra2.zip、terra2.zip、.\terra2.zip等路径

相关Terraform代码:

resource "google_storage_bucket_object" "function_source" {
  name   = "terra2.zip"
  bucket = google_storage_bucket.source_files_production.name
  source = "./terra2.zip"
}

解决方案

1. 确认文件在GitHub Actions工作目录中存在

GitHub Actions默认工作目录是仓库根目录,需确保文件已被推送到仓库,或在CI流程中生成/下载到当前目录:

  • 直接在GitHub仓库页面检查terra2.zip是否存在
  • 在CI步骤中添加ls -la命令,查看当前目录的文件列表,确认文件位置

2. 正确引用GCP Bucket中的文件

Terraform的google_storage_bucket_object的source参数仅支持本地文件路径,不能直接用gs://链接。需先通过gsutil将Bucket中的文件下载到工作目录,再使用本地路径:

# GitHub Actions步骤示例
- name: 从GCP Bucket下载terra2.zip
  run: gsutil cp gs://你的Bucket名称/terra2.zip ./terra2.zip
- name: 执行Terraform Apply
  run: terraform apply -auto-approve

3. 规范路径写法

GitHub Actions使用Linux runner时,路径分隔符为/,避免使用Windows风格的\:

  • 若Terraform配置在仓库根目录,直接用terra2.zip或./terra2.zip
  • 若Terraform配置在子目录(如./terraform),需使用相对上级目录的路径../terra2.zip,或绝对路径/home/runner/work/仓库名/仓库名/terra2.zip

4. 确保Terraform在正确目录执行

如果Terraform配置文件存放在子目录,需先切换到对应目录再执行命令:

- name: 执行Terraform Apply
  run: |
    cd infra
    terraform apply -auto-approve

内容的提问来源于stack exchange,提问作者Dev-Yazeed

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 20:24:52