Skip to content

gh-files-set: preserve user-managed files on terraform destroy#1107

Draft
Copilot wants to merge 2 commits intofeat/969-poc-gh-files-setfrom
copilot/sub-pr-970
Draft

gh-files-set: preserve user-managed files on terraform destroy#1107
Copilot wants to merge 2 commits intofeat/969-poc-gh-files-setfrom
copilot/sub-pr-970

Conversation

Copy link
Contributor

Copilot AI commented Mar 21, 2026

github_repository_file always calls the GitHub delete API on destroy — lifecycle { ignore_changes = [content] } only suppresses content drift, it does not prevent file deletion. terraform destroy would wipe user-managed files from GitHub.

Changes

  • main.tf: Replace github_repository_file.user_managed with terraform_data + local-exec provisioner

    • terraform_data has no built-in destroy action; without a when = destroy provisioner, terraform destroy removes the state entry only — the GitHub file is untouched
    • Provisioner calls PUT /repos/{owner}/{repo}/contents/{file} via curl + $GITHUB_TOKEN on create/re-provision
    • Respects overwrite_on_create: fetches current SHA and skips or updates accordingly
    • triggers_replace = [file, branch] — content drift is intentionally ignored; only path or branch change re-provisions
    • JSON-safe commit message encoding (python3sed fallback); jqgrep/sed fallback for SHA extraction; API errors surfaced to stderr
  • README.md / docs/header.md: Document preserve-on-destroy behavior; add GITHUB_TOKEN env var requirement (same token as the provider); update resource table to terraform_data.user_managed

  • _examples/basic/files.yaml: Remove invalid lifecycle: {} field; add a userManaged: true example entry

# Before — file is deleted on terraform destroy
resource "github_repository_file" "user_managed" {
  lifecycle { ignore_changes = [content] }
}

# After — state entry removed, GitHub file preserved
resource "terraform_data" "user_managed" {
  triggers_replace = [each.value.file, each.value.branch]
  provisioner "local-exec" { command = <<-EOT ... EOT }
  # no when = destroy provisioner
}

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] [WIP] Address feedback on user-managed files in gh-files-set module gh-files-set: preserve user-managed files on terraform destroy Mar 21, 2026
Copilot AI requested a review from alambike March 21, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants