42

Using Terraform modules with a git branch as a source,
I am referring to:

git::ssh://private_server:myport/kbf/my_repository.git//ecs-cluster?ref=v0.0.1

In my module source parameter, this works great and provides me with my module at tag v0.0.1 on master.

However I'd like to specify a branch, not a tag, but am not sure how to do this.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
krystan honour
  • 6,523
  • 3
  • 36
  • 63

7 Answers7

79

As mentioned in the Terraform documentation here:

git::ssh://private_server:myport/kbf/my_repository.git//ecs-cluster?ref=myBranch
rahuljain1311
  • 1,822
  • 19
  • 20
13

You can use something like this:

module "test_module" {
  source = "git::ssh://bitbucketURL/my_repo.git?ref=BranchName"
}

bitbucketURL: Go to bitbucket UI, check clone URL, copy from it.

If you are using something other then bitbucket, please refer to: https://www.terraform.io/docs/modules/sources.html

Stephen
  • 3,607
  • 1
  • 27
  • 30
10

Exactly the same way. You have a generic ref there that Git will work out what you mean by context assuming no collisions.

If you do have 2 refs that are ambiguous then Git will error and tell you that it's an ambiguous ref and force you to specify the full ref using refs/heads/branch-name or refs/tags/tag-name.

ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
  • yes this is what i thought, I had an incorrect escape character in my branch ref so it errored. Accepting as this is the right answer ;) – krystan honour Sep 27 '18 at 14:24
  • Hello @krystanhonour . So how did you construct the url . git::ssh://private_server:myport/kbf/my_repository.git//ecs-cluster?ref=branch-name ??? or – Bala Apr 12 '19 at 15:09
9

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

Sample code to use the module using git tag and branch. reference_link

In simple language: just after ref= add the tag or branch as required.

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

module "vpc" {
    source = "git::https://example.com/vpc.git?ref=develop"
}
Stephen
  • 3,607
  • 1
  • 27
  • 30
Raghu Reddy
  • 312
  • 3
  • 7
4

I know this question is about private server repo, but in case someone is looking for unprefixed github.com urls, this works:

module "foo" {
  source = "github.com/org/repo//path/to/module?ref=branch-name"
}
iamolegga
  • 915
  • 8
  • 8
1

Just like mentioned before to reference a branch in your terraform module. All you need to do is after the ref= .. instead of mentioning the tag, mention the branch name.

So.. instead of

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

which is what you use to reference a tag in that repo... use the one below

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

Hope it is clear and it makes sense.

ApotiEri
  • 11
  • 1
1

I've just tried this and it is working well, even though the branch name includes a backslash.

source = "git::ssh://git@bitbucket.xxx.live/myrepo.git?ref=MyBranchName"