From 5cf60cbcf478ae3433dc28d4f20a14ba46ff42de Mon Sep 17 00:00:00 2001
From: Anton KOVACH <2207136+antonkovach@users.noreply.github.com>
Date: Sun, 19 Feb 2023 18:01:38 +0100
Subject: [PATCH] Fix Terraform formatting and add module_prefix attribute to
modules_config (#1162)
* Fix Terraform formatting and add module/ prefix to path in 0-cicd-github repository population
fix the formatting of Terraform files and adds the module/ prefix to the module path in 0-cicd-github under repository population. Without proper formatting and module path, generated repositories may show formatting mismatches and examples in the README.md file may not run as expected.
The changes include updating the replace function with a new regular expression pattern to correctly apply the git source for modules and updating the each.value.file attribute to include the module/ prefix in the Terraform file path. This ensures that the examples in the README.md file work as intended and that the generated repositories follow best practices for Terraform code.
* revert modules/ prefix change
* Add module_prefix to modules_config
- Add module_prefix to modules_config
- Add example to Readme.md
- use module_prefix variable to specify the path
* fix tfdoc
---
fast/extras/0-cicd-github/README.md | 16 +++++++++++++---
fast/extras/0-cicd-github/main.tf | 5 +++--
fast/extras/0-cicd-github/variables.tf | 1 +
3 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/fast/extras/0-cicd-github/README.md b/fast/extras/0-cicd-github/README.md
index 58407b5e4e..0bd0b5be1d 100644
--- a/fast/extras/0-cicd-github/README.md
+++ b/fast/extras/0-cicd-github/README.md
@@ -39,6 +39,16 @@ modules_config = {
# tftest skip
```
+If the modules are located in a non modules only repository, use the module_prefix attribute to set the location of your modules within the repository:
+
+```hcl
+modules_config = {
+ repository_name = "GoogleCloudPlatform/cloud-foundation-fabric"
+ module_prefix = "modules/"
+}
+# tftest skip
+```
+
In the above example, no key options are set so it's assumed modules will be fetched from a public repository. If modules repository authentication is needed the `key_config` attribute also needs to be set.
If no keypair path is specified an internally generated key will be stored as an access key in the modules repository, and as secrets in the stage repositories:
@@ -125,10 +135,10 @@ Finally, a `commit_config` variable is optional: it can be used to configure aut
| name | description | type | required | default |
|---|---|:---:|:---:|:---:|
-| [organization](variables.tf#L50) | GitHub organization. | string
| ✓ | |
+| [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#L55) | Repositories to create. | map(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({…}))
| | {}
|
## Outputs
diff --git a/fast/extras/0-cicd-github/main.tf b/fast/extras/0-cicd-github/main.tf
index d91ab970c5..9cf319bbf7 100644
--- a/fast/extras/0-cicd-github/main.tf
+++ b/fast/extras/0-cicd-github/main.tf
@@ -18,6 +18,7 @@ locals {
_repository_files = flatten([
for k, v in var.repositories : [
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}/*.tf") : f]
) : {
@@ -143,8 +144,8 @@ resource "github_repository_file" "default" {
endswith(each.value.name, ".tf") && local.modules_repo != null
? replace(
file(each.value.file),
- "/source\\s*=\\s*\"../../../modules/([^/\"]+)\"/",
- "source = \"git@github.com:${local.modules_repo}.git//$1${local.modules_ref}\"" # "
+ "/source(\\s*)=\\s*\"../../../modules/([^/\"]+)\"/",
+ "source$1= \"git@github.com:${local.modules_repo}.git//${local.module_prefix}$2${local.modules_ref}\"" # "
)
: file(each.value.file)
)
diff --git a/fast/extras/0-cicd-github/variables.tf b/fast/extras/0-cicd-github/variables.tf
index 8e5d0832ff..ea378ee789 100644
--- a/fast/extras/0-cicd-github/variables.tf
+++ b/fast/extras/0-cicd-github/variables.tf
@@ -30,6 +30,7 @@ variable "modules_config" {
type = object({
repository_name = string
source_ref = optional(string)
+ module_prefix = optional(string, "")
key_config = optional(object({
create_key = optional(bool, false)
create_secrets = optional(bool, false)