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..f3b3498 --- /dev/null +++ b/modules/deployments/deployment-veriables.tf @@ -0,0 +1,8 @@ +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/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" +} 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" + } + } +}