Skip to content

About

OpenTofu repo for managing TailRocks GitHub org secrets

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

74 Commits

Folders and files

Repository files navigation

github-terraform

Manages GitHub repository settings and branch/tag protection for the tailrocks organization: 50 repositories, each with repository merge settings plus protect-main and protect-tags rulesets, and optional per-repository tag-creation rulesets. Two exceptions: archived repositories (archived = true) get no managed settings, because GitHub refuses changes to them, and private repositories get no rulesets, because the free plan returns 403 for them.

Layout

  • variables.tf — repository_policies inventory (single source of truth).
  • locals.tf — check_templates copy-paste source for check lists.
  • repositories.tf — module.repository_policy wiring.
  • organization.tf — organization settings, Actions/workflow permissions, and the Velnor runner groups.
  • environments.tf — the velnor-new release deployment environment (branch main and tag tools-v* deployments).
  • modules/repository-policy/ — shared policy module (identical across orgs).
  • checks.tf — invariant guards (no scripts).
  • imports.tf — imports for existing repository settings and rulesets.
  • providers.tf, versions.tf, outputs.tf — provider, pins, outputs.

Authentication

Run tofu with GITHUB_TOKEN exported in the shell, or via the mise tasks mise run tofu:plan / mise run tofu:apply, which inject the organization token from 1Password. The token needs Administration: write on every managed repository. It also needs org admin on tailrocks for the organization settings, Actions policies, and runner group in organization.tf.

Adding a repository

  1. Add one entry to repository_policies in variables.tf, copying a check list from locals.tf (dco_policy_ci = ["DCO", "Policy", "ci-required"], dco_required = ["DCO", "Required"], control = ["Control / Required"], required_only = ["Required"], dco_only = ["DCO"], policy = ["Policy"]); bespoke checks inline with a comment citing the producing workflow. A repository that requires DCO and is not archived must also set required_check_apps = { DCO = 974774 } (the cncf/dco2 app; tofu plan fails otherwise). Repos with no CI use required_checks = [] plus a non-empty no_ci_reason and extend the allowlist check in checks.tf. Always add the name to the mandatory list in mandatory_target_repositories_present in checks.tf (updating its count and message).
  2. Add imports for settings and any existing protect_main and protect_tags rulesets to imports.tf using the live numeric ruleset IDs. The module creates missing rulesets. A private repository gets only the settings import; an archived one gets only the two ruleset imports.
  3. Set the pull-request creation policy, which the provider cannot manage yet (integrations/terraform-provider-github#3479): gh api -X PATCH repos/tailrocks/<repo> -f pull_request_creation_policy=collaborators_only. Check it with gh api repos/tailrocks/<repo> --jq .pull_request_creation_policy.

Per-repository options

Each repository_policies entry may also set:

  • protect_tags (default true): set false to skip the protect-tags ruleset. Only termrock does (TODO in variables.tf).
  • review_thread_resolution (default true): set false to drop the requirement to resolve review threads on protect-main. Only termrock does.
  • required_check_apps (default {}): map from a required check context to the ID of the app that must post it on protect-main. 15368 is GitHub Actions and 974774 is the cncf/dco2 app. A check that is not listed accepts any source. Every non-archived repository that requires DCO pins it to 974774 (local.dco_app_id; a variables.tf validation, in the root and the module, enforces this; archived repositories are exempt and keep DCO unpinned, as live); every pinned ID must be positive, since 0 is the provider's unset value and means any source; only termrock and velnor-new pin Required to 15368, and ruxel pins Acceptance local to 15368 (a variables.tf validation keeps all three pinned). Read an app ID from live data, not from memory: gh api repos/tailrocks/<repo>/commits/<sha>/check-runs --jq '.check_runs[] | {name, app: .app.slug, id: .app.id}'.
  • merge_settings (default null): the merge policy. null is the organization policy (squash-only, delete branch on merge, linear history, PR_TITLE/PR_BODY). Only termrock overrides it, to keep the merge-commit policy of the repository it was renamed from. checks.tf pins that exception list.
  • admin_pull_request_bypass (default false): let repository admins bypass protect-main through a pull request only (actor RepositoryRole 5, mode pull_request), never by direct push. Only velnor-new sets it; this is enforced by a variables.tf validation (an input error, not a warn-only check).
  • archived (default false): set it for a repository that is archived on GitHub. The module then skips its settings and reads its ID through a data source; its rulesets stay managed.
  • strict_required_status_checks (default false): require the branch to be up to date with the default branch before merging.
  • protect_tags_update (default false): add the update rule to protect-tags, so that existing tags cannot move.
  • tag_creation_rulesets (default {}): extra tag rulesets, keyed by ruleset name, with the creation rule for the include patterns and an always bypass for each user in bypass_user_ids and each GitHub App in bypass_app_ids (actor type Integration). Import each existing ruleset as tag_creation["<repository>/<ruleset name>"] in imports.tf.

Organization overlays

  • organization.tf manages the org profile (github_organization_settings), Actions availability/allow policy (github_actions_organization_permissions), default GITHUB_TOKEN workflow permissions (github_actions_organization_workflow_permissions), and the Velnor runner groups velnor-ci and velnor-ci-trusted (var.velnor_runner_groups: each group has a repository list that must be a subset of repository_policies; a non-empty selected_workflows list also restricts the group to those workflows; velnor-ci-trusted must keep a non-empty list, enforced by a variables.tf validation). Import a group by its numeric ID in imports.tf.
  • GitHub does not expose organization package-creation visibility through the REST API or the Terraform GitHub provider; manage those settings in Organization settings → Packages. Making a package public is irreversible.

Verification

mise run check:tofu       # tofu fmt, init, validate: the CI OpenTofu job
mise run workflows        # velnor-gen --root . check: the CI Generated workflows job

CI is generated by velnor-gen from .velnor/rust-ci.toml; change that file and run velnor-gen --root . generate. The Required job gates the merge.

OpenTofu is pinned via mise.toml (opentofu 1.13.1); existing repos are imported via imports.tf on first apply.

About

OpenTofu repo for managing TailRocks GitHub org secrets

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages