From 7a53511c9a408a80755ab9c3c6ad30877af50d20 Mon Sep 17 00:00:00 2001 From: Anton KOVACH <2207136+antonkovach@users.noreply.github.com> Date: Sat, 11 Mar 2023 15:27:41 +0100 Subject: [PATCH 1/3] Enable populating of data directory and .sample files and update dependencies The Readme.md files reference the data directory and .sample files, but the code did not allow for their populating. This update enables the copying of the data directory and .sample files, with the data directory being populating as a data.sample directory to prevent overwriting any existing data directory. Additionally, dependencies have been updated by adding the depends_on section to several resources to ensure that the dependencies are in the correct order. This update addresses some states that were not being handled previously. There is a minor known issue with Pull Request creation in the current state of the code. The Pull Request is only created after the first run has occurred. A fix for this issue is currently being worked on and will be addressed in a separate Pull Request. However, this issue does not affect the main functionality of the code. --- fast/extras/0-cicd-github/main.tf | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/fast/extras/0-cicd-github/main.tf b/fast/extras/0-cicd-github/main.tf index 37607435..00a91e18 100644 --- a/fast/extras/0-cicd-github/main.tf +++ b/fast/extras/0-cicd-github/main.tf @@ -20,11 +20,13 @@ locals { for f in concat( [for f in fileset(path.module, "${v.populate_from}/*.svg") : f], [for f in fileset(path.module, "${v.populate_from}/*.md") : f], + [for f in fileset(path.module, "${v.populate_from}/*.sample") : f], + [for f in fileset(path.module, "${v.populate_from}/data/**/*.*") : f], [for f in fileset(path.module, "${v.populate_from}/*.tf") : f] ) : { repository = k file = f - name = replace(f, "${v.populate_from}/", "") + name = replace(replace(f, "${v.populate_from}/", ""), "data/", "data.sample/") } ] if v.populate_from != null ]) @@ -143,14 +145,18 @@ resource "github_branch" "default" { : {} ) repository = each.key - branch = var.pull_request_config.head_ref - source_branch = var.pull_request_config.base_ref + branch = var.pull_request_config.create == true ? var.pull_request_config.head_ref : "main" + source_branch = var.pull_request_config.create == true ? var.pull_request_config.base_ref : "" + + depends_on = [ + github_repository.default + ] } resource "github_repository_file" "default" { for_each = local.modules_repo == null ? {} : local.repository_files repository = local.repositories[each.value.repository] - branch = try(var.pull_request_config.head_ref, "main") + branch = var.pull_request_config.create == true ? var.pull_request_config.head_ref : "main" file = each.value.name content = ( endswith(each.value.name, ".tf") && local.modules_repo != null @@ -171,6 +177,11 @@ resource "github_repository_file" "default" { content, ] } + + depends_on = [ + github_branch.default, + github_repository.default, + ] } resource "github_repository_pull_request" "default" { @@ -184,4 +195,8 @@ resource "github_repository_pull_request" "default" { body = var.pull_request_config.body base_ref = var.pull_request_config.base_ref head_ref = var.pull_request_config.head_ref + + depends_on = [ + github_repository_file.default, + ] } From e344dbc4f4de80d0d09df47f802c89f47323205f Mon Sep 17 00:00:00 2001 From: Anton KOVACH <2207136+antonkovach@users.noreply.github.com> Date: Mon, 13 Mar 2023 20:29:50 +0100 Subject: [PATCH 2/3] Add populate_samples attribute --- fast/extras/0-cicd-github/README.md | 35 +++++++++++++++++++++++++- fast/extras/0-cicd-github/main.tf | 6 ++--- fast/extras/0-cicd-github/variables.tf | 3 ++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/fast/extras/0-cicd-github/README.md b/fast/extras/0-cicd-github/README.md index fc4a30c6..f388c8db 100644 --- a/fast/extras/0-cicd-github/README.md +++ b/fast/extras/0-cicd-github/README.md @@ -114,6 +114,39 @@ The `create_options` repository attribute controls creation: if the attribute is Initial population depends on a modules repository being configured in the `modules_config` variable described in the preceding section and on the`populate_from` attributes in each repository where population is required, which point to the folder holding the files to be committed. +Each repository may contain some sample tfvars and data files that can be used as a starting point for your own files. By default, the samples are not populate. However, you can enable this by setting the `populate_samples` attribute to `true`. Here's an updated example: + +```tfvars +repositories = { + fast_00_bootstrap = { + create_options = { + description = "FAST bootstrap." + features = { + issues = true + } + } + populate_from = "../../stages/0-bootstrap" + populate_samples = true + } + fast_01_resman = { + create_options = { + description = "FAST resource management." + features = { + issues = true + } + } + populate_from = "../../stages/1-resman" + populate_samples = true + } + fast_02_networking = { + populate_from = "../../stages/2-networking-peering" + populate_samples = true + } +} +# tftest skip + +Please note that setting `populate_samples` to `true` will populate the sample files to the repository, potentially overwriting any existing files with the same name. To minimize the risk of overwriting existing files, we populate the original `data` directory to a `data.sample` directory. In any case, be careful when enabling this option and review commit history to check any changes made to the sample files. + ### Commit configuration 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. @@ -158,7 +191,7 @@ terraform state list | grep github_repository_file | awk '{print "terraform stat | [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 | | [pull_request_config](variables.tf#L56) | Configure pull request metadata. | object({…}) | | {} | -| [repositories](variables.tf#L69) | Repositories to create. | map(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 00a91e18..5dabe5e3 100644 --- a/fast/extras/0-cicd-github/main.tf +++ b/fast/extras/0-cicd-github/main.tf @@ -20,13 +20,13 @@ locals { for f in concat( [for f in fileset(path.module, "${v.populate_from}/*.svg") : f], [for f in fileset(path.module, "${v.populate_from}/*.md") : f], - [for f in fileset(path.module, "${v.populate_from}/*.sample") : f], - [for f in fileset(path.module, "${v.populate_from}/data/**/*.*") : f], + (v.populate_samples ? [for f in fileset(path.module, "${v.populate_from}/*.sample") : f] : []), + (v.populate_samples ? [for f in fileset(path.module, "${v.populate_from}/data/**/*.*") : f] : []), [for f in fileset(path.module, "${v.populate_from}/*.tf") : f] ) : { repository = k file = f - name = replace(replace(f, "${v.populate_from}/", ""), "data/", "data.sample/") + name = replace(replace(f, "${v.populate_from}/", ""), (v.populate_samples ? "data/" : ""), (v.populate_samples ? "data.sample/" : "")) } ] if v.populate_from != null ]) diff --git a/fast/extras/0-cicd-github/variables.tf b/fast/extras/0-cicd-github/variables.tf index 63854ffb..88187fc2 100644 --- a/fast/extras/0-cicd-github/variables.tf +++ b/fast/extras/0-cicd-github/variables.tf @@ -93,7 +93,8 @@ variable "repositories" { }), {}) visibility = optional(string, "private") })) - populate_from = optional(string) + populate_from = optional(string) + populate_samples = optional(bool, false) })) default = {} nullable = true From 1355ee4c444c7775b4627c4c76e563e9722459e1 Mon Sep 17 00:00:00 2001 From: Anton KOVACH <2207136+antonkovach@users.noreply.github.com> Date: Wed, 15 Mar 2023 10:07:09 +0100 Subject: [PATCH 3/3] Refactor to avoid explicit dependencies --- fast/extras/0-cicd-github/main.tf | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/fast/extras/0-cicd-github/main.tf b/fast/extras/0-cicd-github/main.tf index 5dabe5e3..35665763 100644 --- a/fast/extras/0-cicd-github/main.tf +++ b/fast/extras/0-cicd-github/main.tf @@ -141,22 +141,18 @@ resource "github_actions_secret" "default" { resource "github_branch" "default" { for_each = ( try(var.pull_request_config.create, null) == true - ? local.repositories + ? github_repository.default : {} ) repository = each.key - branch = var.pull_request_config.create == true ? var.pull_request_config.head_ref : "main" - source_branch = var.pull_request_config.create == true ? var.pull_request_config.base_ref : "" - - depends_on = [ - github_repository.default - ] + 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 = var.pull_request_config.create == true ? var.pull_request_config.head_ref : "main" + branch = var.pull_request_config.create == true ? github_branch.default[each.value.repository].branch : "main" file = each.value.name content = ( endswith(each.value.name, ".tf") && local.modules_repo != null @@ -171,32 +167,21 @@ resource "github_repository_file" "default" { commit_author = var.commmit_config.author commit_email = var.commmit_config.email overwrite_on_create = true - - lifecycle { - ignore_changes = [ - content, - ] - } - - depends_on = [ - github_branch.default, - github_repository.default, - ] } resource "github_repository_pull_request" "default" { for_each = ( try(var.pull_request_config.create, null) == true - ? local.repositories + ? github_repository.default : {} ) 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 + head_ref = github_branch.default[each.key].branch depends_on = [ - github_repository_file.default, + github_repository_file.default ] -} +} \ No newline at end of file