Platform · Enterprise

A Repo and BI Environment Factory: Terraform for GitHub Repositories and Looker Projects

Automate creation of GitHub repositories and Looker project plumbing with standard settings, branch protections, teams and access, so every new project starts compliant.

  • Terraform
  • GitHub provider
  • GitHub Actions
  • Looker
  • Branch protection
  • CODEOWNERS
  • Deploy keys
  • YAML

// Why this matters

A Terraform GitHub repository factory is how a platform team stops being a ticket queue for new repos. Every project wants a repository with the same defaults: protected main branch, required review, a CODEOWNERS file, the right teams and a baseline of workflows. Hand-creating them leaves settings inconsistent and nobody can say which repos lack protection.

Business intelligence adds a twist. A Looker project is backed by a Git repository, with a deploy key and a webhook for deployment. That is a handful of fiddly manual steps per project, and a place where private keys end up in the wrong place.

// The problem

Without automation each repository drifts. Someone disables a protection to push a fix and forgets to restore it. Teams are granted access one person at a time and nobody removes leavers. New repos miss the standard workflow files, so security scanning is silently absent.

The BI side is worse because it spans two systems: the repository host and the BI tool. A missed step produces a project that cannot deploy, and a deploy key pasted into a ticket is a credential leak waiting to happen.

// The solution

The factory has three pieces. A catalogue file lists each repository with its name, description, owning team, visibility, topics and whether it is a BI project. A module takes one entry and creates the repository, the branch protection, the team grants and the template files. A pipeline runs plan on pull request and apply on merge.

Standard settings live in the module as defaults: squash-merge only, automatic branch deletion, vulnerability alerts on, a protected default branch that requires pull-request review and passing status checks, and linear history. Teams are granted by role (maintain for the owning team, read for a wider group), so access follows membership; compare Making a 2,000-Member Identity Plan Fast, Safe and Authoritative for how the groups themselves are managed.

For BI projects the module also generates an SSH key pair with the TLS provider, registers the public half as a read-only deploy key on the repository, and writes the private half to a secret store so an administrator can paste it into the BI tool's project settings or an API call, with no human ever seeing it in a ticket. A webhook is added so pushes trigger the BI deploy.

I rejected clicking through a template repository because templates do not enforce protections and cannot repair drift. I also rejected one Terraform root per repo; a single root with for_each is easier to review, and state size stays manageable for a few hundred repos.

How it flows flow1. Request → 2. Validate → 3. Plan → 4. Gate: review → 5. Create repo → 6. Protect → 7. Grant → 8. BI plumbing → 9. Record01Request02Validate03Plan04 · GATEGate: review05Create repo06Protect07Grant08BI plumbing09Record
How it flows. End-to-end flow. Dashed pink nodes are gates: points where a human or a check must pass before the next stage runs.
  1. Requestpull request adds an entry to the repo catalogue
  2. Validateschema and naming checks in CI
  3. PlanTerraform plan posted on the pull request
  4. Gate: review gateplatform owner approves the plan
  5. Create repostandard settings, topics, template files
  6. Protectbranch protection and required checks
  7. Grantteam permissions from the catalogue
  8. BI plumbingLooker project repo, deploy key and webhook
  9. Recordoutputs written for the requester
RequesterCatalogue repoPipelineGitHubSecret storeopen PR adding catalogue entryrun validate and plancomment plan on the PRowner approves and mergesapply: repo, protection, teams, keystore generated private key
Adding a repository through a pull request. The catalogue change is reviewed once; the pipeline does the rest.

// Before you start

  • Terraform 1.6 or newer with the GitHub provider
  • A GitHub App or token with repository administration and organisation permission, supplied by the pipeline
  • Existing teams in the organisation (or created by another module)
  • A secret store for generated private keys
  • A BI instance with project Git integration (for the Looker part)
  • A remote Terraform state backend

