Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(terragrunt): add support for tfr prefix #26209

Merged
merged 17 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/modules/manager/terragrunt/__fixtures__/1.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#real
terraform {
extra_arguments "common_vars" {
commands = ["plan", "apply"]

arguments = [
"-var-file=../../common.tfvars",
"-var-file=../region.tfvars"
]
}

before_hook "before_hook" {
commands = ["apply", "plan"]
execute = ["echo", "Running Terraform"]
}

source = "tfr:///myuser/myrepo/cloud//folder/modules/moduleone?ref=v0.0.9"

after_hook "after_hook" {
commands = ["apply", "plan"]
execute = ["echo", "Finished running Terraform"]
run_on_error = true
}
}

#submodule
terraform {
source = "tfr:///terraform-google-modules/kubernetes-engine/google//modules/private-cluster?version=1.2.3"
}

#bar
terraform {
source = "tfr:///terraform-aws-modules/vpc/aws?version=3.3.0"
}

#missing third backslash
terraform {
source = "tfr://terraform-aws-modules/vpc/aws?version=3.3.0"
}
27 changes: 27 additions & 0 deletions lib/modules/manager/terragrunt/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ describe('modules/manager/terragrunt/extract', () => {
expect(extractPackageFile('nothing here')).toBeNull();
});

it('extracts terragrunt sources using tfr protocol', () => {
const res = extractPackageFile(Fixtures.get('1.hcl'));
expect(res).toEqual({
deps: [
{
currentValue: 'v0.0.9',
datasource: 'terraform-module',
depName: 'myuser/myrepo/cloud',
depType: 'terragrunt',
},
{
currentValue: '1.2.3',
datasource: 'terraform-module',
depName: 'terraform-google-modules/kubernetes-engine/google',
depType: 'terragrunt',
},
{
currentValue: '3.3.0',
datasource: 'terraform-module',
depName: 'terraform-aws-modules/vpc/aws',
depType: 'terragrunt',
},
{},
],
});
});

it('extracts terragrunt sources', () => {
const res = extractPackageFile(Fixtures.get('2.hcl'));
expect(res).toEqual({
Expand Down
14 changes: 14 additions & 0 deletions lib/modules/manager/terragrunt/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const githubRefMatchRegex = regEx(
export const gitTagsRefMatchRegex = regEx(
/(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<path>.*.*\/(?<project>.*\/.*)))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/,
);
export const tfrVersionMatchRegex = regEx(
/tfr:\/\/(?<registry>.*?)\/(?<org>[^/]+?)\/(?<name>[^/]+?)\/(?<cloud>[^/?]+).*\?(?:ref|version)=(?<currentValue>.*?)$/,
);
const hostnameMatchRegex = regEx(/^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/);

export function extractTerragruntModule(
Expand All @@ -35,6 +38,7 @@ export function analyseTerragruntModule(
const source = dep.managerData!.source;
const githubRefMatch = githubRefMatchRegex.exec(source ?? '');
const gitTagsRefMatch = gitTagsRefMatchRegex.exec(source ?? '');
const tfrVersionMatch = tfrVersionMatchRegex.exec(source ?? '');

if (githubRefMatch?.groups) {
dep.depType = 'github';
Expand All @@ -58,6 +62,16 @@ export function analyseTerragruntModule(
}
dep.currentValue = gitTagsRefMatch.groups.tag;
dep.datasource = GitTagsDatasource.id;
} else if (tfrVersionMatch?.groups) {
dep.depType = 'terragrunt';
dep.depName =
tfrVersionMatch.groups.org +
'/' +
tfrVersionMatch.groups.name +
'/' +
tfrVersionMatch.groups.cloud;
dep.currentValue = tfrVersionMatch.groups.currentValue;
dep.datasource = TerraformModuleDatasource.id;
} else if (source) {
const moduleParts = source.split('//')[0].split('/');
if (moduleParts[0] === '..') {
Expand Down