Terraform
AdvanceTopics
DeployingInfrastructure
GitHub

GitHub Provider in Terraform

Overview

The GitHub Provider in Terraform allows you to manage your GitHub resources, such as repositories, teams, and collaborators, using infrastructure as code. This guide will walk you through the process of setting up the provider, creating a GitHub repository, and applying the changes.

Requirements

  • A GitHub account.
  • A GitHub Personal Access Token (PAT) with the necessary permissions.

Setup

  1. Create a GitHub Personal Access Token (PAT):

    • Navigate to GitHub Settings (opens in a new tab).
    • Click on "Generate new token".
    • Select the necessary scopes (e.g., repo for repository access).
    • Generate the token and keep it secure.
  2. Install Terraform:

Configuration

  1. Terraform Configuration File:

    Create a file named main.tf and add the following configuration:

    terraform {
      required_providers {
        github = {
          source  = "integrations/github"
          version = "~> 5.0"
        }
      }
    }
     
    provider "github" {
      token = "your-token-here"
    }
     
    resource "github_repository" "example" {
      name        = "example"
      description = "My awesome codebase"
      visibility  = "public"
    }

    Explanation:

    • terraform { required_providers { ... } }: Specifies the required provider and its version.
    • provider "github" { ... }: Configures the GitHub provider with your personal access token.
    • resource "github_repository" "example" { ... }: Defines a GitHub repository resource with the specified name, description, and visibility.
  2. Replace your-token-here with your actual GitHub Personal Access Token.

Initialize and Apply

  1. Initialize Terraform:

    • Open your terminal and navigate to the directory containing your main.tf file.

    • Run the following command to initialize Terraform and download the necessary provider plugins:

      terraform init
  2. Plan the Configuration:

    • Run the following command to create an execution plan. This will show you what actions Terraform will take:

      terraform plan
  3. Apply the Configuration:

    • Run the following command to apply the configuration and create the GitHub repository:

      terraform apply
    • Terraform will prompt you to confirm the action. Type yes and press Enter.

Example Output

After running terraform apply, you should see output similar to this:

github_repository.example: Creating...
github_repository.example: Creation complete after 3s [id=example]
 
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Verification

  1. Verify in GitHub:
    • Navigate to your GitHub account.
    • Check the repositories section to see the new repository named "example".

Additional Configuration

You can customize the repository further by adding more attributes. Here are a few examples:

resource "github_repository" "example" {
  name        = "example"
  description = "My awesome codebase"
  visibility  = "public"
  
  homepage_url = "https://example.com"
  has_issues   = true
  has_wiki     = true
  topics       = ["terraform", "github", "example"]
}

Cleaning Up

To remove the created repository, run:

terraform destroy

This will delete all the resources defined in your Terraform configuration.

Conclusion

By following these steps, you have successfully set up the GitHub provider in Terraform, created a repository, and applied the configuration. Terraform's infrastructure as code approach simplifies the management of GitHub resources, making it easier to automate and maintain your repositories.

If you need more information, refer to the official GitHub Provider documentation (opens in a new tab).


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!