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:"))}")) |
This should use the git CLI without the external sh file
data "external" "git" {
program = [
"git",
"log",
"--pretty=format:{ \"sha\": \"%H\" }",
"-1",
"HEAD"
]
}
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
I encountered an error relating to not finding the file. Fixed it with a
chmod +x get_sha.sh