Skip to content

Commit

Permalink
fix: extra_atlantis_dependencies using project-hcl-files
Browse files Browse the repository at this point in the history
  • Loading branch information
tafkam committed Feb 4, 2023
1 parent 8b2463c commit 7470166
Show file tree
Hide file tree
Showing 14 changed files with 242 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ func createHclProject(sourcePaths []string, workingDir string, projectHcl string

if locals.ExtraAtlantisDependencies != nil {
for _, dep := range locals.ExtraAtlantisDependencies {
relDep, err := filepath.Rel(workingDir, dep)
absolutePath := dep
if !filepath.IsAbs(absolutePath) {
absolutePath = filepath.Join(workingDir, dep)
}
relDep, err := filepath.Rel(workingDir, absolutePath)
if err != nil {
return nil, err
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,12 @@ func TestWithExecutionOrderGroups(t *testing.T) {
"--execution-order-groups",
})
}

func TestEnvHCLProjectMarkerRelDeps(t *testing.T) {
runTest(t, filepath.Join("golden", "project_marker_reldep.yaml"), []string{
"--root",
filepath.Join("..", "test_examples", "project_hcl_with_project_marker_reldep"),
"--project-hcl-files=environment.hcl",
"--use-project-markers=true",
})
}
22 changes: 21 additions & 1 deletion cmd/golden/envhcl_allchilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ projects:
- ../terragrunt.hcl
- ../someRandomDir/terragrunt.hcl
dir: hcl_json/json_expanded
workflow: terragruntjson
workflow: terragruntjson
- autoplan:
enabled: false
when_modified:
Expand Down Expand Up @@ -565,6 +565,26 @@ projects:
- ../../region.hcl
- ../env.hcl
dir: project_hcl_with_project_marker/non-prod/us-east-1/stage/webserver-cluster
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../../terragrunt.hcl
- ../../../account.hcl
- ../../region.hcl
- ../environment.hcl
dir: project_hcl_with_project_marker_reldep/non-prod/us-east-1/qa/mysql
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../../terragrunt.hcl
- ../../../account.hcl
- ../../region.hcl
- ../environment.hcl
dir: project_hcl_with_project_marker_reldep/non-prod/us-east-1/qa/webserver-cluster
- autoplan:
enabled: false
when_modified:
Expand Down
22 changes: 21 additions & 1 deletion cmd/golden/envhcl_externalchilds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ projects:
- ../terragrunt.hcl
- ../someRandomDir/terragrunt.hcl
dir: hcl_json/json_expanded
workflow: terragruntjson
workflow: terragruntjson
- autoplan:
enabled: false
when_modified:
Expand Down Expand Up @@ -408,6 +408,26 @@ projects:
- ../region.hcl
dir: project_hcl_with_project_marker/non-prod/us-east-1/stage
workflow: workflowSpecifiedInParent
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../../terragrunt.hcl
- ../../../account.hcl
- ../../region.hcl
- ../environment.hcl
dir: project_hcl_with_project_marker_reldep/non-prod/us-east-1/qa/mysql
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- ../../../../terragrunt.hcl
- ../../../account.hcl
- ../../region.hcl
- ../environment.hcl
dir: project_hcl_with_project_marker_reldep/non-prod/us-east-1/qa/webserver-cluster
- autoplan:
enabled: false
when_modified:
Expand Down
21 changes: 21 additions & 0 deletions cmd/golden/project_marker_reldep.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
automerge: false
parallel_apply: true
parallel_plan: true
projects:
- autoplan:
enabled: false
when_modified:
- '*.hcl'
- '*.tf*'
- '**/*.hcl'
- '**/*.tf*'
- ../../../terragrunt.hcl
- ../../account.hcl
- ../region.hcl
- ../../arbitrary.hcl
- ../stage/**/*.hcl
- internaldep/*.json
- ../../../externaldep.hcl
dir: non-prod/us-east-1/qa
workflow: anotherWorkflowSpecifiedInParent
version: 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals{
key = "value"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Set account-wide variables. These are automatically pulled in to configure the remote state bucket in the root
# terragrunt.hcl configuration.
locals {
account_name = "non-prod"
aws_account_id = "replaceme" # TODO: replace me with your AWS account ID!
aws_profile = "non-prod"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
locals {
some = "stuff"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Set common variables for the environment. This is automatically pulled in in the root terragrunt.hcl configuration to
# feed forward to the child modules.
locals {
environment = "qa"
extra_atlantis_dependencies = [
find_in_parent_folders("arbitrary.hcl"),
"${dirname(find_in_parent_folders("account.hcl"))}/us-east-1/stage/**/*.hcl",
"./internaldep/*.json",
"../../../externaldep.hcl",
]
atlantis_workflow = "anotherWorkflowSpecifiedInParent"
atlantis_project = true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"key": "value"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
locals {
# Automatically load environment-level variables
environment_vars = read_terragrunt_config(find_in_parent_folders("environment.hcl"))

# Extract out common variables for reuse
env = local.environment_vars.locals.environment
}

# Terragrunt will copy the Terraform configurations specified by the source parameter, along with any files in the
# working directory, into a temporary folder, and execute your Terraform commands in that folder.
terraform {
source = "git::git@github.com:gruntwork-io/terragrunt-infrastructure-modules-example.git//mysql?ref=v0.3.0"
}

# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders()
}

