A supplemental Terraform provider offering extended GitHub capabilities alongside the official provider, built using the Terraform Plugin Framework.
- User Information: Query GitHub user information and profiles
- Repository Management: Create and manage GitHub repositories with extended capabilities
- Branch Management: Create and manage repository branches
- File Management: Create, update, and manage files in repositories
- Pull Request Automation: Create pull requests with auto-merge capabilities, including automatic approval and merge when ready
- Data Sources: Query GitHub users, repositories, branches, and files
- Extensible: Designed to complement the official GitHub provider with additional capabilities
Add the provider to your Terraform configuration:
terraform {
required_providers {
githubx = {
source = "tfstack/githubx"
version = "~> 0.1"
}
}
}
provider "githubx" {
token = var.github_token
}-
Clone the repository:
git clone https://github.com/tfstack/terraform-provider-githubx.git cd terraform-provider-githubx -
Build the provider:
go install
-
Install the provider to your local Terraform plugins directory:
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/tfstack/githubx/0.1.0/linux_amd64 cp $GOPATH/bin/terraform-provider-githubx ~/.terraform.d/plugins/registry.terraform.io/tfstack/githubx/0.1.0/linux_amd64/
The provider supports various configuration options for authentication, GitHub Enterprise Server, and development/testing.
The provider supports multiple authentication methods for higher rate limits and access to private resources.
provider "githubx" {
token = "your-github-token-here"
}provider "githubx" {
oauth_token = "your-oauth-token-here"
}Set the GITHUB_TOKEN environment variable:
export GITHUB_TOKEN="your-github-token-here"Then use the provider without the token attribute:
provider "githubx" {
# token will be read from GITHUB_TOKEN environment variable
}If you have the GitHub CLI (gh) installed and authenticated, the provider will automatically use your GitHub CLI token:
# Authenticate with GitHub CLI (if not already done)
gh auth loginThen use the provider without any configuration:
provider "githubx" {
# token will be automatically retrieved from 'gh auth token'
}For GitHub App authentication, you need to provide the App ID, Installation ID, and path to the private key PEM file:
provider "githubx" {
app_auth {
id = 123456
installation_id = 789012
pem_file = "/path/to/private-key.pem"
}
}Note: The provider checks for authentication in this order:
- Provider
tokenattribute (Personal Access Token) - Provider
oauth_tokenattribute GITHUB_TOKENenvironment variable- GitHub CLI (
gh auth token) - GitHub App authentication (
app_authblock) - Unauthenticated (if none of the above are available)
Development Container: If you're using the devcontainer, the host OS GitHub CLI authentication is automatically mounted and will be used by the provider. You only need to authenticate once on your host OS with gh auth login, and it will persist across container rebuilds.
- Go to GitHub Settings > Developer settings > Personal access tokens
- Click "Generate new token" (classic) or "Generate new token (fine-grained)"
- Select the scopes/permissions you need
- Click "Generate token"
- Copy the token immediately (it won't be shown again)
Note: For most data sources, a token is optional but recommended. Without a token, you'll be limited to 60 requests/hour. With a token, you get 5,000 requests/hour.
GitHub App authentication is useful for CI/CD pipelines and automated workflows. To use it:
- Create a GitHub App in your organization or personal account
- Install the App in the repositories or organization where you need access
- Generate a private key for the App (download the PEM file)
- Configure the provider with the App ID, Installation ID, and path to the PEM file
The provider will automatically:
- Generate a JWT token using the private key
- Exchange it for an installation access token
- Use the installation token for API requests
Note: Installation tokens are automatically refreshed as needed (they expire after 1 hour).
The provider supports GitHub Enterprise Server (GHES) by configuring a custom base URL:
provider "githubx" {
base_url = "https://github.example.com/api/v3/"
}Or via environment variable:
export GITHUB_BASE_URL="https://github.example.com/api/v3/"Default: https://api.github.com/
Specify the GitHub owner (user or organization) to manage:
provider "githubx" {
owner = "my-organization"
}Or via environment variable:
export GITHUB_OWNER="my-organization"This can be useful for:
- Multi-organization scenarios
- Resource scoping
- Validation and defaults
Enable insecure mode for testing with self-signed certificates:
provider "githubx" {
insecure = true
}Or via environment variable:
export GITHUB_INSECURE="true"Warning: Only use this in development/testing environments. It disables TLS certificate verification.
- Unauthenticated: 60 requests/hour
- Authenticated: 5,000 requests/hour
The provider will work without a token, but with very limited rate limits. For testing, this is usually sufficient for a few queries.
Here's a simple example to get you started:
terraform {
required_providers {
githubx = {
source = "tfstack/githubx"
version = "~> 0.1"
}
}
}
provider "githubx" {
# Token will be read from GITHUB_TOKEN environment variable
}
data "githubx_user" "example" {
username = "octocat"
}
output "user" {
value = data.githubx_user.example
}For more examples, see the examples/ directory.
githubx_user- Retrieves information about a GitHub usergithubx_repository- Retrieves information about a GitHub repositorygithubx_repository_branch- Retrieves information about a GitHub repository branchgithubx_repository_file- Retrieves information about a file in a GitHub repository
githubx_repository- Creates and manages a GitHub repositorygithubx_repository_branch- Creates and manages a GitHub repository branchgithubx_repository_file- Creates and manages files in a GitHub repositorygithubx_repository_pull_request_auto_merge- Creates and manages a GitHub pull request with optional auto-merge capabilities
When developing in the devcontainer, you can test the provider locally using the following steps:
Build the provider binary:
make build
# or
go build -o terraform-provider-githubx -buildvcs=falseInstall the provider to Terraform's local plugin directory so Terraform can find it:
Option A: Using Make (Recommended)
make install-localOption B: Manual installation
# Create the plugin directory structure
mkdir -p ~/.terraform.d/plugins/registry.terraform.io/tfstack/githubx/0.1.0/linux_amd64
# Copy the built binary
cp terraform-provider-githubx ~/.terraform.d/plugins/registry.terraform.io/tfstack/githubx/0.1.0/linux_amd64/Note: The version number (0.1.0) should match the version in your Terraform configuration's required_providers block.
Option A: Initialize all examples automatically
make init-examplesThis will:
- Build and install the provider locally
- Initialize Terraform in all example directories
- Skip examples that require variables (you'll need to set those manually)
Option B: Initialize a specific example
make init-example EXAMPLE=examples/data-sources/githubx_userOption C: Manual initialization
Navigate to the example directory and initialize manually:
cd examples/data-sources/githubx_user
terraform initAfter initialization, navigate to any example directory and test the provider:
cd examples/data-sources/githubx_user
# Option 1: Use .env file (recommended - edit .env with your values)
# Copy .env.example to .env and fill in your values, then:
source .env
# Option 2: Set environment variables manually
# Set your GitHub token (optional but recommended)
export GITHUB_TOKEN="your-github-token-here"
# Plan to see what Terraform will do
terraform plan
# Apply to test the provider
terraform applyYou should see output like:
Outputs:
user_bio = "GitHub's mascot"
user_id = 583231
user_login = "octocat"
user_name = "The Octocat"
user_public_repos = 8
Run the unit tests:
make test
# or
go test -v ./...Generate a test coverage report:
Option A: Using Make (Recommended)
make test-coverageThis will:
- Run tests with coverage
- Display coverage summary in the terminal
- Generate an HTML coverage report (
coverage.html)
Option B: Manual commands
# Generate coverage profile
go test -coverprofile=coverage.out ./...
# View coverage report in terminal
go tool cover -func=coverage.out
# Generate HTML coverage report
go tool cover -html=coverage.out -o coverage.html
# View coverage for specific package
go test -cover ./internal/provider/Coverage Options:
-coverprofile=coverage.out- Generate coverage profile file-covermode=count- Show how many times each statement was executed (default:set)-covermode=atomic- Same as count but thread-safe (useful for parallel tests)-coverpkg=./...- Include coverage for all packages, not just tested ones
Example output:
github.com/tfstack/terraform-provider-githubx/internal/provider/data_source_user.go:Metadata 100.0%
github.com/tfstack/terraform-provider-githubx/internal/provider/data_source_user.go:Schema 100.0%
...
total: (statements) 85.5%
Acceptance tests make real API calls to GitHub. Set the TF_ACC environment variable to enable them:
export GITHUB_TOKEN="your-github-token-here"
export TF_ACC=1
make testacc
# or
TF_ACC=1 go test -v ./...Warning: Acceptance tests make real API calls to GitHub. Use a test token and be mindful of rate limits.
Helper scripts are available to automate common tasks:
Install Provider Locally:
make install-localInitialize All Examples:
make init-examplesInitialize Specific Example:
make init-example EXAMPLE=examples/data-sources/githubx_user- Provider not found: Ensure the version in your Terraform config matches the directory version (
0.1.0) - Permission denied: Make sure the plugin directory is writable:
chmod -R 755 ~/.terraform.d/plugins/ - Provider version mismatch: Update the version in your Terraform config or rename the plugin directory to match
- Rate limit errors: Set the
GITHUB_TOKENenvironment variable for higher rate limits (5,000 requests/hour vs 60 requests/hour) - Connection errors: Verify your token is correct and has the necessary permissions
Comprehensive examples are available in the examples/ directory:
- Data Sources: See
examples/data-sources/for examples of querying GitHub resourcesgithubx_user- Query user informationgithubx_repository- Query repository informationgithubx_repository_branch- Query branch informationgithubx_repository_file- Query file content and metadata
- Resources: See
examples/resources/for examples of managing GitHub resourcesgithubx_repository- Create and manage repositoriesgithubx_repository_branch- Create and manage branchesgithubx_repository_file- Create and manage filesgithubx_repository_pull_request_auto_merge- Create pull requests with auto-merge
- Provider: See
examples/provider/for a simple provider example
Each example includes a data-source.tf, resource.tf, or provider.tf file with working Terraform configuration.
- Rate Limits: Without authentication, you're limited to 60 requests/hour. Use a token for 5,000 requests/hour.
- Scope: This provider is designed to supplement the official GitHub provider, not replace it. Use it for extended capabilities alongside the official provider.
Full documentation for all data sources and resources is available in the docs/ directory:
See CONTRIBUTING.md for information on developing the provider.
- Issues: Report bugs and request features on GitHub Issues
- Discussions: Ask questions and share ideas on GitHub Discussions
This project is licensed under the MIT License - see the LICENSE file for details.