DEV Community

Cover image for Creating or Deleting GitHub Repos With Terraform
Joe Hobot
Joe Hobot

Posted on

Creating or Deleting GitHub Repos With Terraform

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 for more info.

First you will need a token and then create a provider, so create file provider.tf

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

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

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 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 plan

Should look something like this

  + 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 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 and you would add users to that team membership and then you would add the team to the repositories and if you have repos already, you would definitely first import them.

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.

Top comments (0)