// Step-by-step implementation

  1. 01 Step 1. Define the repository catalogue

    Keep the catalogue boring and declarative. Each entry has just the fields the module needs; anything not listed gets the standard default.

  2. 02 Step 2. Load it in the root module

    Decode the YAML with yamldecode and index by name so for_each keys are stable. Changing a repository name would be a destroy and create, so keep names immutable.

  3. 03 Step 3. Create the repository with standard settings

    The repository resource carries the merge settings and security defaults. Auto-init with a template so that a default branch exists, which branch protection needs.

  4. 04 Step 4. Protect the default branch

    Require a pull request with at least one approval, dismiss stale reviews, require code-owner review and require your CI checks. Apply the rule to administrators too, otherwise the protection is optional for the people most likely to bypass it.

  5. 05 Step 5. Grant access to teams and add CODEOWNERS

    Give the owning team maintain rights and a wider group read. Add a CODEOWNERS file as a managed file so every repository routes review to its owner.

  6. 06 Step 6. Wire the BI project: deploy key and webhook

    For BI projects, generate a key pair, register the public key as a read-only deploy key and store the private key. The BI tool's project then pulls over SSH without any person's credentials.

    A webhook triggers the BI deployment on push. Use a shared secret for the signature, taken from your secret store.

  7. 07 Step 7. Run plan on pull request and apply on merge

    The pipeline is what makes the catalogue authoritative. Plan on every pull request so reviewers see exactly what will change; apply only from the default branch and gate it with a protected environment. The same discipline is described in Vault Access via Directory Groups, Requested by an Agent, Approved by a Human.

// How to verify it works

  • After merge, the repository exists with squash-only merges and vulnerability alerts enabled.
  • Direct pushes to main are rejected, including from an organisation admin, because enforce_admins is set.
  • The owning team appears with maintain permission and .github/CODEOWNERS exists on main.
  • For a BI repo the deploy key is listed as read-only and the private key exists at the secret path, not in any log.
  • A second terraform plan shows no changes; edit a setting by hand and the next plan shows the drift.

// Design decisions and trade-offs

  • A YAML catalogue of repositories drives Terraform with for_each, so adding a repo is a small reviewed pull request.
  • Standard settings enforced by the module: default branch, squash merges, branch protection and required reviews.
  • Access is granted to teams, never individuals, and every repo has a CODEOWNERS file from a template.
  • Looker projects get a dedicated repository plus a deploy key, with the private half written straight to a secret store.
  • Plan on pull request, apply on merge, so the repository for repositories is itself governed.

// Pitfalls and lessons learned

  • Branch protection before the branch exists. Use auto_init or create an initial commit first.
  • Renaming a catalogue entry, which plans a destroy and recreate. Keep names immutable and use archive_on_destroy.
  • Admin bypass. Without enforce_admins the rule does not bind the people who most need it.
  • Required status check names that do not match the actual workflow job names, which blocks every merge.
  • Provider rate limits on large catalogues. Use a GitHub App token and parallelism limits.
  • Printing a generated private key as an output. Outputs show up in logs and state.

// Frequently asked questions

How do I create GitHub repositories with Terraform?

Use the GitHub provider's github_repository resource, drive it with for_each from a YAML catalogue and add branch protection and team permission resources in the same module.

How do I enforce branch protection across all repositories?

Create the protection in the shared module with enforce_admins on, so every repository created by the factory inherits it and drift is corrected on apply.

How does Looker connect to a GitHub repository?

A Looker project uses a Git remote with an SSH deploy key and often a webhook for deploy-on-push. The factory can generate and register the key and store the private half securely.

Should I use a template repository or Terraform?

Use both if you like, but Terraform is what enforces and repairs settings; a template only seeds files once.

// The outcome

New repositories and BI project wiring arrive with consistent settings, protections and access in minutes through one reviewed change, and drift is corrected on the next run.

// Where to take it next

// Keep reading