diff --git a/fast/extras/0-cicd-github/README.md b/fast/extras/0-cicd-github/README.md index 0bd0b5be..fc4a30c6 100644 --- a/fast/extras/0-cicd-github/README.md +++ b/fast/extras/0-cicd-github/README.md @@ -116,8 +116,27 @@ Initial population depends on a modules repository being configured in the `modu ### Commit configuration -Finally, a `commit_config` variable is optional: it can be used to configure author, email and message used in commits for initial population of files, its defaults are probably fine for most use cases. +An optional variable `commit_config` can be used to configure the author, email, and message used in commits for the initial population of files. Its defaults are probably fine for most use cases. +### Pull Request configuration + +An optional variable `pull_request_config` can be used to configure the title, body, head_ref, and base_ref of the pull request created for the initial population or update of files. By default, no pull request is created. To create a pull request, set the `create` attribute to `true`. `base_ref` defaults to `main` and `head_ref` to the head branch name. If the head branch does not exist, it will be created from the base_ref branch. + +```hcl +pull_request_config = { + create = true + title = "FAST: initial loading or update" + body = "" + base_ref = "main" + head_ref = "fast-loader" +} +# tftest skip +``` + +To start using a pull request workflow, if the initial loading was created without a pull request in the past, please use the following command to delete the actual branch files from the Terraform state to keep it in the current state: +```bash +terraform state list | grep github_repository_file | awk '{print "terraform state rm '\''"$1"'\''"}' +``` @@ -126,7 +145,7 @@ Finally, a `commit_config` variable is optional: it can be used to configure aut | name | description | resources | |---|---|---| | [cicd-versions.tf](./cicd-versions.tf) | Provider version. | | -| [main.tf](./main.tf) | Module-level locals and resources. | github_actions_secret · github_repository · github_repository_deploy_key · github_repository_file · tls_private_key | +| [main.tf](./main.tf) | Module-level locals and resources. | github_actions_secret · github_branch · github_repository · github_repository_deploy_key · github_repository_file · github_repository_pull_request · tls_private_key | | [outputs.tf](./outputs.tf) | Module outputs. | | | [providers.tf](./providers.tf) | Provider configuration. | | | [variables.tf](./variables.tf) | Module variables. | | @@ -138,7 +157,8 @@ Finally, a `commit_config` variable is optional: it can be used to configure aut | [organization](variables.tf#L51) | GitHub organization. | string | ✓ | | | [commmit_config](variables.tf#L17) | Configure commit metadata. | object({…}) | | {} | | [modules_config](variables.tf#L28) | Configure access to repository module via key, and replacement for modules sources in stage repositories. | object({…}) | | null | -| [repositories](variables.tf#L56) | Repositories to create. | map(object({…})) | | {} | +| [pull_request_config](variables.tf#L56) | Configure pull request metadata. | object({…}) | | {} | +| [repositories](variables.tf#L69) | Repositories to create. | map(object({…})) | | {} | ## Outputs diff --git a/fast/extras/0-cicd-github/main.tf b/fast/extras/0-cicd-github/main.tf index 81739cc6..37607435 100644 --- a/fast/extras/0-cicd-github/main.tf +++ b/fast/extras/0-cicd-github/main.tf @@ -136,10 +136,21 @@ resource "github_actions_secret" "default" { ) } +resource "github_branch" "default" { + for_each = ( + try(var.pull_request_config.create, null) == true + ? local.repositories + : {} + ) + repository = each.key + branch = var.pull_request_config.head_ref + source_branch = var.pull_request_config.base_ref +} + resource "github_repository_file" "default" { for_each = local.modules_repo == null ? {} : local.repository_files repository = local.repositories[each.value.repository] - branch = "main" + branch = try(var.pull_request_config.head_ref, "main") file = each.value.name content = ( endswith(each.value.name, ".tf") && local.modules_repo != null @@ -161,3 +172,16 @@ resource "github_repository_file" "default" { ] } } + +resource "github_repository_pull_request" "default" { + for_each = ( + try(var.pull_request_config.create, null) == true + ? local.repositories + : {} + ) + base_repository = each.key + title = var.pull_request_config.title + body = var.pull_request_config.body + base_ref = var.pull_request_config.base_ref + head_ref = var.pull_request_config.head_ref +} diff --git a/fast/extras/0-cicd-github/variables.tf b/fast/extras/0-cicd-github/variables.tf index ea378ee7..63854ffb 100644 --- a/fast/extras/0-cicd-github/variables.tf +++ b/fast/extras/0-cicd-github/variables.tf @@ -53,6 +53,19 @@ variable "organization" { type = string } +variable "pull_request_config" { + description = "Configure pull request metadata." + type = object({ + create = optional(bool, false) + title = optional(string, "FAST: initial loading or update") + body = optional(string, "") + base_ref = optional(string, "main") + head_ref = optional(string, "fast-loader") + }) + default = {} + nullable = false +} + variable "repositories" { description = "Repositories to create." type = map(object({