# These are the variables we have to pass in to use the module specified in the terragrunt configuration above
inputs = {
name = "mysql_${local.env}"
instance_class = "db.t2.micro"

allocated_storage = 20
storage_type = "standard"

master_username = "admin"
# TODO: To avoid storing your DB password in the code, set it as the environment variable TF_VAR_master_password
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
locals {
# Automatically load environment-level variables
environment_vars = read_terragrunt_config(find_in_parent_folders("environment.hcl"))

# Extract out common variables for reuse
env = local.environment_vars.locals.environment
}

# Terragrunt will copy the Terraform configurations specified by the source parameter, along with any files in the
# working directory, into a temporary folder, and execute your Terraform commands in that folder.
terraform {
source = "git::git@github.com:gruntwork-io/terragrunt-infrastructure-modules-example.git//asg-elb-service?ref=v0.3.0"
}

# Include all settings from the root terragrunt.hcl file
include {
path = find_in_parent_folders()
}

# These are the variables we have to pass in to use the module specified in the terragrunt configuration above
inputs = {
name = "webserver-example-${local.env}"
instance_type = "t2.micro"

min_size = 2
max_size = 2

server_port = 8080
elb_port = 80
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Set common variables for the region. This is automatically pulled in in the root terragrunt.hcl configuration to
# configure the remote state bucket and pass forward to the child modules as inputs.
locals {
aws_region = "us-east-1"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# ---------------------------------------------------------------------------------------------------------------------
# TERRAGRUNT CONFIGURATION
# Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules,
# remote state, and locking: https://github.com/gruntwork-io/terragrunt
# ---------------------------------------------------------------------------------------------------------------------

locals {
extra_atlantis_dependencies = [
find_in_parent_folders("account.hcl"),
find_in_parent_folders("region.hcl"),
find_in_parent_folders("environment.hcl"),
]

# Automatically load account-level variables
account_vars = read_terragrunt_config(find_in_parent_folders("account.hcl"))

# Automatically load region-level variables
region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))

# Automatically load environment-level variables
environment_vars = read_terragrunt_config(find_in_parent_folders("environment.hcl"))

# Extract the variables we need for easy access
account_name = local.account_vars.locals.account_name
account_id = local.account_vars.locals.aws_account_id
aws_region = local.region_vars.locals.aws_region
}

# Generate an AWS provider block
generate "provider" {
path = "provider.tf"
if_exists = "overwrite_terragrunt"
contents = <<EOF
provider "aws" {
region = "${local.aws_region}"
# Only these AWS Account IDs may be operated on by this template
allowed_account_ids = ["${local.account_id}"]
}
EOF
}

# Configure Terragrunt to automatically store tfstate files in an S3 bucket
remote_state {
backend = "s3"
config = {
encrypt = true
bucket = "${get_env("TG_BUCKET_PREFIX", "")}terragrunt-example-terraform-state-${local.account_name}-${local.aws_region}"
key = "${path_relative_to_include()}/terraform.tfstate"
region = local.aws_region
dynamodb_table = "terraform-locks"
}
generate = {
path = "backend.tf"
if_exists = "overwrite_terragrunt"
}
}


# ---------------------------------------------------------------------------------------------------------------------
# GLOBAL PARAMETERS
# These variables apply to all configurations in this subfolder. These are automatically merged into the child
# `terragrunt.hcl` config via the include block.
# ---------------------------------------------------------------------------------------------------------------------

# Configure root level variables that all resources can inherit. This is especially helpful with multi-account configs
# where terraform_remote_state data sources are placed directly into the modules.
inputs = merge(
local.account_vars.locals,
local.region_vars.locals,
local.environment_vars.locals,
)

0 comments on commit 7470166

Please sign in to comment.