About a year ago I posted an article on how I structured my Terraform projects. Since then, GitHub has released templates, which allows you to create new repositories from a template repository. This has greatly simplified how I generate new repositories to run Terraform code.

The Old Way

The old way, while it worked, required a lot of steps to do it. You can see the whole process if you read the article linked above, but essentially you had to create a new empty repository, then you needed to check out the skeleton repository to the new repository name. Once that was done you had to remove the old origin, add the new origin, and then push the files up. It also left all the commits of the skeleton as part of the repo.

The New Way

Before you can use a repository as a template, you must first tell GitHub that it is one. To do this, you can go to the Settings page of the repository and click “Template repository” under the Repository name, or you can use the API to set it as a template.


curl -s -X PATCH https://api.github.com/repos/austincloudguru/my-new-repo \
-H "Accept: application/vnd.github.baptiste-preview+json" \
-H "Authorization: token $GIT_TOKEN" \
-d @<(cat <<EOF
{
	"is_template": true
}
EOF
)|jq -r .is_template

Once a repository has been designated a template, you can select it from the Repository template dropdown on the Create a new repository page, or you can use the API to create the new repository from a template.


curl -s -X POST https://api.github.com/repos/austincloudguru/terraform-skeleton/generate \
-H "Accept: application/vnd.github.baptiste-preview+json" \
-H "Authorization: token $GIT_TOKEN" \
-d @<(cat <<EOF
{
	"owner": "austincloudguru",
  "name": "my-new-repo",
	"description": "My Test Repo",
  "private": true
}
EOF
)|jq -r .ssh_url

It may seem like a trivial change, but it has greatly increased my efficiency in starting new terraform projects.