git - 如何通过 Terraform 下载 git 代码?

标签 git github terraform

我正在尝试在 Terraform 中 check out git repo 代码并访问其中的文件 (my_file.py),我使用了以下代码,

module "my_git_repo" {
  source = "https://git.mycompany.org/my_repo.git"
}
输出:
 "my_display" {
      value       = "${my_git_repo.source}/my_file.py"
    }
我得到的错误是,
错误:无法下载模块
下载“https://git.mycompany.org/my_repo.git”:否
已返回源 URL
注意:我可以在命令上使用“git clone” checkout 。

最佳答案

一个 module Terraform 中的块用于引用 Terraform 模块,而不是像 Python 文件这样的任意代码。
正如 Don 提到的,告诉 Terraform 从 HTTPS URL 使用 git 获取模块的正确语法是添加 git::前缀,覆盖 HTTP URL 的默认处理:

module "my_git_repo" {
  source = "git::https://git.mycompany.org/my_repo.git"
}
但是,如果您要在不包含任何 .tf 的存储库中使用它目标目录中的文件,然后 Terraform 会提示源地址没有引用有效的 Terraform 模块。要获得类似于您在问题中描述的内容,您还需要在该目录中至少放置一个最小的 Terraform 模块配置,例如以下 outputs.tf文件:
output "python_file" {
  value = "${path.module}/my_file.py"
}
您的调用模块可能如下所示:
module "my_git_repo" {
  source = "git::https://git.mycompany.org/my_repo.git"
}

output "my_display" {
  value = module.my_git_repo.python_file
}

然而,这对于 Terraform 来说是一件非常不寻常的事情。您在这里没有提到您的基本目标是什么,但是您提出问题的方式让我怀疑您正在尝试使用 Terraform 来解决它不是旨在解决的问题。可能值得提出一个新问题,该问题更多地围绕您的潜在问题(为什么您需要 Terraform 配置中远程 git 存储库中的这个 Python 文件),其中的回答者可能能够建议其他方法来解决该问题,而无需强制远程存储库的行为类似于 Terraform 模块。

关于git - 如何通过 Terraform 下载 git 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62988678/

相关文章:

terraform - module.db 是一个对象列表,只有在申请后才知道

作者姓名为 "/CN=myname"的 Git svn 克隆

git - 为什么 .gitattributes 不会覆盖 Linux 上的 core.autocrlf 配置?

r - 如何在 RStudio 中 merge git 分支

"no available releases match the given constraints ~> 2.1.28"的地形初始化步骤

azure - 如何使用 terraform 为 azure redis 缓存创建专用端点?

git checkout 在 --assume-unchanged 文件上抛出错误

git - 克隆 git 时 vagrant setup 抛出 Permission denied (publickey)

推送被拒绝后 git rebase 修复

git - 更新远程服务器以从重命名的 Git 存储库中提取