A Terraform plugin to manage files in any remote Git repository.
Available on the Terraform registry as arl-sh/git.
terraform {
required_providers {
git = {
source = "arl-sh/git"
version = "~> 0.3"
}
}
}
Then, run terraform init
.
# 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
}
# 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
}
# 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
}
The auth
block is supported on all data sources and resources.
# 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"
}
}
}
# 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"
}
}
}
# 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...=" ]
}
}
}
# 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...=" ]
}
}
}
Licensed under the Apache License, Version 2.0.
See the included LICENSE file for more details.