From 8488e9502a66c4e852f028c584b7b23dc01b1f0f Mon Sep 17 00:00:00 2001 From: Aram Karapetyan Date: Mon, 12 Feb 2024 10:18:30 +0400 Subject: [PATCH 1/3] feat(DMVP-3484): Fix publish problem --- modules/deployments/README.md | 92 +++++++++++++++++++++ modules/deployments/deployment-veriables.tf | 9 ++ modules/deployments/deployment.tf | 7 ++ modules/deployments/locals.tf | 12 +++ modules/deployments/output.tf | 3 + modules/deployments/variables.tf | 15 ++++ modules/deployments/versions.tf | 8 ++ 7 files changed, 146 insertions(+) create mode 100644 modules/deployments/README.md create mode 100644 modules/deployments/deployment-veriables.tf create mode 100644 modules/deployments/deployment.tf create mode 100644 modules/deployments/locals.tf create mode 100644 modules/deployments/output.tf create mode 100644 modules/deployments/variables.tf create mode 100644 modules/deployments/versions.tf diff --git a/modules/deployments/README.md b/modules/deployments/README.md new file mode 100644 index 0000000..a23a688 --- /dev/null +++ b/modules/deployments/README.md @@ -0,0 +1,92 @@ +# Terraform module to manage repository `Deployments` + +## About + +--- +Deployment is environment which has its own variables to use in pipelines + +## Provider configuration + +--- +```hcl +terraform { + required_providers { + bitbucket = { + source = "DrFaust92/bitbucket" + version = "2.30.2" + } + } +} +``` +## Usage + +--- +```hcl +module "deployments" { + + source = "./deployments" + deployments = local.deployments + repository_id = "my-repository-xxxx-id" + +} + +locals { + deployments = [ + { + name = "development1" + stage = "Test" + variables = { + "key1" = ["value1", "secured"] + "key2" = ["value2", "secured"] + } + }, + + { + name = "development1" + stage = "Test" + variables = { + "key1" = ["value1", "secured"] + "key2" = ["value2"] + } + } + + ] +} +``` + +## Requirements + +| Name | Version | +|------|---------| +| [bitbucket](#requirement\_bitbucket) | 2.31.0 | + +## Providers + +| Name | Version | +|------|---------| +| [bitbucket](#provider\_bitbucket) | 2.31.0 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [bitbucket_deployment.depl](https://registry.terraform.io/providers/DrFaust92/bitbucket/2.31.0/docs/resources/deployment) | resource | +| [bitbucket_deployment_variable.depl-vars](https://registry.terraform.io/providers/DrFaust92/bitbucket/2.31.0/docs/resources/deployment_variable) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [deployments](#input\_deployments) | n/a |
list(object({
name = string
stage = string
variables = list(object({
name = string
value = string
secured = bool
}))
}))
| n/a | yes | +| [repository\_id](#input\_repository\_id) | n/a | `string` | n/a | yes | + +## Outputs + +| Name | Description | +|------|-------------| +| [variables](#output\_variables) | n/a | + diff --git a/modules/deployments/deployment-veriables.tf b/modules/deployments/deployment-veriables.tf new file mode 100644 index 0000000..778eb6a --- /dev/null +++ b/modules/deployments/deployment-veriables.tf @@ -0,0 +1,9 @@ +resource "bitbucket_deployment_variable" "depl-vars" { + + for_each = { for item in local.variables_flat : "${item.deploy}-${item.variable.key}" => item } + + deployment = bitbucket_deployment.depl[each.value.deploy].id + key = each.value.variable.key + value = each.value.variable.value + secured = each.value.variable.secured +} diff --git a/modules/deployments/deployment.tf b/modules/deployments/deployment.tf new file mode 100644 index 0000000..157ca71 --- /dev/null +++ b/modules/deployments/deployment.tf @@ -0,0 +1,7 @@ +resource "bitbucket_deployment" "depl" { + for_each = { for deployment in var.deployments : deployment.name => deployment } + + repository = var.repository_id + name = each.value.name + stage = each.value.stage +} diff --git a/modules/deployments/locals.tf b/modules/deployments/locals.tf new file mode 100644 index 0000000..4d7a206 --- /dev/null +++ b/modules/deployments/locals.tf @@ -0,0 +1,12 @@ +locals { + variables_flat = flatten([for depl in var.deployments : [ + for variable in depl.variables : { + deploy : depl.name + variable : { + key : variable.name, + value : variable.value, + secured : variable.secured + } + } + ]]) +} diff --git a/modules/deployments/output.tf b/modules/deployments/output.tf new file mode 100644 index 0000000..11aaf81 --- /dev/null +++ b/modules/deployments/output.tf @@ -0,0 +1,3 @@ +output "variables" { + value = local.variables_flat +} diff --git a/modules/deployments/variables.tf b/modules/deployments/variables.tf new file mode 100644 index 0000000..2505ef1 --- /dev/null +++ b/modules/deployments/variables.tf @@ -0,0 +1,15 @@ +variable "deployments" { + type = list(object({ + name = string + stage = string + variables = list(object({ + name = string + value = string + secured = bool + })) + })) +} + +variable "repository_id" { + type = string +} diff --git a/modules/deployments/versions.tf b/modules/deployments/versions.tf new file mode 100644 index 0000000..e74431d --- /dev/null +++ b/modules/deployments/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + bitbucket = { + source = "DrFaust92/bitbucket" + version = "2.31.0" + } + } +} From 0424d2beac3a48ac8410b17cb6f0af2d0f12f50a Mon Sep 17 00:00:00 2001 From: Aram Karapetyan Date: Mon, 12 Feb 2024 10:32:53 +0400 Subject: [PATCH 2/3] feat(DMVP-3484): Fix publish problem --- .../deployments/tests/full-example/README.md | 29 ++++++++++++ .../deployments/tests/full-example/main.tf | 46 +++++++++++++++++++ .../tests/minimal-example/README.md | 29 ++++++++++++ .../deployments/tests/minimal-example/main.tf | 6 +++ 4 files changed, 110 insertions(+) create mode 100644 modules/deployments/tests/full-example/README.md create mode 100644 modules/deployments/tests/full-example/main.tf create mode 100644 modules/deployments/tests/minimal-example/README.md create mode 100644 modules/deployments/tests/minimal-example/main.tf diff --git a/modules/deployments/tests/full-example/README.md b/modules/deployments/tests/full-example/README.md new file mode 100644 index 0000000..5d74c64 --- /dev/null +++ b/modules/deployments/tests/full-example/README.md @@ -0,0 +1,29 @@ +# full-example + + +## Requirements + +No requirements. + +## Providers + +No providers. + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [bitbucket](#module\_bitbucket) | ../ | n/a | + +## Resources + +No resources. + +## Inputs + +No inputs. + +## Outputs + +No outputs. + diff --git a/modules/deployments/tests/full-example/main.tf b/modules/deployments/tests/full-example/main.tf new file mode 100644 index 0000000..fade1df --- /dev/null +++ b/modules/deployments/tests/full-example/main.tf @@ -0,0 +1,46 @@ +module "bitbucket" { + source = "../" + + name = "Repository-terraformed" + owner = "DevOps" + description = "Repository description" + website = "www.dasmeta.com" + language = "python" + fork_policy = "allow_forks" + + project = { + create = true + name = "Project1" + key = "project1" + is_private = false + } + + restrictions = { + action = "push" + branch = "master" + } + + deployments = [ + { + name = "development1" + stage = "Test" + variables = [ + { name = "key1", value = "value1", secured = true }, + { name = "key2", value = "value2", secured = false } + ] + }, + { + name = "development2" + stage = "Test" + variables = [ + { name = "key1", value = "value1", secured = true }, + { name = "key2", value = "value2", secured = false } + ] + }, + ] + + repository_variables = [ + { name = "key1", value = "value1", secured = true }, + { name = "key2", value = "value2", secured = false } + ] +} diff --git a/modules/deployments/tests/minimal-example/README.md b/modules/deployments/tests/minimal-example/README.md new file mode 100644 index 0000000..cb57919 --- /dev/null +++ b/modules/deployments/tests/minimal-example/README.md @@ -0,0 +1,29 @@ +# minimal-example + + +## Requirements + +No requirements. + +## Providers + +No providers. + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [bitbucket](#module\_bitbucket) | ../ | n/a | + +## Resources + +No resources. + +## Inputs + +No inputs. + +## Outputs + +No outputs. + diff --git a/modules/deployments/tests/minimal-example/main.tf b/modules/deployments/tests/minimal-example/main.tf new file mode 100644 index 0000000..cb95aec --- /dev/null +++ b/modules/deployments/tests/minimal-example/main.tf @@ -0,0 +1,6 @@ +module "bitbucket" { + source = "../" + + name = "Repository-terraformed" + owner = "DevOps" +} From e12fcec15f2aa47a36f8c0733792ead1e09d6fc3 Mon Sep 17 00:00:00 2001 From: Aram Karapetyan Date: Mon, 12 Feb 2024 11:22:50 +0400 Subject: [PATCH 3/3] feat(DMVP-3484): Fix publish problem --- modules/deployments/deployment-veriables.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/deployments/deployment-veriables.tf b/modules/deployments/deployment-veriables.tf index 778eb6a..f3b3498 100644 --- a/modules/deployments/deployment-veriables.tf +++ b/modules/deployments/deployment-veriables.tf @@ -1,5 +1,4 @@ resource "bitbucket_deployment_variable" "depl-vars" { - for_each = { for item in local.variables_flat : "${item.deploy}-${item.variable.key}" => item } deployment = bitbucket_deployment.depl[each.value.deploy].id