gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.01k stars 971 forks source link

Support for go-getter's subdirectory glob patterns #2364

Open sybereal opened 1 year ago

sybereal commented 1 year ago

go-getter supports using glob patterns in the subdirectory part after the double-slash, as long as there is only one match. This is incredibly useful when dealing with repository tar balls, as these usually have a top-level directory wrapping the repository contents.

My use case is the following: to reduce external dependencies (Git), we wanted to configure Terragrunt to download our module sources as a tar ball from the GitHub API using the https://api.github.com/repos/:org/:repo/tarball/:ref API, using a fine-grained PAT as the basic auth password to limit access to only the modules repository.

The problem is that the tar balls this API produces have a top-level directory name following the pattern ${org}-${repo}-${commit-sha}, even when the ref is a branch or tag. For go-getter itself, this isn't an issue. I ran go-getter https://dummy:$GITHUB_TOKEN@api.github.com/repos/org/repo/tarball/vX.Y.Z//org-repo-*/submodule?archive=tgz submodule and it worked perfectly fine; the submodule directory contained the correct files from that tag.

However, when attempting to use an URL like this with Terragrunt, it fails with Working dir org-repo-*/submodule from source https://dummy:$GITHUB_TOKEN@api.github.com/repos/org/repo/tarball/vX.Y.Z//org-repo-*/submodule?archive=tgz does not exist.

Minimal repro

I'm using the https://github.com/hashicorp/learn-terraform-modules-create repo because it was the first example repository I found. I've used comments to represent the command outputs to simplify pasting.

go-getter 'https://api.github.com/repos/hashicorp/learn-terraform-modules-create/tarball/main//hashicorp-learn-terraform-modules-create-*?archive=tgz' test
# 2022/11/17 12:50:54 success!
ls test
# LICENSE  main.tf  modules  outputs.tf  README.md  variables.tf
cat <<EOF > terragrunt.hcl
terraform {
    source = "https://api.github.com/repos/hashicorp/learn-terraform-modules-create/tarball/main//hashicorp-learn-terraform-modules-create-*?archive=tgz"
}
EOF
terragrunt apply
# ERRO[0001] Working dir hashicorp-learn-terraform-modules-create-* from source https://api.github.com/repos/hashicorp/learn-terraform-modules-create/tarball/main?archive=tgz does not exist
# ERRO[0001] Unable to determine underlying exit code, so Terragrunt will exit with error code 1
gqrlt1207 commented 1 year ago

According to the comment in the source code, the source url should use Terraform's module source syntax

for example: module "consul" { source = "github.com/hashicorp/example" }

Therefore, the url, which works for go-getter, may not work for terragrunt.