簡體   English   中英

使用來自另一個私有倉庫的 terraform 模塊和 git 子模塊

[英]Use terraform module from another private repo with git submodule

我正在嘗試使用來自另一個私有 github 存儲庫的模塊。 這另一個 github 存儲庫具有來自第三存儲庫的 git 子模塊。

module "ecr_repos" {
  for_each = local.ecr_repos 

  source = "git@github.com:gitaccount/repo2.git//terraform/modules/ecr"
  name = each.key
}

tf 模塊不使用 repo 中的 git 子模塊目錄。

它在本地工作(使用我的 ssh 密鑰),但是當我在 github 工作流程中運行 terraform 時,它失敗了。

它也在 docker 容器中本地復制。

在這兩種情況下,我都會注入GITHUB_TOKEN變量,即 github 私有令牌(不是 github 操作生成的臨時令牌)

我跑

git config --global url."https://oauth2:$GITHUB_TOKEN@github.com".insteadOf ssh://git@github.com
terraform init

Initializing modules...
Downloading git::ssh://git@github.com/gitaccount/repo2.git for ecr_repos...
╷
│ Error: Failed to download module
│ 
│ Could not download module "ecr_repos" (ecr.tf:1) source code from "git::ssh://git@github.com/gitaccount/repo2.git": error downloading 'ssh://git@github.com/gitaccount/repo2.git': /usr/bin/git exited with 1: Submodule 'third/repo/submodule/path'
│ (git@github.com:gitaccount/repo3.git) registered for path 'third/repo/submodule/path'
│ Cloning into '/root/repo1/terraform/infra/ecr/.terraform/modules/ecr_repos/third/repo/submodule/path'...
│ git@github.com: Permission denied (publickey).
│ fatal: Could not read from remote repository.
│ 
│ Please make sure you have the correct access rights
│ and the repository exists.
│ fatal: clone of 'git@github.com:gitaccount/repo3.git' into submodule path '/root/repo1/terraform/infra/ecr/.terraform/modules/ecr_repos/third/repo/submodule/path' failed
│ Failed to clone 'third/repo/submodule/path'. Retry scheduled
│ Cloning into '/root/repo1/terraform/infra/ecr/.terraform/modules/ecr_repos/third/repo/submodule/path'...
│ git@github.com: Permission denied (publickey).
│ fatal: Could not read from remote repository.

結果repo2下載到了.terraform目錄,但是從repo3下載子模塊失敗

如果我嘗試使用GITHUB_TOKEN克隆 repo2,則克隆操作成功並且子模塊存在。

我錯過了什么,我還應該配置什么才能使其工作? 將 ssh 密鑰添加到~/.ssh是最后的手段,我真的不想使用它。

錯誤Permission denied (publickey)表示您正在嘗試使用 ssh 公鑰/私鑰進行身份驗證。 聽起來您想使用 https 代替,因為您要提供 GITHUB_TOKEN 值。

例如git::https://example.com/vpc.git

我不太確定您的 gitconfig 命令,但我個人只是更改我的鏈接。 If you're unhappy that the code is using https instead of your locally configured ssh, you'll need to add.ssh keys to your workflow as dynamic module sources aren't supported in Terraform. 也許 git 配置命令也可以工作,但看起來你已經嘗試過了,但它沒有達到預期的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM