Created: 5/27/2022

git describe in Terraform

It's really easy to get the output of git describe --tags using Terraform's native external provider.

The external provider allows you to run shell commands for the underlying system. Terraform will let you use the output as long as the output is valid JSON. Therefore, you can get the git-tag-derived version inside Terraform with a simple wrapper script:


# git_describe.sh
# terraform programs must output valid JSON

echo '{"output": "'"$(git describe --tags)"'"}'

And you can define it in Terraform like this:

data "external" "git_describe" {
  program = ["sh", "scripts/git_describe.sh"]

your thoughts?