Skip to content

Commit

Permalink
Initial release (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiendelpierre authored Oct 8, 2021
2 parents 0428bfb + b0efcd2 commit 7e51d9c
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 19 deletions.
102 changes: 94 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,112 @@
# Name of the Terraform module
# terraform-github-tfc-workspace-module

Short description of the module
Creates an empty GitHub repository (blank or from a specified template) and a corresponding workspace in Terraform Cloud.

## What this does
## Requirements

Lorem ipsum
After setting up the GitHub repo + TFC workspace, you must set a secret in your GitHub repo called `TFC_API_TOKEN` and set its value to a personal token obtained from Terraform Cloud. This is required for the GitHub Action that runs Terraform on your behalf.

## Inputs and outputs

<!--- BEGIN_TF_DOCS --->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0.0 |
| <a name="requirement_github"></a> [github](#requirement\_github) | >= 4.1.0 |
| <a name="requirement_tfe"></a> [tfe](#requirement\_tfe) | >= 0.26.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_github"></a> [github](#provider\_github) | >= 4.1.0 |
| <a name="provider_tfe"></a> [tfe](#provider\_tfe) | >= 0.26.1 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [github_repository.blank](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) | resource |
| [github_repository.templated](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) | resource |
| [github_repository_file.blank_providers_tf](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file) | resource |
| [github_repository_file.templated_providers_tf](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_file) | resource |
| [tfe_workspace.main](https://registry.terraform.io/providers/hashicorp/tfe/latest/docs/resources/workspace) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_github_repo_description"></a> [github\_repo\_description](#input\_github\_repo\_description) | A description for the GitHub repo | `string` | `""` | no |
| <a name="input_github_repo_enable_issues"></a> [github\_repo\_enable\_issues](#input\_github\_repo\_enable\_issues) | value | `bool` | `true` | no |
| <a name="input_github_repo_enable_projects"></a> [github\_repo\_enable\_projects](#input\_github\_repo\_enable\_projects) | value | `bool` | `false` | no |
| <a name="input_github_repo_enable_wiki"></a> [github\_repo\_enable\_wiki](#input\_github\_repo\_enable\_wiki) | value | `bool` | `false` | no |
| <a name="input_github_repo_gitignore_template"></a> [github\_repo\_gitignore\_template](#input\_github\_repo\_gitignore\_template) | A gitignore template to preinstall in the GitHub repo | `string` | `"Terraform"` | no |
| <a name="input_github_repo_license_template"></a> [github\_repo\_license\_template](#input\_github\_repo\_license\_template) | The license to apply to the GitHub repository | `string` | `"unlicense"` | no |
| <a name="input_github_repo_name"></a> [github\_repo\_name](#input\_github\_repo\_name) | The name of the GitHub repo | `string` | n/a | yes |
| <a name="input_github_repo_template_owner_name"></a> [github\_repo\_template\_owner\_name](#input\_github\_repo\_template\_owner\_name) | The owner organization of the template repo to use to create this repo | `string` | `""` | no |
| <a name="input_github_repo_template_repo_name"></a> [github\_repo\_template\_repo\_name](#input\_github\_repo\_template\_repo\_name) | The name of the template repo to use to create this repo | `string` | `""` | no |
| <a name="input_github_repo_use_template"></a> [github\_repo\_use\_template](#input\_github\_repo\_use\_template) | Use a template to create the GitHub repo | `bool` | `true` | no |
| <a name="input_github_repo_visibility"></a> [github\_repo\_visibility](#input\_github\_repo\_visibility) | The GitHub repo's visibility (public, private, or internal) | `string` | `"public"` | no |
| <a name="input_tfc_organization_name"></a> [tfc\_organization\_name](#input\_tfc\_organization\_name) | The name of the Terraform Cloud organization to which the workspace belongs | `string` | n/a | yes |
| <a name="input_tfc_workspace_name"></a> [tfc\_workspace\_name](#input\_tfc\_workspace\_name) | The name of the workspace in Terraform Cloud | `string` | n/a | yes |
| <a name="input_tfc_workspace_terraform_version"></a> [tfc\_workspace\_terraform\_version](#input\_tfc\_workspace\_terraform\_version) | The version of Terraform to use during the workspace's runs | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_github_repo_full_name"></a> [github\_repo\_full\_name](#output\_github\_repo\_full\_name) | n/a |
| <a name="output_github_templated_repo_full_name"></a> [github\_templated\_repo\_full\_name](#output\_github\_templated\_repo\_full\_name) | n/a |
| <a name="output_tfc_workspace_id"></a> [tfc\_workspace\_id](#output\_tfc\_workspace\_id) | n/a |
| <a name="output_tfc_workspace_name"></a> [tfc\_workspace\_name](#output\_tfc\_workspace\_name) | n/a |

<!--- END_TF_DOCS --->

## Code example

```hcl
terraform {
...
required_version = "~> 1.0"
required_providers {
github = {
source = "integrations/github"
version = "~> 4.1"
}
tfe = {
source = "hashicorp/tfe"
version = "~> 0.26"
}
}
}
module "this_module" {
source = "./"
...
provider "github" {
token = "..."
}
provider "tfe" {
token = "..."
}
# module "test" {
# source = "github.com/fabiendelpierre/terraform-github-tfc-workspace-module"
# github_repo_name = "foo"
# github_repo_description = "..."
# github_repo_use_template = true
# github_repo_template_owner_name = "owner-org"
# github_repo_template_repo_name = "template-repo-name"
# tfc_workspace_name = "foo"
# tfc_organization_name = "tfc-org-name"
# tfc_workspace_terraform_version = "1.0.7"
# }
```
82 changes: 78 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
resource "random_string" "random" {
length = 16
special = true
override_special = "/@£$"
resource "github_repository" "blank" {
count = (var.github_repo_use_template == true ? 0 : 1)

name = var.github_repo_name
description = var.github_repo_description
visibility = var.github_repo_visibility

has_issues = var.github_repo_enable_issues
has_projects = var.github_repo_enable_projects
has_wiki = var.github_repo_enable_wiki

gitignore_template = var.github_repo_gitignore_template
license_template = var.github_repo_license_template
}

resource "github_repository" "templated" {
count = (var.github_repo_use_template == true ? 1 : 0)

name = var.github_repo_name
description = var.github_repo_description
visibility = var.github_repo_visibility

template {
owner = var.github_repo_template_owner_name
repository = var.github_repo_template_repo_name
}
}

resource "github_repository_file" "blank_providers_tf" {
count = (var.github_repo_use_template == true ? 0 : 1)

repository = github_repository.blank[0].name
branch = github_repository.blank[0].default_branch
file = "providers.tf"

commit_message = "Create providers.tf with names of TFC org and workspace"
commit_author = "Terraform Cloud"
commit_email = "[email protected]"

overwrite_on_create = true

content = templatefile("${path.module}/providers.tf.tpl", {
tfe_organization_name = var.tfc_organization_name
tfe_workspace_name = var.tfc_workspace_name
})

lifecycle {
ignore_changes = [content]
}
}

resource "github_repository_file" "templated_providers_tf" {
count = (var.github_repo_use_template == true ? 1 : 0)

repository = github_repository.templated[0].name
branch = github_repository.templated[0].default_branch
file = "providers.tf"

commit_message = "Update providers.tf with names of TFC org and workspace"
commit_author = "Terraform Cloud"
commit_email = "[email protected]"

overwrite_on_create = true

content = templatefile("${path.module}/providers.tf.tpl", {
tfe_organization_name = var.tfc_organization_name
tfe_workspace_name = var.tfc_workspace_name
})

lifecycle {
ignore_changes = [content]
}
}

resource "tfe_workspace" "main" {
name = var.tfc_workspace_name
organization = var.tfc_organization_name
terraform_version = var.tfc_workspace_terraform_version
}
16 changes: 14 additions & 2 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
output "foo" {
value = random_string.random.result
output "github_repo_full_name" {
value = github_repository.blank.*.full_name
}

output "github_templated_repo_full_name" {
value = github_repository.templated.*.full_name
}

output "tfc_workspace_name" {
value = tfe_workspace.main.name
}

output "tfc_workspace_id" {
value = tfe_workspace.main.id
}
10 changes: 7 additions & 3 deletions providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ terraform {
required_version = ">= 1.0.0"

required_providers {
provider = {
source = "hashicorp/random"
version = ">= 3.0.0"
github = {
source = "integrations/github"
version = ">= 4.1.0"
}
tfe = {
source = "hashicorp/tfe"
version = ">= 0.26.1"
}
}
}
18 changes: 18 additions & 0 deletions providers.tf.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_version = "~> 1.0"
required_providers {
provider = {
source = "hashicorp/random"
version = "~> 3.0"
}
}

backend "remote" {
organization = "${tfe_organization_name}"
workspaces {
name = "${tfe_workspace_name}"
}
}
}
84 changes: 82 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,84 @@
variable "foo" {
description = "..."
variable "github_repo_name" {
description = "The name of the GitHub repo"
type = string
}

variable "github_repo_description" {
description = "A description for the GitHub repo"
type = string
default = ""
}

variable "github_repo_visibility" {
description = "The GitHub repo's visibility (public, private, or internal)"
type = string
default = "public"

validation {
condition = contains(["public", "private", "internal"], var.github_repo_visibility)
error_message = "The variable github_repo_visibility must be set to 'public', 'private', or 'internal'."
}
}

variable "github_repo_enable_issues" {
description = "value"
type = bool
default = true
}

variable "github_repo_enable_projects" {
description = "value"
type = bool
default = false
}

variable "github_repo_enable_wiki" {
description = "value"
type = bool
default = false
}

variable "github_repo_gitignore_template" {
description = "A gitignore template to preinstall in the GitHub repo"
type = string
default = "Terraform"
}

variable "github_repo_license_template" {
description = "The license to apply to the GitHub repository"
type = string
default = "unlicense"
}

variable "github_repo_use_template" {
description = "Use a template to create the GitHub repo"
type = bool
default = true
}

variable "github_repo_template_owner_name" {
description = "The owner organization of the template repo to use to create this repo"
type = string
default = ""
}

variable "github_repo_template_repo_name" {
description = "The name of the template repo to use to create this repo"
type = string
default = ""
}

variable "tfc_workspace_name" {
description = "The name of the workspace in Terraform Cloud"
type = string
}

variable "tfc_organization_name" {
description = "The name of the Terraform Cloud organization to which the workspace belongs"
type = string
}

variable "tfc_workspace_terraform_version" {
description = "The version of Terraform to use during the workspace's runs"
type = string
}

0 comments on commit 7e51d9c

Please sign in to comment.