0

在 tf 版本 1.0.3

我在本地导入资源:

terraform import github_repository.terraform-xxxx-github_repo terraform-xxxx-github
github_repository.terraform-xxxx-github_repo: Importing from ID "terraform-xxxx-github"...
github_repository.terraform-xxxx-github_repo: Import prepared!
  Prepared github_repository for import
github_repository.terraform-xxxx-github_repo: Refreshing state... [id=terraform-xxxx-github]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

这是 repos.tf 文件中的资源配置:

resource "github_repository" "terraform-xxxx-github_repo" {
  name                   = "terraform-xxxx-github"
  visibility             = "private"
  has_projects           = true
  has_wiki               = true
  has_issues             = true
  has_downloads          = true
  allow_merge_commit     = true
  allow_squash_merge     = true
  allow_rebase_merge     = true
  archived               = false
  auto_init              = false
}

现在,当我在本地运行 tf init 和 tf plan 时,我确实没有看到任何变化:

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of integrations/github from the dependency lock file
- Reusing previous version of hashicorp/aws from the dependency lock file
- Using previously-installed integrations/github v4.13.0
- Using previously-installed hashicorp/aws v3.54.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

当我运行 tf 计划时:

github_repository.terraform-xxxx-github_repo: Refreshing state... [id=terraform-xxxx-github]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

但是,问题是当我将本地推送到连接到在 Jenkins 上运行的 Terraform 的 github PR 时......在 Jenkins tf 计划中,我看到了这个:

10:59:30  + terraform plan -input=false -out=terraform.plan
10:59:33  github_repository.terraform-xxxx-github_repo: Refreshing state... [id=terraform-xxxx-github]
10:59:33  
10:59:33  Note: Objects have changed outside of Terraform
10:59:33  
10:59:33  Terraform detected the following changes made outside of Terraform since the
10:59:33  last "terraform apply":
10:59:33  
10:59:33    # github_repository.terraform-xxxx-github_repo has been deleted
10:59:33    - resource "github_repository" "terraform-xxxx-github_repo" {
10:59:33        - allow_merge_commit     = true -> null
10:59:33        - allow_rebase_merge     = true -> null
10:59:33        - allow_squash_merge     = true -> null
10:59:33        - archived               = false -> null
10:59:33        - auto_init              = false -> null
10:59:33        - default_branch         = "master" -> null
10:59:33        - visibility             = "private" -> null
10:59:33      }
10:59:33  
10:59:33  Unless you have made equivalent changes to your configuration, or ignored the
10:59:33  relevant attributes using ignore_changes, the following plan may include
10:59:33  actions to undo or respond to these changes.
10:59:33  
10:59:33  ─────────────────────────────────────────────────────────────────────────────
10:59:33  
10:59:33  Terraform used the selected providers to generate the following execution
10:59:33  plan. Resource actions are indicated with the following symbols:
10:59:33    + create
10:59:33  
10:59:33  Terraform will perform the following actions:
10:59:33  
10:59:33    # github_repository.terraform-xxxx-github_repo will be created
10:59:33    + resource "github_repository" "terraform-xxxx-github_repo" {
10:59:33        + allow_merge_commit     = true
10:59:33        + allow_rebase_merge     = true
10:59:33        + allow_squash_merge     = true
10:59:33        + archived               = false
10:59:33        + auto_init              = false
10:59:33        + default_branch         = (known after apply)
10:59:33        + visibility             = "private"
10:59:33      }
10:59:33  
10:59:33  Plan: 1 to add, 0 to change, 0 to destroy.

所以它没有接受我在本地进行的 terraform 导入。我不知道为什么会这样,也不知道如何解决这个问题。

4

1 回答 1

0

解决方案是使用的 github 个人访问令牌没有 github 帐户/repo 的正确权限。因此,使用了具有更多访问权限的新令牌并修复了此问题。

于 2021-08-20T18:52:05.113 回答