From 6e9f657c0c9e29ed33a5d5e1d4f478bc6b01fe70 Mon Sep 17 00:00:00 2001 From: Melekhin Anton Date: Fri, 5 Apr 2024 18:02:02 +0400 Subject: [PATCH 1/3] add `default_branch` --- README.md | 4 ++++ main.tf | 28 ++++++++++++++++++++++++++++ outputs.tf | 18 +++++++++++++----- variables.tf | 16 ++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 644ef1b..57b9b07 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ No modules. | Name | Type | |------|------| | [github_actions_secret.this](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/actions_secret) | 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 @@ -42,6 +44,8 @@ No modules. | [archive\_on\_destroy](#input\_archive\_on\_destroy) | Set to `true` to archive the repository instead of deleting on destroy. | `bool` | `false` | no | | [archived](#input\_archived) | Specifies if the repository should be archived. | `bool` | `false` | no | | [auto\_init](#input\_auto\_init) | Set to `true` to produce an initial commit in the repository. | `bool` | `false` | no | +| [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 | +| [default\_branch\_rename](#input\_default\_branch\_rename) | Indicate if it should rename the branch rather than use an existing branch. | `bool` | `false` | no | | [delete\_branch\_on\_merge](#input\_delete\_branch\_on\_merge) | Automatically delete head branch after a pull request is merged. | `bool` | `false` | no | | [description](#input\_description) | A description of the repository. | `string` | `null` | no | | [gitignore\_template](#input\_gitignore\_template) | Use the name of the template without the extension. For example, 'Haskell'. | `string` | `null` | no | diff --git a/main.tf b/main.tf index 294cfb6..1ee2753 100644 --- a/main.tf +++ b/main.tf @@ -41,6 +41,34 @@ 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 + ] +} + ################################################################ # Actions secret ################################################################ diff --git a/outputs.tf b/outputs.tf index 5792ddc..b0983e5 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,3 +1,7 @@ +################################################################ +# Repository +################################################################ + output "name" { description = "The name of the repository." value = github_repository.this.name @@ -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 = try(github_branch.default[0].branch, var.default_branch) +} diff --git a/variables.tf b/variables.tf index 45e9e8f..f2e8aac 100644 --- a/variables.tf +++ b/variables.tf @@ -169,6 +169,22 @@ 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 branch." + type = bool + default = false +} + ################################################################ # Actions secret variables ################################################################ From b72cb63258efc569ad56315353589e2a3877320b Mon Sep 17 00:00:00 2001 From: Melekhin Anton Date: Fri, 5 Apr 2024 19:44:34 +0400 Subject: [PATCH 2/3] fix value for `default_branch` output --- outputs.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/outputs.tf b/outputs.tf index b0983e5..042b0c9 100644 --- a/outputs.tf +++ b/outputs.tf @@ -42,5 +42,5 @@ output "repo_id" { output "default_branch" { description = "The name of the default branch of the repository." - value = try(github_branch.default[0].branch, var.default_branch) + value = var.default_branch } From 7a5e2a2ded2cdfa7c6e2e629740691423d932deb Mon Sep 17 00:00:00 2001 From: Melekhin Anton Date: Fri, 5 Apr 2024 19:45:38 +0400 Subject: [PATCH 3/3] add resource for create additional branches --- README.md | 4 +++- main.tf | 17 +++++++++++++++++ variables.tf | 8 +++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 57b9b07..4475722 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ 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 | @@ -44,8 +45,9 @@ No modules. | [archive\_on\_destroy](#input\_archive\_on\_destroy) | Set to `true` to archive the repository instead of deleting on destroy. | `bool` | `false` | no | | [archived](#input\_archived) | Specifies if the repository should be archived. | `bool` | `false` | no | | [auto\_init](#input\_auto\_init) | Set to `true` to produce an initial commit in the repository. | `bool` | `false` | no | +| [branches](#input\_branches) | The list of a branches of the repository. Branches can only be created on non-empty repositories. | `list(map(string))` | `[]` | no | | [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 | -| [default\_branch\_rename](#input\_default\_branch\_rename) | Indicate if it should rename the branch rather than use an existing branch. | `bool` | `false` | no | +| [default\_branch\_rename](#input\_default\_branch\_rename) | Indicate if it should rename the branch rather than use an existing default branch. | `bool` | `false` | no | | [delete\_branch\_on\_merge](#input\_delete\_branch\_on\_merge) | Automatically delete head branch after a pull request is merged. | `bool` | `false` | no | | [description](#input\_description) | A description of the repository. | `string` | `null` | no | | [gitignore\_template](#input\_gitignore\_template) | Use the name of the template without the extension. For example, 'Haskell'. | `string` | `null` | no | diff --git a/main.tf b/main.tf index 1ee2753..3c7a46e 100644 --- a/main.tf +++ b/main.tf @@ -69,6 +69,23 @@ resource "github_branch_default" "this" { ] } +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 ################################################################ diff --git a/variables.tf b/variables.tf index f2e8aac..ece514f 100644 --- a/variables.tf +++ b/variables.tf @@ -180,11 +180,17 @@ variable "default_branch" { } variable "default_branch_rename" { - description = "Indicate if it should rename the branch rather than use an existing branch." + 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 ################################################################