Skip to main content
The trunk-io/trunk Terraform provider manages merge queues as code with the trunk_merge_queue resource. The Registry documentation lists every attribute. Current version: 2.0.1. Upgrading from 1.x? See Migrate to provider 2.0.

Prerequisites

  • Terraform >= 1.0
  • A Trunk API key with the Update Merge Queue settings permission, created by an organization admin
  • A repository connected to Trunk

Authentication

Set the API key in the TRUNK_API_KEY environment variable:
Or pass it in the provider block:
The organization API token (Organization API token (v1) under Settings → Developer → API Keys) also works, but uses the v1 API, which will be deprecated soon, and doesn’t support queue_id, managed_state, import by ID, batching_max_wait_time_minutes = 0, or updating queues on hosts other than github.com, such as GitHub Enterprise. base_url (or TRUNK_BASE_URL) sets the API host, https://api.trunk.io by default. api_key and base_url must be known at plan time, so apply anything they depend on first.
Never commit an API key or the organization API token to version control. Use environment variables or a secrets manager.

Quick start

Run terraform init, terraform plan and terraform apply. If the queue already exists, apply fails with Merge queue already exists; import it instead.

What Terraform manages

Only the settings in your configuration; others keep their current value, or Trunk’s default for a new queue. Set Slack notifications, impacted targets from fork PRs and bot approvals (Settings → Organization → Bots) in the Trunk app.

Importing existing queues

Add a trunk_merge_queue block for the queue, then import it:
The ID is the part after merge-queue/ in the queue’s Trunk app URL, or queue_id in terraform state show. After importing, resolve any differences terraform plan shows before you apply.

Resource reference: trunk_merge_queue

Values are lowercase, as written below.

Required attributes

Changing any of them replaces the queue.

Settings

With an API key, an apply above a v2 cap fails, such as concurrency over 1000 or not_ready_timeout_hours over 720 (all limits).

Read-only attributes

Required statuses

required_statuses overrides .trunk/trunk.yaml and branch protection. Leave it out to keep the current override, or set [] to remove it. Order doesn’t matter.

Batching rules

target_pattern matches an impacted target exactly, or as a prefix when it ends in *; "isolate" tests a matching PR on its own. Rules apply only in "parallel" mode, and [] removes them all.

Managing drift

The Trunk app shows a banner on queues Terraform manages.

Banner for Merge Queue settings showing that the Merge Queue is managed by Terraform.

Changes made in the app to settings in your configuration show in the next plan, and apply reverts them; changes to other settings are kept.

Banner for Merge Queue settings showing that the Merge Queue is managed by Terraform but has since drifted due to settings being changed manually in the UI.

Either way, the queue shows as drifted (managed_state = "terraform_with_drift") until an apply next changes it. With an API key, once queue_id is recorded, Terraform doesn’t take over a queue recreated outside it; import the new one.

Switching modes

Changing mode drains the queue and switches once it’s empty; until then, other changes to the queue fail. To cancel, click Cancel next to Switching to … Mode in the queue’s settings, then set mode back.

If creating a queue fails partway

Terraform marks the half-created queue tainted, so the next apply recreates it; run terraform untaint trunk_merge_queue.<name> first to keep it instead.

Deleting a queue

Only an empty queue can be deleted: set state = "draining", apply, wait for in-flight PRs to finish, then run terraform destroy.

Examples

High-throughput queue with batching

Queue with explicit required statuses