在 Terraform 和 Cloudbuild 中找不到 Gsutil

Posted

技术标签:

【中文标题】在 Terraform 和 Cloudbuild 中找不到 Gsutil【英文标题】:Gsutil not found with Terraform and Cloudbuild 【发布时间】:2021-07-16 21:51:26 【问题描述】:

我的计划 terraform 在使用云构建时遇到问题。我无法在模块 terraform 中使用 gsutil 命令,出现错误:

Error: Error running command 'gsutil -m rsync -d -r ../../../sources/composer gs://toto/dags/': exit status 127. Output: /bin/sh: gsutil: not found

我的 cloudbuild.yaml :

steps:
- id: 'branch name'
  name: 'alpine'
  entrypoint: 'sh'
  args:
    - '-c'
    - |
      echo "***********************"
      echo "$BRANCH_NAME"
      echo "***********************"
...
# [START tf-apply]
- id: 'tf apply'
  name: 'hashicorp/terraform:0.15.0'
  entrypoint: 'sh'
  args:
    - '-c'
    - |
      if [ -d "terraform/environments/$BRANCH_NAME/" ]; then
        cd terraform/environments/$BRANCH_NAME      
        terraform apply -auto-approve
      else
        echo "***************************** SKIPPING APPLYING *******************************"
        echo "Branch '$BRANCH_NAME' does not represent an oficial environment."
        echo "*******************************************************************************"
      fi
# [END tf-apply]
timeout: 3600s

我将文件放入 gcs 的模块:

resource "null_resource" "upload_folder_content" 
  provisioner "local-exec" 
    command = "gsutil -m rsync -d -r $var.dag_folder_path $var.composer_dag_gcs/"
  

【问题讨论】:

【参考方案1】:

当您在您的步骤中使用 Hashicorp 的 Terraform 图像时,可以预期 gsutil 默认情况下不包含它,因此您无法运行您的 null_resource 定义的与您相反的命令d 能够在您的本地环境中进行操作。

为了克服这个问题,您可以构建自己的自定义映像并将其push 发送到 Google Container Registry,以便您以后可以使用它。使用该选项,您还将拥有更大的灵活性,因为您可以安装 Terraform 代码具有的任何依赖项。

【讨论】:

感谢您的回答。使用云构建和 terraform 将文件放入 gcs 是唯一的解决方案。最后,我们使用自己的 jenkins 服务器并安装了 terraform 和 gsutil。 很高兴我能帮上忙! Jenkins 可能会更好,因为您可以更好地控制环境,并且可以安装插件,有很多方便的功能可供使用:)【参考方案2】:

如果您查看实际的错误行,最后会显示这是命令的输出:

/bin/sh: gsutil: not found

我怀疑 gsutil 在您的 shell 路径中根本找不到。

也许您需要安装任何包gsutil

【讨论】:

我同意你的看法,但我担心我正在使用 Cloud Build 来运行我的 terraform 计划,我无法安装 gsutil,因为它处于无服务器模式。我正在使用图像:hashicorp / terraform: 0.15.0

以上是关于在 Terraform 和 Cloudbuild 中找不到 Gsutil的主要内容,如果未能解决你的问题,请参考以下文章

有啥方法可以从 docker hub 提取图像并使用 cloudbuild.yaml 部署到云运行

无法在 CloudBuild 中拉取 golang 映像

如何在 google cloudbuild 上构建 quarkus 原生镜像

cloudbuild.yaml 包含不同的云构建器配置

cloudbuild.yaml 上的错误:(gcloud.builds.submit)将 cloudbuild.yaml 解释为构建配置:“列表”对象没有属性“项目”

Cloudbuild 中的秘密环境变量(没有文件),如何?