Creating or Deleting GitHub Repos With Terraform
Tags: github, terraform, devops
Published: 2019-04-30 14:02:48

Just saw on twitter some tweet regards to site that helps you remove multiple github. While that's all fine and dandy, I really do not trust giving other sites permissions to my org or any type of API access even to my personal github account. 

Now remember, to use terraform github provider you will need to have an org, otherwise it does not work with regular users. 

I've been using terraform for almost any infrastructure orchestration and that includes anything that goes along with GitHub permissions,repos,users,orgs etc... because managing repos and performing some type of auditing with checks and balances can get messy. 


Here is super easy way to manage your GitHub repos. I won't go too deep into setting up variables, but you can check [GitHub Provider](https://www.terraform.io/docs/providers/github/index.html) for more info. 

First you will need a [token](https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) and then create a provider, so create file provider.tf

```terraform
provider "github" {
  token        = "YourTokenGoesHere"
  organization = "YourOrg"
}
``` 

Next , create a file that will hold your repos and collaborators.

```terraform
resource "github_repository" "mynewrepo-repo" {
  name        = "mynewrepo-repo"
  description = "Sweet Terraform Repo Yey..."
}

resource "github_repository_collaborator" "a_repo_collaborator" {
  repository = "mynewrepo-repo"
  username   = "wow-much-love$$"
  permission = "admin"
}
```

Run these commands...

First you need to initialize terraform,so you get the modules etc.


```terraform 
terraform init
```

Once you are done with initialization , run a terraform plan, to see what will terraform do for you before you apply the changes. 

```terraform 
terraform plan
```


Should look something like this

```terraform
  + github_repository.mynewrepo-repo
      id:                 <computed>
      allow_merge_commit: "true"
      allow_rebase_merge: "true"
      allow_squash_merge: "true"
      archived:           "false"
      default_branch:     <computed>
      description:        "Sweet Terraform Repo Yey..."
      etag:               <computed>
      full_name:          <computed>
      git_clone_url:      <computed>
      html_url:           <computed>
      http_clone_url:     <computed>
      name:               "mynewrepo-repo"
      ssh_clone_url:      <computed>
      svn_url:            <computed>

  + github_repository_collaborator.a_repo_collaborator
      id:                 <computed>
      permission:         "admin"
      repository:         "mynewrepo-repo"
      username:           "wow-much-love$$"
Plan: 2 to add, 0 to change, 0 to destroy.
```

And if you are happy with outcome above, and you do not have anything like DESTROY or CHANGE. Feel free to run 

```terraform 
terraform apply
```
Within few seconds you should see your new repo in your org.

Now this is super dirty and quick way of creating repos and adding a collaborator. 

However, in real world you would create [teams](https://www.terraform.io/docs/providers/github/r/team.html) and you would add users to that [team membership](https://www.terraform.io/docs/providers/github/r/team_membership.html) and then you would add the [team to the repositories](https://www.terraform.io/docs/providers/github/r/team_repository.html) and if you have repos already, you would definitely first [import them](https://www.terraform.io/docs/providers/github/r/repository.html#import). 


Oh and to remove the repo that you created via terraform, just remove repo and collaborator from step above and that should be it. 

As mentioned, this is really quick of start creating github repos. It should not take you more than 15 minutes to set this all up.