An open API service indexing awesome lists of open source software.

https://github.com/happypathway/terraform-github-repo-subdir

Terraform Module
https://github.com/happypathway/terraform-github-repo-subdir

module terraform terraform-managed

Last synced: 4 months ago
JSON representation

Terraform Module

Awesome Lists containing this project

README

          

# terraform-github-repo-subdir

This Terraform module enables you to extract a subdirectory from an existing GitHub repository and create a new GitHub repository with that content, preserving all its files and optionally creating an initial release.

## Features

- Extract and copy a specific subdirectory from a source GitHub repository
- Create a new GitHub repository with the extracted content
- Support for both SSH and HTTPS protocols for source and destination repositories
- Configure comprehensive GitHub repository settings
- Create an optional GitHub release for the new repository
- Fully customizable repository settings (visibility, branch protection, teams, etc.)
- Support for adding extra files, CODEOWNERS, and repository metadata

## Usage

```hcl
module "example_repo" {
source = "github.com/HappyPathway/terraform-github-repo-subdir"

# Source repository information
repo_src = "org/source-repository"
repo_branch = "main"
sub_dir = "path/to/subdirectory"

# Destination repository information
repo_org = "destination-org"
repo_dest = "new-repository-name"
repo_desc = "Description for the new repository"

# Local filesystem path where repo will be cloned
repo_dir = "/tmp/new-repository-name"

# Set to true if this is a Terraform module (creates a v1.0.0 tag)
module = "true"

# Configure Git protocol (SSH or HTTPS)
use_ssh_source = true # Use SSH for source repository
use_ssh_destination = false # Use HTTPS for destination repository

# GitHub repository settings
github_is_private = false
enforce_prs = true
github_default_branch = "main"

# Add appropriate topics
github_repo_topics = ["terraform", "infrastructure", "example"]
}
```

## Requirements

- Python 3.6+
- Git
- GitHub Personal Access Token with appropriate permissions
- Terraform 0.13+

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| repo_src | Name of source repo, must be in form `/` | `string` | n/a | yes |
| repo_dest | Name of newly created GitHub repo | `string` | n/a | yes |
| repo_desc | Description of newly created GitHub repo | `string` | n/a | yes |
| repo_dir | Location to save repo on local system | `string` | n/a | yes |
| repo_org | GitHub organization or user name where the repository will be created | `string` | n/a | yes |
| repo_branch | Source repository branch to use | `string` | `"master"` | no |
| sub_dir | Subdirectory containing desired repo content | `string` | `false` | no |
| module | Is this a Terraform module? If so, set to true | `string` | `false` | no |
| github_is_private | Whether the GitHub repository should be private | `bool` | `false` | no |
| enforce_prs | Whether to enforce pull request reviews before merging | `bool` | `true` | no |
| pull_request_bypassers | List of GitHub usernames that can bypass pull request requirements | `list(string)` | `[]` | no |
| use_ssh_source | Whether to use SSH URLs for source repository Git operations | `bool` | `true` | no |
| use_ssh_destination | Whether to use SSH URLs for destination repository Git operations | `bool` | `true` | no |

This module supports many additional GitHub repository configuration options. See `variables.tf` for a complete list of supported variables.

## Outputs

| Name | Description |
|------|-------------|
| ssh_clone_url | SSH clone URL of the created repository |
| http_clone_url | HTTP clone URL of the created repository |
| repo_name | Full name of the created repository |
| repo_setup_result | Result of the repository setup operation |

## Examples

See the [examples directory](./examples) for complete examples:

1. **[Split All Submodules](./examples/split_monolith.tf)**: Split all submodules from a monolithic repository into separate repositories
2. **[Split Single Module](./examples/split_single_module.tf)**: Extract just one module into its own repository
3. **[Custom Module Settings](./examples/module_with_custom_settings.tf)**: Create a module repository with extensive custom GitHub settings

## How It Works

