代码之家  ›  专栏  ›  技术社区  ›  James Geddes

当将现有的repo导入github _repository时,如何修复Schrdinger的repo?

  •  3
  • James Geddes  · 技术社区  · 2 周前

    背景

    我的 estate-repos 回购,

    目标:导入手动创建的仓库

    我正试图将我(可悲的是)在构建房地产回购之前手动创建的回购导入房地产回购。

    期望

    import {
      to = module.github_repository["estate-config"].github_repository.repo
      id = "evoteum/estate-config"
    }
    

    这应该会导致现有的回购成功导入豆腐管理。

    现实

    在导入块就位的情况下, tofu apply 返回以下错误;

     module.github_repository["estate-config"].github_repository.repo: Refreshing state... [id=evoteum/estate-config]
    ╷
    │ Error: Cannot import non-existent remote object
    │ 
    │ While attempting to import an existing object to
    │ "module.github_repository[\"estate-config\"].github_repository.repo", the
    │ provider detected that no object exists with the given id. Only
    │ pre-existing objects can be imported; check that the id is correct and that
    │ it is associated with the provider's configured region or endpoint, or use
    │ "tofu apply" to create a new remote object for this resource.
    ╵
    

    如果我删除 import 块, 豆腐涂抹 返回,

     module.github_repository["estate-config"].github_repository.repo: Creating...
    ╷
    │ Error: POST https://api.github.com/orgs/evoteum/repos: 422 Repository creation failed. [{Resource:Repository Field:name Code:custom Message:name already exists on this account}]
    │ 
    │   with module.github_repository["estate-config"].github_repository.repo,
    │   on .terraform/modules/github_repository/github/repo/main.tofu line 11, in resource "github_repository" "repo":
    │   11: resource "github_repository" "repo" {
    │ 
    ╵
    Error: OpenTofu exited with code 1.
    Error: Process completed with exit code 1.
    

    此外,两者 gh CLI和 API 确认回购可用。

    安装程序

    • 在GitHub Actions中运行OpenTofu。
    • 房地产回购使用GitHub应用程序“evoteumbot”运行
    • evoteumbot拥有以下权限:;
      • 存储库.操作=写入
      • 仓库管理=写入
      • 存储库.内容=写入
      • repository.media=已读
      • 存储库.secrets=写入
      • 存储库.变量=写入
      • 组织管理=写作
      • 组织。秘密=写
      • 组织.变量=写入
    • evoteumbot已经成功创建了几个repo。
    • 当使用具有所有权限的个人访问令牌时,我得到了相同的响应。
    • 版本:
    • 跑步 tofu state list 回报;
    module.github_repository[".github"].github_branch.main
    module.github_repository[".github"].github_branch_default.main
    module.github_repository[".github"].github_repository.repo
    module.github_repository[".github"].github_repository_file.readme
    module.github_repository["discussions"].github_branch.main
    module.github_repository["discussions"].github_branch_default.main
    module.github_repository["discussions"].github_repository.repo
    module.github_repository["discussions"].github_repository_file.readme
    module.github_repository["estate-reusable-workflows"].github_branch.main
    module.github_repository["estate-reusable-workflows"].github_branch_default.main
    module.github_repository["estate-reusable-workflows"].github_repository.repo
    module.github_repository["estate-reusable-workflows"].github_repository_file.readme
    module.github_repository["evoteum.github.io"].github_branch.main
    module.github_repository["evoteum.github.io"].github_branch_default.main
    module.github_repository["evoteum.github.io"].github_repository.repo
    module.github_repository["evoteum.github.io"].github_repository_file.readme
    module.github_repository["planzoco"].github_branch.main
    module.github_repository["planzoco"].github_branch_default.main
    module.github_repository["planzoco"].github_repository.repo
    module.github_repository["planzoco"].github_repository_file.readme
    module.github_repository["testy-mctestface"].github_branch.main
    module.github_repository["testy-mctestface"].github_branch_default.main
    module.github_repository["testy-mctestface"].github_repository.repo
    module.github_repository["testy-mctestface"].github_repository_file.readme
    

    提问

    既然我们已经确认API知道回购确实存在,那么为什么GitHub Terraform提供商说它既存在也不存在呢?我做错了什么?

    1 回复  |  直到 2 周前
        1
  •  1
  •   Chris Doyle    2 周前

    当您导入到 github_repository 你需要使用 name . https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository#import

    可以使用以下名称导入存储库

    在您的repos文件中 名称 s只是repo名称,但在您的导入中,您可以用owner作为前缀。但是您的PAT令牌已经与之关联,并且可能会添加前缀,因此您的导入将成为 evoteum/evoteum/estate-config

    我已经能够用以下方式重现这个问题

    resource "github_repository" "repo" {
      name = "killercoda"
    }
    
    import {
      id = "cdsre/killercoda"
      to = github_repository.repo
    }
    
    
    $ terraform apply
    github_repository.repo: Preparing import... [id=cdsre/killercoda]
    github_repository.repo: Refreshing state... [id=cdsre/killercoda]
    ╷
    │ Error: Cannot import non-existent remote object
    │
    │ While attempting to import an existing object to "github_repository.repo", the provider detected that no object exists with the given id. Only pre-existing objects can be imported; check that the id  
    │ is correct and that it is associated with the provider's configured region or endpoint, or use "terraform apply" to create a new remote object for this resource.
    ╵
    

    当我从导入中的id中删除我的用户前缀时

    
    github_repository.repo: Importing... [id=killercoda]
    github_repository.repo: Import complete [id=killercoda]
    github_repository.repo: Modifying... [id=killercoda]
    github_repository.repo: Modifications complete after 2s [id=killercoda]
    
    Apply complete! Resources: 1 imported, 0 added, 1 changed, 0 destroyed.
    

    编辑 我已经检查了github terraform提供者的源代码,可以看到这正是发生的事情 https://github.com/integrations/terraform-provider-github/blob/main/github/resource_github_repository.go#L628

    func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) error {
        client := meta.(*Owner).v3client
    
        owner := meta.(*Owner).name
        repoName := d.Id()
    ...
    ...
    
        repo, resp, err := client.Repositories.Get(ctx, owner, repoName)
    
    推荐文章