Skip to content

Commit a69542a

Browse files
Create basic module that supports creating repository from the template (#4)
* create new repo based on the template * adding delete branch on merge * adding default branch protection * adding tag protection * adding extra labels * adding example of usage * updating Readme
1 parent f5b7bb8 commit a69542a

File tree

8 files changed

+328
-34
lines changed

8 files changed

+328
-34
lines changed

README.md

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
# terraform-github
1+
<a href="https://www.opsd.io" target="_blank"><img alt="OPSd" src=".github/img/OPSD_logo.svg" width="180px"></a>
2+
3+
Meet **OPSd**. The unique and effortless way of managing cloud infrastructure.
24

3-
<a href="https://www.opsd.io" target="_blank"><img alt="OPSd" src=".github/img/OPSD_logo.svg" width="150px"></a>
5+
Visit our website [www.opsd.io](https://www.opsd.io) for more details.
6+
7+
# terraform-github
48

59
## Introduction
610

7-
A Terraform module for creating and managing GitHub repositories.
11+
A terraform module responsible for creating GitHub repositories from the templates.
812

913
## Usage
1014

11-
The example of module usage.
15+
```
16+
module "terraform-github" {
17+
source = "github.com/opsd-io/terraform-github"
18+
version = ">= 0.1.0"
19+
20+
# Setup basic repository settings
21+
repository_name = "test_repo"
22+
repository_description = "Brief description of the test_repo project."
23+
repository_visibility = "public"
24+
}
25+
```
26+
27+
**IMPORTANT**: Make sure not to pin to master because there may be breaking changes between releases.
28+
29+
You can find more example [here](examples).
1230

1331
## Related modules
1432

15-
The list of related modules.
33+
No related modules.
1634

1735
<!-- BEGIN_TF_DOCS -->
1836
## Requirements
@@ -24,19 +42,43 @@ The list of related modules.
2442

2543
## Providers
2644

27-
No providers.
45+
| Name | Version |
46+
|------|---------|
47+
| <a name="provider_github"></a> [github](#provider\_github) | >= 5.3.0 |
2848

2949
## Modules
3050

3151
No modules.
3252

3353
## Resources
3454

35-
No resources.
55+
| Name | Type |
56+
|------|------|
57+
| [github_branch_protection_v3.main](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/branch_protection_v3) | resource |
58+
| [github_issue_label.breaking](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/issue_label) | resource |
59+
| [github_issue_label.chore](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/issue_label) | resource |
60+
| [github_issue_label.skip_changelog](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/issue_label) | resource |
61+
| [github_repository.main](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository) | resource |
62+
| [github_repository_tag_protection.main](https://registry.terraform.io/providers/integrations/github/latest/docs/resources/repository_tag_protection) | resource |
3663

3764
## Inputs
3865

39-
No inputs.
66+
| Name | Description | Type | Default | Required |
67+
|------|-------------|------|---------|:--------:|
68+
| <a name="input_repository_default_branch"></a> [repository\_default\_branch](#input\_repository\_default\_branch) | The default branch name. | `string` | `"main"` | no |
69+
| <a name="input_repository_delete_branch_on_merge"></a> [repository\_delete\_branch\_on\_merge](#input\_repository\_delete\_branch\_on\_merge) | Automatically delete head branch after a pull request is merged. | `bool` | `true` | no |
70+
| <a name="input_repository_description"></a> [repository\_description](#input\_repository\_description) | Brief description of the project. | `string` | `"test_repo desc"` | no |
71+
| <a name="input_repository_has_issues"></a> [repository\_has\_issues](#input\_repository\_has\_issues) | Enable the GitHub Issues on the repository. | `bool` | `true` | no |
72+
| <a name="input_repository_has_projects"></a> [repository\_has\_projects](#input\_repository\_has\_projects) | Enable the GitHub Project on the repository. | `bool` | `false` | no |
73+
| <a name="input_repository_has_wiki"></a> [repository\_has\_wiki](#input\_repository\_has\_wiki) | Enable the GitHub Wiki on the repository. | `bool` | `false` | no |
74+
| <a name="input_repository_name"></a> [repository\_name](#input\_repository\_name) | The name of the repository. | `string` | `"test_repo"` | no |
75+
| <a name="input_repository_require_code_owner_reviews"></a> [repository\_require\_code\_owner\_reviews](#input\_repository\_require\_code\_owner\_reviews) | Require code owners review before PR can be merged | `bool` | `true` | no |
76+
| <a name="input_repository_require_conversation_resolution"></a> [repository\_require\_conversation\_resolution](#input\_repository\_require\_conversation\_resolution) | Resolve all the comments before PR can be merged | `bool` | `true` | no |
77+
| <a name="input_repository_required_approving_review_count"></a> [repository\_required\_approving\_review\_count](#input\_repository\_required\_approving\_review\_count) | Require N aprovales before PR can be merged | `number` | `1` | no |
78+
| <a name="input_repository_tag_protection_pattern"></a> [repository\_tag\_protection\_pattern](#input\_repository\_tag\_protection\_pattern) | The pattern of the tag to protect. | `string` | `"v*"` | no |
79+
| <a name="input_repository_template_owner"></a> [repository\_template\_owner](#input\_repository\_template\_owner) | The GitHub organization or user the template repository is owned by. | `string` | `"opsd-io"` | no |
80+
| <a name="input_repository_template_repository"></a> [repository\_template\_repository](#input\_repository\_template\_repository) | Name of the (template) repository from which to create the new repository. | `string` | `"terraform-module-template"` | no |
81+
| <a name="input_repository_visibility"></a> [repository\_visibility](#input\_repository\_visibility) | Specify whether the created repository should be private or public. Available options `private` or `public`. | `string` | `"public"` | no |
4082

4183
## Outputs
4284

@@ -45,11 +87,11 @@ No outputs.
4587

4688
## Contributing
4789

48-
[Contributing](CONTRIBUTING.md)
90+
If you are interested in contributing to the project, see see our [guide](CONTRIBUTING.md).
4991

5092
## Support
5193

52-
If you have a problem with the module or want to propose a new feature, you can report it via the project's (Github) issue tracker.
94+
If you have a problem with the module or want to propose a new feature, you can report it via the project's (Github) [issue tracker](https://github.com/opsd-io/terraform-github/issues/new/choose).
5395

5496
If you want to discuss something in person, you can join our community on [Slack](https://join.slack.com/t/opsd-community/signup).
5597

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
terraform 1.3.1
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Create GitHub repo from the tempate
2+
3+
Before you start, you need to create a [GitHub token](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) or use an existing one.
4+
5+
Next, set the environment variable.
6+
7+
```bash
8+
export GITHUB_TOKEN="ghp_your_github_token"
9+
```
10+
11+
Now, you need to initialize terraform.
12+
13+
```shell
14+
terraform init
15+
```
16+
17+
Execute plan command.
18+
19+
```shell
20+
terraform plan
21+
```
22+
23+
and verify what will be created.
24+
25+
The last step is to create the repo
26+
27+
```shell
28+
terrafrorm apply
29+
```
30+
31+
**IMPORTANT**: Please double-check the command output. The vital section can be seen in the example `Plan: 6 to add, 0 to change, 0 to destroy`. Ensure that you understand the changes you are making.
32+
33+
Next, you will be asked
34+
35+
```shell
36+
Do you want to perform these actions?
37+
Terraform will perform the actions described above.
38+
Only 'yes' will be accepted to approve.
39+
40+
Enter a value:
41+
```
42+
43+
Type `yes` to approve and let the magic happen.
44+
45+
<!-- BEGIN_TF_DOCS -->
46+
## Requirements
47+
48+
| Name | Version |
49+
|------|---------|
50+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.1 |
51+
| <a name="requirement_github"></a> [github](#requirement\_github) | >= 5.3.0 |
52+
53+
## Providers
54+
55+
No providers.
56+
57+
## Modules
58+
59+
| Name | Source | Version |
60+
|------|--------|---------|
61+
| <a name="module_terraform-github"></a> [terraform-github](#module\_terraform-github) | ../../ | n/a |
62+
63+
## Resources
64+
65+
No resources.
66+
67+
## Inputs
68+
69+
No inputs.
70+
71+
## Outputs
72+
73+
No outputs.
74+
<!-- END_TF_DOCS -->
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module "terraform-github" {
2+
source = "../../"
3+
4+
# Setup basic repository settings
5+
repository_name = "test_repo"
6+
repository_description = "Brief description of the test_repo project."
7+
repository_visibility = "public"
8+
9+
# Enabling/disabling repository features
10+
repository_has_issues = true
11+
repository_has_wiki = true
12+
repository_has_projects = true
13+
14+
# Manually/automatically delete head branch after a pull request is merged.
15+
repository_delete_branch_on_merge = false
16+
17+
# Use the terraform-module-template repo owned by the opsd-io team as template
18+
repository_template_repository = "terraform-module-template"
19+
repository_template_owner = "opsd-io"
20+
21+
# Set tag protection
22+
repository_tag_protection_pattern = "v*"
23+
24+
# Set default branch to `main`, make it protected and owned by the `terraformers` team.
25+
repository_default_branch = "main"
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.3.1"
3+
required_providers {
4+
github = {
5+
source = "integrations/github"
6+
version = ">= 5.3.0"
7+
}
8+
}
9+
}

main.tf

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
1-
terraform {
2-
required_version = ">= 1.3.1"
3-
required_providers {
4-
# azurerm = {
5-
# source = "hashicorp/azurerm"
6-
# version = ">= 3.22.0"
7-
# }
8-
# aws = {
9-
# source = "hashicorp/aws"
10-
# version = ">= 4.30.0"
11-
# }
12-
github = {
13-
source = "integrations/github"
14-
version = ">= 5.3.0"
15-
}
1+
# Create repo from the template
2+
provider "github" {
3+
owner = "opsd-io"
4+
}
5+
6+
resource "github_repository" "main" {
7+
name = var.repository_name
8+
description = var.repository_description
9+
10+
visibility = var.repository_visibility
11+
12+
has_issues = var.repository_has_issues
13+
has_wiki = var.repository_has_wiki
14+
has_projects = var.repository_has_projects
15+
16+
delete_branch_on_merge = var.repository_delete_branch_on_merge
17+
18+
template {
19+
owner = var.repository_template_owner
20+
repository = var.repository_template_repository
21+
}
22+
23+
}
24+
25+
# Protect the main branch.
26+
27+
resource "github_branch_protection_v3" "main" {
28+
repository = github_repository.main.name
29+
branch = var.repository_default_branch
30+
enforce_admins = true
31+
32+
require_conversation_resolution = var.repository_require_conversation_resolution
33+
34+
required_pull_request_reviews {
35+
require_code_owner_reviews = var.repository_require_code_owner_reviews
36+
required_approving_review_count = var.repository_required_approving_review_count
1637
}
1738
}
39+
40+
# Tag protection rules
41+
42+
resource "github_repository_tag_protection" "main" {
43+
repository = github_repository.main.name
44+
pattern = var.repository_tag_protection_pattern
45+
}
46+
47+
# Extra issue labels
48+
49+
resource "github_issue_label" "chore" {
50+
repository = github_repository.main.name
51+
name = "chore"
52+
description = "Changes to the build process or auxiliary tools and libraries such as documentation generation"
53+
color = "E9C978"
54+
}
55+
56+
resource "github_issue_label" "skip_changelog" {
57+
repository = github_repository.main.name
58+
name = "skip-changelog"
59+
description = "Exclude commit or PR from the changelog"
60+
color = "E8B4CF"
61+
}
62+
63+
resource "github_issue_label" "breaking" {
64+
repository = github_repository.main.name
65+
name = "breaking"
66+
description = "Breaking changes"
67+
color = "C0084C"
68+
}

variables.tf

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,83 @@
1-
# variable "variable_name" {
2-
# default = 1
3-
# type = number
4-
# }
5-
6-
# variable "another_variable_name" {
7-
# default = "c5.xlarge"
8-
# type = string
9-
# }
1+
variable "repository_name" {
2+
description = "The name of the repository."
3+
type = string
4+
default = "test_repo"
5+
}
6+
7+
variable "repository_description" {
8+
description = "Brief description of the project."
9+
type = string
10+
default = "test_repo desc"
11+
}
12+
13+
variable "repository_visibility" {
14+
description = "Specify whether the created repository should be private or public. Available options `private` or `public`."
15+
type = string
16+
default = "public"
17+
}
18+
19+
variable "repository_has_issues" {
20+
description = "Enable the GitHub Issues on the repository."
21+
type = bool
22+
default = true
23+
}
24+
25+
variable "repository_has_wiki" {
26+
description = "Enable the GitHub Wiki on the repository."
27+
type = bool
28+
default = false
29+
}
30+
31+
variable "repository_has_projects" {
32+
description = "Enable the GitHub Project on the repository."
33+
type = bool
34+
default = false
35+
}
36+
37+
variable "repository_delete_branch_on_merge" {
38+
description = "Automatically delete head branch after a pull request is merged."
39+
type = bool
40+
default = true
41+
}
42+
43+
variable "repository_template_owner" {
44+
description = "The GitHub organization or user the template repository is owned by."
45+
type = string
46+
default = "opsd-io"
47+
}
48+
49+
variable "repository_template_repository" {
50+
description = "Name of the (template) repository from which to create the new repository."
51+
type = string
52+
default = "terraform-module-template"
53+
}
54+
55+
variable "repository_require_conversation_resolution" {
56+
description = "Resolve all the comments before PR can be merged"
57+
type = bool
58+
default = true
59+
}
60+
61+
variable "repository_require_code_owner_reviews" {
62+
description = "Require code owners review before PR can be merged"
63+
type = bool
64+
default = true
65+
}
66+
67+
variable "repository_required_approving_review_count" {
68+
description = "Require N aprovales before PR can be merged"
69+
type = number
70+
default = 1
71+
}
72+
73+
variable "repository_tag_protection_pattern" {
74+
description = "The pattern of the tag to protect."
75+
type = string
76+
default = "v*"
77+
}
78+
79+
variable "repository_default_branch" {
80+
description = "The default branch name."
81+
type = string
82+
default = "main"
83+
}

0 commit comments

Comments
 (0)