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

feat: added variables and appropriate resources that creates branches #7

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ No modules.
| Name | Type |
|------|------|
| [github_actions_secret.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_secret) | resource |
| [github_branch.additional](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch) | resource |
| [github_branch.default](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch) | resource |
| [github_branch_default.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_default) | resource |
| [github_repository.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) | resource |

## Inputs
Expand All @@ -42,6 +45,9 @@ No modules.
| <a name="input_archive_on_destroy"></a> [archive\_on\_destroy](#input\_archive\_on\_destroy) | Set to `true` to archive the repository instead of deleting on destroy. | `bool` | `false` | no |
| <a name="input_archived"></a> [archived](#input\_archived) | Specifies if the repository should be archived. | `bool` | `false` | no |
| <a name="input_auto_init"></a> [auto\_init](#input\_auto\_init) | Set to `true` to produce an initial commit in the repository. | `bool` | `false` | no |
| <a name="input_branches"></a> [branches](#input\_branches) | The list of a branches of the repository. Branches can only be created on non-empty repositories. | `list(map(string))` | `[]` | no |
| <a name="input_default_branch"></a> [default\_branch](#input\_default\_branch) | The name of the default branch of the repository. Branches can only be created on non-empty repositories. | `string` | `"main"` | no |
| <a name="input_default_branch_rename"></a> [default\_branch\_rename](#input\_default\_branch\_rename) | Indicate if it should rename the branch rather than use an existing default branch. | `bool` | `false` | no |
| <a name="input_delete_branch_on_merge"></a> [delete\_branch\_on\_merge](#input\_delete\_branch\_on\_merge) | Automatically delete head branch after a pull request is merged. | `bool` | `false` | no |
| <a name="input_description"></a> [description](#input\_description) | A description of the repository. | `string` | `null` | no |
| <a name="input_gitignore_template"></a> [gitignore\_template](#input\_gitignore\_template) | Use the name of the template without the extension. For example, 'Haskell'. | `string` | `null` | no |
Expand Down
45 changes: 45 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,51 @@ resource "github_repository" "this" {
topics = sort(var.topics)
}

################################################################
# Branches
################################################################

resource "github_branch" "default" {
count = var.default_branch == "main" || var.default_branch_rename == true ? 0 : 1

repository = github_repository.this.name
branch = var.default_branch

depends_on = [
github_repository.this
]
}

resource "github_branch_default" "this" {
count = var.default_branch == "main" ? 0 : 1

repository = github_repository.this.name
branch = var.default_branch
rename = var.default_branch_rename

depends_on = [
github_repository.this,
github_branch.default
]
}

locals {
branches = { for branch in var.branches : branch.name => branch }
}

resource "github_branch" "additional" {
for_each = local.branches

repository = github_repository.this.name
branch = each.key
source_branch = try(each.value.source_branch, null)
source_sha = try(each.value.source_sha, null)

depends_on = [
github_repository.this
]
}

################################################################
# Actions secret
################################################################
Expand Down
18 changes: 13 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
################################################################
# Repository
################################################################

output "name" {
description = "The name of the repository."
value = github_repository.this.name
Expand Down Expand Up @@ -27,12 +31,16 @@ output "git_clone_url" {
value = github_repository.this.git_clone_url
}

output "default_branch" {
description = "The name of the default branch of the repository."
value = github_repository.this.default_branch
}

output "repo_id" {
description = "GitHub ID for the repository."
value = github_repository.this.repo_id
}

################################################################
# Branches
################################################################

output "default_branch" {
description = "The name of the default branch of the repository."
value = var.default_branch
}
22 changes: 22 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ variable "topics" {
default = []
}

################################################################
# Branches
################################################################

variable "default_branch" {
description = "The name of the default branch of the repository. Branches can only be created on non-empty repositories."
type = string
default = "main"
}

variable "default_branch_rename" {
description = "Indicate if it should rename the branch rather than use an existing default branch."
type = bool
default = false
}

variable "branches" {
description = "The list of a branches of the repository. Branches can only be created on non-empty repositories."
type = list(map(string))
default = []
}

################################################################
# Actions secret variables
################################################################
Expand Down
Loading