1. Clones the source repository
2. Extracts the specified subdirectory
3. Initializes a new Git repository with the extracted content
4. Creates a new GitHub repository with the specified settings
5. Pushes the content to the new repository
6. Optionally creates a v1.0.0 release tag

## License

This project is licensed under the terms of the MIT license.

[![Terraform Validation](https://github.com/HappyPathway/terraform-github-repo-subdir/actions/workflows/terraform.yaml/badge.svg)](https://github.com/HappyPathway/terraform-github-repo-subdir/actions/workflows/terraform.yaml)

## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| [external](#provider\_external) | 2.3.4 |
| [github](#provider\_github) | 6.6.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| [repo](#module\_repo) | HappyPathway/repo/github | n/a |

## Resources

| Name | Type |
|------|------|
| [github_release.initial_release](https://registry.terraform.io/providers/hashicorp/github/latest/docs/resources/release) | resource |
| [external_external.repo_setup](https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external) | data source |
| [github_repository.repo_src](https://registry.terraform.io/providers/hashicorp/github/latest/docs/data-sources/repository) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| [additional\_codeowners](#input\_additional\_codeowners) | Additional entries for CODEOWNERS file | `list(string)` | `[]` | no |
| [admin\_teams](#input\_admin\_teams) | Teams to grant admin access | `list(string)` | `[]` | no |
| [allow\_unsigned\_files](#input\_allow\_unsigned\_files) | Whether to allow file management even when signed commits are required | `bool` | `false` | no |
| [archive\_on\_destroy](#input\_archive\_on\_destroy) | Archive repository instead of deleting on destroy | `bool` | `true` | no |
| [archived](#input\_archived) | Archive this repository | `bool` | `false` | no |
| [collaborators](#input\_collaborators) | Map of collaborators and their permission levels | `map(string)` | `{}` | no |
| [commit\_author](#input\_commit\_author) | The author name to use for file commits | `string` | `"Terraform"` | no |
| [commit\_email](#input\_commit\_email) | The email to use for file commits | `string` | `"terraform@github.com"` | no |
| [create\_codeowners](#input\_create\_codeowners) | Create CODEOWNERS file | `bool` | `true` | no |
| [create\_repo](#input\_create\_repo) | Whether to create a new repository or manage an existing one | `bool` | `true` | no |
| [deploy\_keys](#input\_deploy\_keys) | List of SSH deploy keys to add to the repository |

list(object({
title = string
key = optional(string, "")
read_only = optional(bool, true)
create = optional(bool, false)
}))
| `[]` | no |
| [enforce\_prs](#input\_enforce\_prs) | Whether to enforce pull request reviews before merging | `bool` | `true` | no |
| [environments](#input\_environments) | List of GitHub environments to create for the repository |
list(object({
name = string
reviewers = optional(object({
teams = optional(list(string), [])
users = optional(list(string), [])
}), {})
deployment_branch_policy = optional(object({
protected_branches = optional(bool, true)
custom_branch_policies = optional(bool, false)
}), {})
secrets = optional(list(object({
name = string
value = string
})), [])
vars = optional(list(object({
name = string
value = string
})), [])
}))
| `[]` | no |
| [extra\_files](#input\_extra\_files) | Additional files to create in the repository |
list(object({
path = string
content = string
}))
| `[]` | no |
| [force\_name](#input\_force\_name) | Keep exact repository name (no date suffix) | `bool` | `false` | no |
| [github\_allow\_auto\_merge](#input\_github\_allow\_auto\_merge) | Allow pull requests to be automatically merged | `bool` | `false` | no |
| [github\_allow\_merge\_commit](#input\_github\_allow\_merge\_commit) | Allow merge commits | `bool` | `false` | no |
| [github\_allow\_rebase\_merge](#input\_github\_allow\_rebase\_merge) | Allow rebase merging | `bool` | `false` | no |
| [github\_allow\_squash\_merge](#input\_github\_allow\_squash\_merge) | Allow squash merging | `bool` | `true` | no |
| [github\_allow\_update\_branch](#input\_github\_allow\_update\_branch) | Allow updating pull request branches | `bool` | `true` | no |
| [github\_auto\_init](#input\_github\_auto\_init) | Initialize repository with README | `bool` | `true` | no |
| [github\_codeowners\_team](#input\_github\_codeowners\_team) | GitHub team to use for CODEOWNERS file | `string` | `"terraform-reviewers"` | no |
| [github\_default\_branch](#input\_github\_default\_branch) | Default branch name | `string` | `"main"` | no |
| [github\_delete\_branch\_on\_merge](#input\_github\_delete\_branch\_on\_merge) | Delete head branch after merge | `bool` | `true` | no |
| [github\_dismiss\_stale\_reviews](#input\_github\_dismiss\_stale\_reviews) | Dismiss stale pull request approvals | `bool` | `true` | no |
| [github\_enforce\_admins\_branch\_protection](#input\_github\_enforce\_admins\_branch\_protection) | Enforce branch protection rules on administrators | `bool` | `true` | no |
| [github\_has\_discussions](#input\_github\_has\_discussions) | Enable discussions feature | `bool` | `false` | no |
| [github\_has\_downloads](#input\_github\_has\_downloads) | Enable downloads feature | `bool` | `false` | no |
| [github\_has\_issues](#input\_github\_has\_issues) | Enable issues feature | `bool` | `false` | no |
| [github\_has\_projects](#input\_github\_has\_projects) | Enable projects feature | `bool` | `true` | no |
| [github\_has\_wiki](#input\_github\_has\_wiki) | Enable wiki feature | `bool` | `true` | no |
| [github\_is\_private](#input\_github\_is\_private) | Whether the GitHub repository should be private | `bool` | `false` | no |
| [github\_merge\_commit\_message](#input\_github\_merge\_commit\_message) | Message for merge commits | `string` | `"PR_TITLE"` | no |
| [github\_merge\_commit\_title](#input\_github\_merge\_commit\_title) | Title for merge commits | `string` | `"MERGE_MESSAGE"` | no |
| [github\_org\_teams](#input\_github\_org\_teams) | Organization teams configuration | `list(any)` | `null` | no |
| [github\_pro\_enabled](#input\_github\_pro\_enabled) | Is this a Github Pro Account? If not, then it's limited in feature set | `bool` | `false` | no |
| [github\_push\_restrictions](#input\_github\_push\_restrictions) | List of team/user IDs with push access | `list(string)` | `[]` | no |
| [github\_repo\_description](#input\_github\_repo\_description) | Repository description | `string` | `null` | no |
| [github\_repo\_topics](#input\_github\_repo\_topics) | Repository topics | `list(string)` | `[]` | no |
| [github\_require\_code\_owner\_reviews](#input\_github\_require\_code\_owner\_reviews) | Require code owner review | `bool` | `true` | no |
| [github\_required\_approving\_review\_count](#input\_github\_required\_approving\_review\_count) | Number of approvals needed for pull requests | `number` | `1` | no |
| [github\_squash\_merge\_commit\_message](#input\_github\_squash\_merge\_commit\_message) | Message for squash merge commits | `string` | `"COMMIT_MESSAGES"` | no |
| [github\_squash\_merge\_commit\_title](#input\_github\_squash\_merge\_commit\_title) | Title for squash merge commits | `string` | `"COMMIT_OR_PR_TITLE"` | no |
| [gitignore\_template](#input\_gitignore\_template) | Gitignore template to use | `string` | `null` | no |
| [homepage\_url](#input\_homepage\_url) | Repository homepage URL | `string` | `null` | no |
| [is\_template](#input\_is\_template) | Make this repository a template | `bool` | `false` | no |
| [license\_template](#input\_license\_template) | License template to use for the repository | `string` | `null` | no |
| [managed\_extra\_files](#input\_managed\_extra\_files) | Additional files to manage in the repository |
list(object({
path = string
content = string
}))
| `[]` | no |
| [module](#input\_module) | Is this a Terraform Module? if so, set to true | `string` | `false` | no |
| [pages\_config](#input\_pages\_config) | Configuration for GitHub Pages |
object({
branch = optional(string, "gh-pages")
path = optional(string, "/")
cname = optional(string)
})
| `null` | no |
| [prefix](#input\_prefix) | Prefix to add to repository name | `string` | `null` | no |
| [pull\_request\_bypassers](#input\_pull\_request\_bypassers) | List of GitHub usernames that can bypass pull request requirements | `list(string)` | `[]` | no |
| [repo\_branch](#input\_repo\_branch) | n/a | `string` | `"master"` | no |
| [repo\_desc](#input\_repo\_desc) | Description of newly created github repo | `string` | n/a | yes |
| [repo\_dest](#input\_repo\_dest) | Name of newly created github repo | `string` | n/a | yes |
| [repo\_dir](#input\_repo\_dir) | Location to save repo on Local System | `string` | n/a | yes |
| [repo\_org](#input\_repo\_org) | GitHub organization or user name where the repository will be created | `string` | n/a | yes |
| [repo\_src](#input\_repo\_src) | Name of Source Repo, must be in form / | `string` | n/a | yes |
| [require\_last\_push\_approval](#input\_require\_last\_push\_approval) | Require approval from the last pusher | `bool` | `false` | no |
| [require\_signed\_commits](#input\_require\_signed\_commits) | Whether to require signed commits for the default branch | `bool` | `false` | no |
| [required\_status\_checks](#input\_required\_status\_checks) | Required status checks for protected branches |
object({
contexts = list(string)
strict = optional(bool, false)
})
| `null` | no |
| [secrets](#input\_secrets) | GitHub Actions secrets |
list(object({
name = string
value = string
}))
| `[]` | no |
| [security\_and\_analysis](#input\_security\_and\_analysis) | Security and analysis settings for the repository |
object({
advanced_security = optional(object({
status = string
}), { status = "disabled" })
secret_scanning = optional(object({
status = string
}), { status = "disabled" })
secret_scanning_push_protection = optional(object({
status = string
}), { status = "disabled" })
})
| `null` | no |
| [sub\_dir](#input\_sub\_dir) | Subdirectory containing desired repo content | `string` | `false` | no |
| [template\_repo](#input\_template\_repo) | Template repository name | `string` | `null` | no |
| [template\_repo\_org](#input\_template\_repo\_org) | Template repository organization | `string` | `null` | no |
| [use\_ssh](#input\_use\_ssh) | Whether to use SSH URLs for Git operations (true) or HTTPS URLs (false) | `bool` | `true` | no |
| [use\_ssh\_destination](#input\_use\_ssh\_destination) | Whether to use SSH URLs for destination repository Git operations (true) or HTTPS URLs (false) | `bool` | `true` | no |
| [use\_ssh\_source](#input\_use\_ssh\_source) | Whether to use SSH URLs for source repository Git operations (true) or HTTPS URLs (false) | `bool` | `true` | no |
| [vars](#input\_vars) | GitHub Actions variables |
list(object({
name = string
value = string
}))
| `[]` | no |
| [vulnerability\_alerts](#input\_vulnerability\_alerts) | Enable Dependabot alerts | `bool` | `false` | no |

## Outputs

| Name | Description |
|------|-------------|
| [http\_clone\_url](#output\_http\_clone\_url) | n/a |
| [repo\_name](#output\_repo\_name) | n/a |
| [repo\_setup\_result](#output\_repo\_setup\_result) | Result of the repository setup operation |
| [ssh\_clone\_url](#output\_ssh\_clone\_url) | n/a |