diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf895e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +terraform/.terraform/ +terraform/terraform.tfstate +terraform/terraform.tfstate.backup +terraform/*.tfvars diff --git a/terraform/.terraform.lock.hcl b/terraform/.terraform.lock.hcl new file mode 100644 index 0000000..437e57f --- /dev/null +++ b/terraform/.terraform.lock.hcl @@ -0,0 +1,25 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/integrations/github" { + version = "6.8.3" + constraints = "~> 6.0" + hashes = [ + "h1:L6vZqEZkJbqdJHLXIJ6ImwpwM2AIJXetKRlJezf5G/I=", + "zh:0795635834c762371aae1748f68d17db778918f48a630c69e673e0339edc0869", + "zh:191649a4ca68b8c5235712247b9ae05b16123e912c8e0f875267df68fda64452", + "zh:3a5260d0af06c37a346e9397f7563e03247c99906b4d2df9d615ab72a6a2dde1", + "zh:57b5f57e45a84124780ebc5b2ffb40926a513dfdd45193eab137634c765db5b0", + "zh:639568914b977203fa3f94dc55c256022f800daaaeb66084e01302cffda9d933", + "zh:8976e4963db88a5ad8209f0422754f2aa75220f12123228804cc97169f701ee0", + "zh:971e1021c45ab06caa966030494957c0e87bd9e30cd8358e41aaf8c120352186", + "zh:9e379ed9235f5dadb4c5b625016a474961d39fba5753a3155f3dda56446f0ec2", + "zh:a50e5e02cd99479d8bc9b06c428b610562986931ba258b45c09f2dec76711086", + "zh:b421552318952b2fa30ef30b4279b56620bddef65d11cbda1cfa123be5a3bf9c", + "zh:bc8bf3a88b9daaca1ce1e5b3e59be8ae0be504f111106094de791e19e0e49c9d", + "zh:d3f9b1d8ed5a58a8e22aa20e3d76dc7afe83f648a41ff996e445de0c6a1f13cc", + "zh:dc55d47cb73f633e9f5d4ebcfba1a2212b6e83fcb7a6c7487606324053a2943f", + "zh:dfc8e85505e3ce90673b460833e94d935142145b79265912c3c805d8de12c4f2", + "zh:fbd1fee2c9df3aa19cf8851ce134dea6e45ea01cb85695c1726670c285797e25", + ] +} diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..abeedfb --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,134 @@ +# Repository data +data "github_repository" "this" { + full_name = "${var.github_owner}/${var.repository_name}" +} + +# develop branch + set as default +resource "github_branch" "develop" { + repository = data.github_repository.this.name + branch = "develop" + source_branch = data.github_repository.this.default_branch +} + +resource "github_branch_default" "default" { + repository = data.github_repository.this.name + branch = github_branch.develop.branch +} + +# Collaborator softservedata +resource "github_repository_collaborator" "softservedata" { + repository = data.github_repository.this.name + username = "softservedata" + permission = "push" +} + +# Branch protection: develop +resource "github_branch_protection" "develop" { + repository_id = data.github_repository.this.node_id + pattern = "develop" + + allows_deletions = false + allows_force_pushes = false + enforce_admins = true + + required_pull_request_reviews { + dismiss_stale_reviews = true + required_approving_review_count = 2 + require_code_owner_reviews = false + } + + require_conversation_resolution = true + +} + +# Branch protection: main +resource "github_branch_protection" "main" { + repository_id = data.github_repository.this.node_id + pattern = "main" + + allows_deletions = false + allows_force_pushes = false + enforce_admins = true + + required_pull_request_reviews { + dismiss_stale_reviews = true + required_approving_review_count = 1 + require_code_owner_reviews = true + } + + require_conversation_resolution = true + +} + +# CODEOWNERS file +resource "github_repository_file" "codeowners" { + repository = data.github_repository.this.name + file = ".github/CODEOWNERS" + branch = "main" + content = "* @softservedata\n" + commit_message = "Add CODEOWNERS file assigning softservedata" + overwrite_on_create = true +} + +# Pull request template +resource "github_repository_file" "pull_request_template" { + repository = data.github_repository.this.name + file = ".github/pull_request_template.md" + branch = "main" + commit_message = "Add pull request template" + overwrite_on_create = true + + content = <<-EOT + ## Describe your changes + + Please provide a clear and concise description of the changes you are making. + + ## Issue ticket number and link + + - Ticket: + + ## Checklist before requesting a review + + - [ ] I have performed a self-review of my code + - [ ] If it is a core feature, I have added thorough tests + - [ ] Do we need to implement analytics? + - [ ] Will this be part of a product update? If yes, please write one phrase about this update + EOT +} + +# Deploy key +resource "github_repository_deploy_key" "deploy_key" { + repository = data.github_repository.this.name + title = "DEPLOY_KEY" + key = var.deploy_key_public + read_only = true +} + +# GitHub Actions secrets +resource "github_actions_secret" "pat" { + repository = data.github_repository.this.name + secret_name = "PAT" + plaintext_value = var.pat_token +} + +resource "github_actions_secret" "terraform_code" { + repository = data.github_repository.this.name + secret_name = "TERRAFORM" + plaintext_value = file("${path.module}/main.tf") +} + +# Discord webhook for PR notifications +resource "github_repository_webhook" "discord_pr" { + repository = data.github_repository.this.name + active = true + + events = [ + "pull_request", + ] + + configuration { + url = var.discord_webhook_url + content_type = "json" + insecure_ssl = false + } +} diff --git a/terraform/variables.tf b/terraform/variables.tf new file mode 100644 index 0000000..0fada5b --- /dev/null +++ b/terraform/variables.tf @@ -0,0 +1,35 @@ +variable "github_token" { + description = "GitHub Personal Access Token for Terraform" + type = string + sensitive = true +} + +variable "github_owner" { + description = "GitHub organization or user owner" + type = string + default = "Practical-DevOps-GitHub" +} + +variable "repository_name" { + description = "Repository name to configure" + type = string + default = "github-terraform-task-chinnk" +} + +variable "pat_token" { + description = "PAT stored as Actions secret PAT" + type = string + sensitive = true +} + +variable "deploy_key_public" { + description = "Public SSH key for DEPLOY_KEY" + type = string + sensitive = true +} + +variable "discord_webhook_url" { + description = "Discord webhook URL for PR events" + type = string + sensitive = true +} diff --git a/terraform/versions.tf b/terraform/versions.tf new file mode 100644 index 0000000..7c61dac --- /dev/null +++ b/terraform/versions.tf @@ -0,0 +1,15 @@ +terraform { + required_version = ">= 1.6.0" + + required_providers { + github = { + source = "integrations/github" + version = "~> 6.0" + } + } +} + +provider "github" { + token = var.github_token + owner = var.github_owner +}