5

I started recently using terragrunt and have been wondering if it's possible to download module from specific branch not specfic tag ( or in addition to tag ) rather than by default master

Download specific tag from master:

terraform {
  source = "git@github.com:<repo>/infrastructure-modules.git//gcp/bucket?ref=v.0.0.1"
}

Download specific branch from repo ?

# Pseudo code
terraform {
  source = "git@github.com:<repo>/infrastructure-modules.git//gcp/bucket?ref=v.0.0.1%branch=test"
}
potatopotato
  • 1,024
  • 2
  • 16
  • 38
  • Did the `ref=` work? – VonC Jul 15 '20 at 08:48
  • nope, looks like tags work repository-wide , so I just made changes on branch, git add, git commit, git tag -a 'v1.branch', git push --follow-tags -u origin , and I could the tag reference to the branch not master – potatopotato Jul 15 '20 at 09:17
  • OK, good to know. I have edited the answer accordingly and included your comment for more vsibility. – VonC Jul 15 '20 at 10:00

2 Answers2

5

I was able to specify a branch using following:

terraform {
  source = "git@github.com:<repo>/infrastructure-modules.git//gcp/bucket?ref=branch-name"
}
German Dautin
  • 51
  • 1
  • 3
  • 1
    Interesting: is there any documentation supporting this syntax? – VonC Mar 05 '21 at 09:34
  • 3
    it's clearly explained here [link](https://www.terraform.io/docs/language/modules/sources.html#selecting-a-revision-1) The value of the ref argument can be any reference that would be accepted by the git checkout command, including branch and tag names. – German Dautin Mar 07 '21 at 08:17
  • @GermanDautin What happens if tag name is same as branch name. Just a curiosity. – Pankaj Devrani Apr 04 '23 at 06:09
1

Looking at the terragrunt cli/download_source_test.go source code, there is no apparent way to specify a branch.

That means you need to add a tag to that branch, and use that tag as ref.
That being said, check first if ref=<mybranch> works.

However, the OP potatopotato confirms in the comments that referencing the branch name directly does not work.

I just made changes on branch, and:

git add
 git commit
 git tag -a 'v1.branch'
 git push --follow-tags -u origin <branch_name>

and I could use the tag reference to the branch, not master.

And German Dautin's answer points to Terraform / Module Sources / Selecting a Revision.

By default, Terraform will clone and use the default branch (referenced by HEAD) in the selected repository. You can override this using the ref argument:

module "vpc" {
  source = "git::https://example.com/vpc.git?ref=v1.2.0"
}

The value of the ref argument can be any reference that would be accepted by the git checkout command, including branch and tag names.

So using a branch name is possible.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250