Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'github_repository_file' overwriting existing files even after 'overwrite_on_create' is set to 'false' #906

Closed
parag-neve-db opened this issue Sep 16, 2021 · 9 comments · Fixed by #2095
Labels
Good first issue Good for newcomers r/repository_file Type: Bug Something isn't working as documented Type: Documentation Improvements or additions to documentation

Comments

@parag-neve-db
Copy link

'github_repository_file' overwriting existing files even after 'overwrite_on_create' is set to 'false'

We are creating github repository and creating some files within the repository after the repo has been created. Some of the files we are creating like config should not be overwritten if it already exists and to achieve this we are using the overwrite_on_create attribute and set it to false. However, we are noticing that the github_repository_file is still overwriting the file on every terraform plan/apply.

GitHub Provider Version

v4.12.0

Terraform Version

v0.14.11

Affected Resource(s)

Please list the resources as a list, for example:

  • github_repository_file

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

resource "github_repository" "repo" {
  name         = var.github_repo_name
  description  = var.github_repo_description
  homepage_url = var.homepage_url

  visibility = var.visibility

  has_issues             = var.has_issues
  has_projects           = var.has_projects
  has_wiki               = var.has_wiki
  is_template            = var.is_template
  allow_merge_commit     = var.allow_merge_commit
  allow_squash_merge     = var.allow_squash_merge
  allow_rebase_merge     = var.allow_rebase_merge
  delete_branch_on_merge = var.delete_branch_on_merge
  auto_init              = true

  archived           = false
  archive_on_destroy = true

  vulnerability_alerts = var.vulnerability_alerts
}

resource "github_repository_file" "epo_files" {
  for_each = { for plugin_repo_file in var.plugin_repo_files : plugin_repo_file.file_path => plugin_repo_file }

  repository          = github_repository.repo.name
  branch              = var.default_branch
  file                = each.value.file_path
  content             = each.value.file_content
  overwrite_on_create = each.value.overwrite_on_create

  commit_message = "<value-removed-as-its-sensitive>"
  commit_author  = "<value-removed-as-its-sensitive>"
  commit_email   = "<value-removed-as-its-sensitive>"

  lifecycle {
    ignore_changes = [
      commit_email,
      commit_author,
      branch
    ]
  }

  depends_on = [
    github_branch_default.plugin_default_branch
  ]
}

# Variable var.plugin_repo_files has following value set

plugin_repo_files = [
    {
      file_path           = "README.md"
      file_content        = file("./files/readme.md")
      overwrite_on_create = true
    },
    {
      file_path           = "config.yaml"
      file_content        = file("./files/config.yaml")
      overwrite_on_create = false
    }
  ]

Expected Behavior

On subsequent runs the config.yaml file should not get overwritten as it has overwrite_on_create' is set to 'false'

Actual Behavior

When we run the plan again, the github_repository_file is overwriting the config.yaml file in the repository

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
  2. Update the config.yaml file in the new repository
  3. Run terraform plan again

References

@jcudit jcudit added r/repository_file Type: Bug Something isn't working as documented Awaiting response labels Sep 28, 2021
@jcudit
Copy link
Contributor

jcudit commented Sep 28, 2021

Any change we can get more debug output? Adding TF_LOG=DEBUG ahead of any run would produce helpful logs to debug this further.

@arthur-rock
Copy link

arthur-rock commented Nov 9, 2021

Hi @jcudit !
I have the same problem and I try to reproduce it with the flag TF_LOG=DEBUG and I hope to help you to solve it.

Of course I'm using
Terraform version: 0.15.3, Go runtime version: go1.16.2 and terraform/github 4.18.0

terraform {
  required_providers {
    github = {
      source  = "hashicorp/github"
      version = "4.18.0"
    }
  }
}
provider "github" {
  token = var.github_token
  owner = "arthur-rock"
}
variable "github_token" {
  default = "-------------------"
}
variable "github_owner" {
  default = "arthur-rock"
}
variable "commit_message" {
  default = "Managed with Terraform by "
}
variable "commit_email" {
  default = "---------------"
}
variable "repository_name" {
  default = "example-terraform"
}
variable "commit_author" {
 default = "arthur-rock"
}

data "github_repository" "rep" {
  full_name = "${var.github_owner}/${var.repository_name}"
}

resource "github_repository_file" "testing" {
  repository          = data.github_repository.rep.name
  branch              = "main"
  file                = "testing/test.txt"
  content             = "testing"
  commit_message      = var.commit_message
  commit_author       = var.commit_author
  commit_email        = var.commit_email
  overwrite_on_create = false
}


output:
tf_plan.txt

@marianobntz-silohub
Copy link

same thing here... anything else you need?

@andlucasamos
Copy link

@parag-neve-db @marianobntz-silohub This looks like the intended behaviour to me. I think you want to add this to your github_repository_file resource. This way the file will be created but any changes to its content will be ignored.

  lifecycle {
    ignore_changes = [
      content
    ]
  }

@marianobntz-silohub
Copy link

You are right @parag-neve-db , adding that does the trick, when I update the content the plan is not updated. THANKS!
PS: it should be documented in the github-file in terraform so we know how to handle this case that I think is common? 😄

@jcudit jcudit added Type: Documentation Improvements or additions to documentation Good first issue Good for newcomers and removed Awaiting response labels Jan 5, 2022
@juicybaba
Copy link

juicybaba commented Jan 9, 2022

I got below error when doing the following task.

Error: [ERROR] Refusing to overwrite existing file. Configure overwrite_on_create to true to override.

I want to add a file to all terraform managed repos, but there is already a file with the exact name/content in a number of repos.

I planned to have "overwrite_on_create" = false to ignore the file creation for the repos that have the file already (but not the others), so that I can have the file in state and manage it via terraform if content changes going forward?

Or what does "overwrite_on_create" really do? @jcudit

@andlucasamos
Copy link

I got below error when doing the following task.

Error: [ERROR] Refusing to overwrite existing file. Configure overwrite_on_create to true to override.

I want to add a file to all terraform managed repos, but there is already a file with the exact name/content in a number of repos.

I planned to have "overwrite_on_create" = false to ignore the file creation for the repos that have the file already (but not the others), so that I can have the file in state and manage it via terraform if content changes going forward?

Or what does "overwrite_on_create" really do? @jcudit

IIUC overwrite_on_create does the following

  • If set to true it will overwrite an existing file with the same name
  • If set to false it will fail if there is an existing file with the same name.

@juicybaba
Copy link

I got below error when doing the following task.
Error: [ERROR] Refusing to overwrite existing file. Configure overwrite_on_create to true to override.
I want to add a file to all terraform managed repos, but there is already a file with the exact name/content in a number of repos.
I planned to have "overwrite_on_create" = false to ignore the file creation for the repos that have the file already (but not the others), so that I can have the file in state and manage it via terraform if content changes going forward?
Or what does "overwrite_on_create" really do? @jcudit

IIUC overwrite_on_create does the following

  • If set to true it will overwrite an existing file with the same name
  • If set to false it will fail if there is an existing file with the same name.

gotcha, thanks for the reply

@github-actions
Copy link

👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

@github-actions github-actions bot added the Status: Stale Used by stalebot to clean house label Nov 30, 2022
@github-actions github-actions bot removed the Status: Stale Used by stalebot to clean house label Dec 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good first issue Good for newcomers r/repository_file Type: Bug Something isn't working as documented Type: Documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants