Dirs
Dirs can be used to describe which tag queries, workspaces, and when modified rules apply to a directory in your repository.
Configuration
Directories are configured in the .terrateam/config.yml
file under the dirs
key. Here’s the default configuration:
Directory configuration
Each directory consists of the directory’s name as a key and a map as a value. For example, the directory foobar
would have the following configuration:
The value map has the following attributes:
Key | Type | Description |
---|---|---|
create_and_select_workspace | Boolean | Select and create the workspace defined in the workspaces configuration. Default is true . |
tags | List | List of tags to assign the directory. |
workspaces | Workspaces | Workspace configuration. |
when_modified | When modified | Configuration to override when to match pull request file changes with autoplan and autoapply. |
Workspaces
The workspaces
configuration is an object where the object key is the name of the workspace and the value is its configuration. Unique custom tags can be created against a directory and workspace combination.
When modified
The when_modified
configuration can be set in the dirs
directive. This configuration is identical to the global when_modified
.
When specifying file_patterns
for a directory, the directory is not automatically included in the list of patterns. For example, the configuration below will only identify a change to dir1
if a file in the modules
directory is changed:
The following configuration will identify a change to dir1
if a file in the dir1
or modules
directory is changed:
Assigning tags
The tags
configuration is used to create a custom tag for a directory. When assigning tags to a directory, tag values can be used in any combination to trigger workflows or target resources with a tag query.
Example
Description:
- Tag
ec2
withaws, ec2
- Use workspace
production
when a tag query includesaws, ec2, production
for a workflow or command. - Override when modified file patterns for directory
ec2
The ${DIR}
variable
File globs specified in file_patterns
are always relative to the root of the repository. The internal ${DIR}
variable can be used to specify the directory that Terrateam is working against. For example, the following configuration would trigger Terrateam operations when foobar/*.tf
is matched in a pull request. The ${DIR}
variable can be used against directories specified by an exact path or glob.
Globs
In addition to specifying absolute path names, the dirs
directive supports glob patterns as well. This can be useful for repositories with a lot of directories that match a pattern with a similar configuration.
The Terrateam runtime environment will list all of the files in your repository and match them according to the directory glob pattern. A dirs
directive is then constructed from matched configurations as if the user had specified all of them by hand.
Example
Consider a repository with the following files:
_templates/ec2/terragrunt.hcl
prod/ec2/us-east-1/foo.tf
prod/ec2/us-west-1/foo.tf
prod/ebs/us-east-1/foo.tf
prod/ebs/us-west-1/foo.tf
A.terrateam/config.yml
configuration file:
Given the file list, the following dirs
directive will be automatically generated during every Terrateam operation:
Longest glob match
In the example above, the files in the prod/ec2
directory match two globs:
prod/**/ec2/**
prod/**
The globprod/**/ec2/**
is longer thanprod/**
and is considered the better match because the glob is more specific.
Directory globs match files (Terragrunt)
Globs can be expressed all the way to the file level. The directory of the file is then taken when constructing the dirs
directory.
For example, in a repository with a structure to be used with Terragrunt, one could have the following configuration:
This configuration would apply the following rules:
- Lines 2-4: Disable Terrateam operations in any directory under the
_templates/
directory with aterragrunt.hcl
file - Lines 5-8: Run Terrateam operations against a directory when:
- The directory contains a
terragrunt.hcl
file - The pull request changed files end in
hcl
,tf
, ortfvars
- The pull request changed files includes a
terragrunt.hcl
file in the_templates/
directory
- The directory contains a
Conclusion
By leveraging the dirs
directive in your Terrateam configuration, you can:
- Assign tags to specific directories
- Configure workspaces for specific directories
- Override When Modified rules for specific directories
- Use globs to match multiple directories with similar configurations
- Use the
${DIR}
variable to reference the current directory in file patterns
This allows you to fine-tune the behavior of Terrateam for different parts of your repository, making it easier to manage complex Terraform setups with multiple directories and workspaces.