View as Markdown

Managing Mergify with Terraform

How to manage Mergify configuration declaratively with the official Terraform provider.


The official Mergify Terraform provider lets you manage your Mergify configuration as code, alongside the rest of your infrastructure.

  1. The Mergify GitHub App is installed on your repositories.

  2. A Mergify application key or a GitHub personal access token. See Authentication below.

Declare the provider in your Terraform configuration:

terraform {
required_providers {
mergify = {
source = "Mergifyio/mergify"
version = "~> 0.1"
}
}
}
provider "mergify" {
# Token resolution order:
# 1. the `token` attribute set here
# 2. the MERGIFY_TOKEN environment variable
# 3. the GITHUB_TOKEN environment variable
}

Then run:

Terminal window
terraform init

The provider authenticates against the Mergify API with a bearer token. Both Mergify application keys and GitHub personal access tokens are accepted.

Tokens are read from, in priority order:

  1. The token attribute on the provider block.
  2. The MERGIFY_TOKEN environment variable.
  3. The GITHUB_TOKEN environment variable.

Manage which Mergify products are enabled on a single GitHub repository.

resource "mergify_repository_products" "monorepo" {
owner = "Mergifyio"
repository = "monorepo"
products = ["merge_queue", "merge_protections", "ci_insights", "workflow_automation"]
}

The products attribute is declarative — applying the resource sets the exact set of enabled products on the repository, removing any that are not listed.

mergify_organization_default_products

Section titled mergify_organization_default_products

Manage the default set of Mergify products enabled when a new repository of an organization is discovered by Mergify.

resource "mergify_organization_default_products" "defaults" {
organization = "Mergifyio"
products = ["merge_queue", "merge_protections"]
}

This only affects new repositories. Existing ones are managed via mergify_repository_products.

Was this page helpful?