Skip to content

Instantly share code, notes, and snippets.

@mattmahn
Last active March 26, 2024 23:26
Show Gist options
  • Save mattmahn/f46420ba8d740cc008accfde964c2e74 to your computer and use it in GitHub Desktop.
Save mattmahn/f46420ba8d740cc008accfde964c2e74 to your computer and use it in GitHub Desktop.
Terraform: Get current Git commit SHA

This will return the full Git commit SHA of whichever branch is currently checkedout.

Dump the snippet into terraform console (run from within a Git repo) to see for yourself.

trimspace(file(".git/${trimspace(trimprefix(file(".git/HEAD"), "ref:"))}"))
@joeshaw
Copy link

joeshaw commented Aug 30, 2023

If you are using Terraform Cloud or Terraform Enterprise you can use the TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA environment variable.

variable "TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA" {
  type = string
}

and reference it as vars.TFC_CONFIGURATION_VERSION_GIT_COMMIT_SHA

https://developer.hashicorp.com/terraform/cloud-docs/run/run-environment#environment-variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment