/terraform-provider-git

Terraform provider for checking out Git repositories and making changes in them

Primary LanguageGoApache License 2.0Apache-2.0

terraform-provider-git   Build Status

A Terraform plugin to manage files in any remote Git repository.

Available on the Terraform registry as arl-sh/git.

Installation

terraform {
  required_providers {
    git = {
      source  = "arl-sh/git"
      version = "~> 0.3"
    }
  }
}

Then, run terraform init.

Data Sources

git_repository

# Read metadata of a Git repository
data "git_repository" "example_repo" {
  url = "https://example.com/repo-name"
}

output "head_sha" {
  value = data.git_repository.example_repo.head.sha
}

output "branch_names" {
  value = data.git_repository.example_repo.branches.*.name
}

output "branch_shas" {
  value = data.git_repository.example_repo.branches.*.sha
}

output "tag_names" {
  value = data.git_repository.example_repo.tags.*.name
}

output "tag_shas" {
  value = data.git_repository.example_repo.tags.*.sha
}

git_file

# Read an existing file in a Git repository
data "git_file" "example_read" {
  url  = "https://example.com/repo-name"
  ref  = "v1.0.0"
  path = "path/to/file.txt"
}

output "file_content" {
  value = data.git_file.example_read.content
}

Resources

git_commit

# Write to a list of files within a Git repository, then commit and push the changes
resource "git_commit" "example_write" {
  url            = "https://example.com/repo-name"
  branch         = "main"
  message        = "Create txt and JSON files"
  update_message = "Update txt and JSON files"
  delete_message = "Delete txt and JSON files"

  add {
    path    = "path/to/file.txt"
    content = "Hello, World!"
  }

  add {
    path    = "path/to/file.json"
    content = jsonencode({ hello = "world" })
  }

  prune = true
}

output "commit_sha" {
  value = git_commit.example_write.sha
}

output "is_new" {
  value = git_commit.example_write.new
}

Authentication

The auth block is supported on all data sources and resources.

HTTP Bearer

# Write to a list of files within a Git repository, then commit and push the changes
resource "git_commit" "example_write" {
  # ...

  auth {
    bearer {
      token = "example_token_123"
    }
  }
}

HTTP Basic

# Write to a list of files within a Git repository, then commit and push the changes
resource "git_commit" "example_write" {
  # ...

  auth {
    basic {
      username = "example"
      password = "123"
    }
  }
}

SSH (from file)

# Write to a list of files within a Git repository, then commit and push the changes
resource "git_commit" "example_write" {
  # ...

  auth {
    ssh_key {
      username         = "example"
      private_key_path = "/home/user/.ssh/id_rsa"
      password         = "key_passphrase_123"
      known_hosts      = [ "github.com ecdsa-sha2-nistp256 AAAA...=" ]
    }
  }
}

SSH (inline)

# Write to a list of files within a Git repository, then commit and push the changes
resource "git_commit" "example_write" {
  # ...

  auth {
    ssh_key {
      username = "example"
      private_key_pem = <<-EOT
      -----BEGIN RSA PRIVATE KEY-----
      ...
      -----END RSA PRIVATE KEY-----
      EOT
      password    = "key_passphrase_123"
      known_hosts = [ "github.com ecdsa-sha2-nistp256 AAAA...=" ]
    }
  }
}

License

Licensed under the Apache License, Version 2.0.
See the included LICENSE file for more details.