|
1 | | -# terraform-git-workspace |
2 | | -Module for attaching Actions and Environments to target repos |
| 1 | +# terraform-github-workspace |
| 2 | + |
| 3 | +A comprehensive Terraform module for configuring GitHub repositories with environments, branch policies, and CI/CD workflows. |
3 | 4 |
|
4 | 5 | [](https://github.com/HappyPathway/terraform-github-workspace/actions/workflows/terraform.yaml) |
5 | 6 |
|
| 7 | +## Overview |
| 8 | + |
| 9 | +This module automates the setup and configuration of GitHub repositories including: |
| 10 | + |
| 11 | +- GitHub environments with appropriate deployment reviews and branch protections |
| 12 | +- Terraform CI/CD workflows (plan and apply) specific to each environment |
| 13 | +- Integration with AWS S3 for Terraform state and workflow caching |
| 14 | +- GitHub Action secrets and environment variables |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- **Environment Management**: Create and configure GitHub environments with custom settings |
| 19 | +- **CI/CD Workflow Templates**: Auto-generated Terraform plan/apply workflows for each environment |
| 20 | +- **AWS Integration**: Support for AWS-backed Terraform state management |
| 21 | +- **Branch Protection**: Customizable branch policies with options for PR reviews and status checks |
| 22 | +- **Secret Management**: Environment-specific secrets and variables for GitHub Actions |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +```hcl |
| 27 | +locals { |
| 28 | + repo = { |
| 29 | + name = "example-repo" |
| 30 | + create_repo = true |
| 31 | + repo_org = "YourOrgName" |
| 32 | + description = "Example repository managed by Terraform" |
| 33 | + } |
| 34 | +} |
| 35 | +
|
| 36 | +resource "aws_s3_bucket" "cache_bucket" { |
| 37 | + bucket = "terraform-state-${local.repo.name}" |
| 38 | +} |
| 39 | +
|
| 40 | +module "github_actions" { |
| 41 | + source = "HappyPathway/workspace/github" |
| 42 | + |
| 43 | + repo = local.repo |
| 44 | + |
| 45 | + environments = [ |
| 46 | + { |
| 47 | + name = "development" |
| 48 | + cache_bucket = aws_s3_bucket.cache_bucket.bucket |
| 49 | + deployment_branch_policy = { |
| 50 | + branch = "dev" |
| 51 | + } |
| 52 | + }, |
| 53 | + { |
| 54 | + name = "production" |
| 55 | + cache_bucket = aws_s3_bucket.cache_bucket.bucket |
| 56 | + reviewers = { |
| 57 | + enforce_reviewers = true |
| 58 | + teams = ["terraform-reviewers"] |
| 59 | + } |
| 60 | + deployment_branch_policy = { |
| 61 | + branch = "main" |
| 62 | + protected_branches = true |
| 63 | + } |
| 64 | + } |
| 65 | + ] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Requirements |
| 70 | + |
| 71 | +- Terraform >= 0.14 |
| 72 | +- GitHub Provider |
| 73 | +- AWS Provider (when using S3 backend) |
| 74 | + |
| 75 | +## Components |
| 76 | + |
| 77 | +This module creates: |
| 78 | + |
| 79 | +1. GitHub environments with deployment review settings |
| 80 | +2. Branch protection rules based on environment settings |
| 81 | +3. GitHub Actions workflows for Terraform plan/apply |
| 82 | +4. Terraform backend configuration files |
| 83 | +5. Environment-specific secrets and variables |
| 84 | + |
| 85 | +## Environment Configuration |
| 86 | + |
| 87 | +Each environment can be configured with: |
| 88 | + |
| 89 | +- Reviewers (users and teams) |
| 90 | +- Deployment branch policies |
| 91 | +- Wait timers for deployments |
| 92 | +- AWS S3 backend settings |
| 93 | +- Environment-specific secrets and variables |
| 94 | + |
| 95 | +## CI/CD Workflow |
| 96 | + |
| 97 | +The module generates GitHub Actions workflows that: |
| 98 | + |
| 99 | +1. Initialize Terraform with the correct backend |
| 100 | +2. Plan changes with environment-specific variables |
| 101 | +3. Apply changes after approval (in the target environment) |
| 102 | +4. Use S3 for caching Terraform artifacts between steps |
| 103 | + |
| 104 | +## Advanced Features |
| 105 | + |
| 106 | +- Self-review prevention |
| 107 | +- Custom branch policies |
| 108 | +- Admin bypass settings |
| 109 | +- Support for custom GitHub Action composite actions |
| 110 | + |
6 | 111 | <!-- BEGIN_TF_DOCS --> |
7 | 112 | {{ .Content }} |
8 | 113 | <!-- END_TF_DOCS --> |
0 commit comments