Skip to content

Latest commit

 

History

History
115 lines (76 loc) · 4.37 KB

File metadata and controls

115 lines (76 loc) · 4.37 KB
layout github
page_title GitHub: github_repository_file
description Creates and manages files within a GitHub repository

github_repository_file

This resource allows you to create and manage files within a GitHub repository.

~> Note: When a repository is archived, Terraform will skip deletion of repository files to avoid API errors, as archived repositories are read-only. The files will be removed from Terraform state without attempting to delete them from GitHub.

Example Usage

Existing Branch

resource "github_repository" "foo" {
  name      = "example"
  auto_init = true
}

resource "github_repository_file" "foo" {
  repository          = github_repository.foo.name
  branch              = "main"
  file                = ".gitignore"
  content             = "**/*.tfstate"
  commit_message      = "Managed by Terraform"
  commit_author       = "Terraform User"
  commit_email        = "[email protected]"
  overwrite_on_create = true
}

Auto Created Branch

resource "github_repository" "foo" {
  name      = "example"
  auto_init = true
}

resource "github_repository_file" "foo" {
  repository          = github_repository.foo.name
  branch              = "does/not/exist"
  file                = ".gitignore"
  content             = "**/*.tfstate"
  commit_message      = "Managed by Terraform"
  commit_author       = "Terraform User"
  commit_email        = "[email protected]"
  overwrite_on_create = true
  autocreate_branch   = true
}

Argument Reference

The following arguments are supported:

  • repository - (Required) The repository to create the file in.

  • file - (Required) The path of the file to manage.

  • content - (Required) The file content.

  • branch - (Optional) Git branch (defaults to the repository's default branch). The branch must already exist, it will only be created automatically if 'autocreate_branch' is set true.

  • commit_author - (Optional) Committer author name to use. NOTE: GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. This maybe useful when a branch protection rule requires signed commits.

  • commit_email - (Optional) Committer email address to use. NOTE: GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. This may be useful when a branch protection rule requires signed commits.

  • commit_message - (Optional) The commit message when creating, updating or deleting the managed file.

~> Note on signed commits: delete commits are produced through GitHub's GraphQL createCommitOnBranch mutation so they are web-flow signed and satisfy rulesets that require signed commits. Create and update commits go through the REST Contents API, which is web-flow signed only when commit_author and commit_email are left unset.

  • overwrite_on_create - (Optional) Enable overwriting existing files. 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.

  • autocreate_branch - (Optional) Deprecated Automatically create the branch if it could not be found. Defaults to false. Subsequent reads if the branch is deleted will occur from 'autocreate_branch_source_branch'. Use the github_branch resource instead.

  • autocreate_branch_source_branch - (Optional) Deprecated The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'. Use the github_branch resource instead.

  • autocreate_branch_source_sha - (Optional) Deprecated The commit hash to start from, if 'autocreate_branch' is set. Defaults to the tip of 'autocreate_branch_source_branch'. If provided, 'autocreate_branch_source_branch' is ignored. Use the github_branch resource instead.

Attributes Reference

The following additional attributes are exported:

  • commit_sha - The SHA of the commit that modified the file.

  • repository_id - The ID of the repository.

  • sha - The SHA blob of the file.

  • ref - The name of the commit/branch/tag.

Import

Repository files can be imported using a combination of the repo, file and branch or empty branch for the default branch, e.g.

terraform import github_repository_file.gitignore example:.gitignore:feature-branch

and using default branch:

terraform import github_repository_file.gitignore example:.gitignore: