Skip to content

Commit 0706965

Browse files
feat(tf): update the tf:init command to work for TF dynamic backends (#40)
## what - We're modernized our TF backend strategy to use a single dynamic backend file. - Therefore, we can remove the backend file switching and simplify the `tf:init` command - Additionally, revised the task `desc` and `summary` attributes to highlight ability to use either Terraform or OpenTofu as a follow up to #38 ## why ## references <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for OpenTofu as an alternative tool to Terraform for infrastructure management, selectable via configuration. * **Improvements** * Simplified backend configuration handling by removing fixed backend-file requirements. * Introduced new public configuration variable for specifying custom Terraform variables path location. * Updated task naming conventions and descriptions to reflect dual tool support. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 71d42fe commit 0706965

1 file changed

Lines changed: 29 additions & 30 deletions

File tree

lib/tf/Taskfile.yml

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
version: "3"
22

3-
vars:
4-
BACKEND_CONFIG_PATH: '{{.BACKEND_CONFIG_PATH | default "./backend-configurations"}}'
5-
TFVARS_PATH: '{{.TFVARS_PATH | default "./tfvars"}}'
6-
73
tasks:
84
setup:
95
desc: Internal task to templatize the variables for the other tasks.
@@ -13,44 +9,45 @@ tasks:
139
`.CLI_ARGS` is not yet available, that's why we have to duplicate the variables for every task.
1410
2. Taskfile commands run in their own shell environments, so if you set an environment variable in one task,
1511
it won't persist after that task completes or be available in other tasks.
12+
3. The `TF_CMD` variable dynamically selects between Terraform and OpenTofu based on the `USE_TERRAFORM`
13+
environment variable (set to "true" for Terraform, otherwise defaults to OpenTofu).
14+
4. Tasks execute in `{{.USER_WORKING_DIR}}`, which should be provided by the including Taskfile.
1615
silent: true
1716
internal: true
1817
vars: &vars
1918
WORKSPACE:
2019
sh: echo "{{.CLI_ARGS}}" | cut -d ' ' -f1 | xargs
20+
TFVARS_PATH:
21+
sh: echo "{{.TFVARS_PATH | default "./tfvars"}}"
2122
TF_ARGS:
2223
sh: echo "{{.CLI_ARGS}}" | cut -s -d ' ' -f2- | xargs
23-
BACKEND_CONFIG_FILE:
24-
sh: echo "{{.BACKEND_CONFIG_PATH}}/{{.WORKSPACE}}.backend.tf"
2524
TFVARS_FILE:
2625
sh: echo "{{.TFVARS_PATH}}/{{.WORKSPACE}}.tfvars"
2726
TF_CMD:
2827
sh: '[ "${USE_TERRAFORM}" = "true" ] && echo "terraform" || echo "tofu"'
2928

3029
init:
31-
desc: Initialize Terraform working directory with a backend configuration file.
30+
desc: Initialize Terraform or OpenTofu working directory.
3231
summary: |
33-
Initializes Terraform directory using the backend config file for the specified environment.
34-
The `terraform init` arguments can be optionally passed in.
35-
Usage: task terraform:init -- ENVIRONMENT [terraform init arguments]
36-
Example: task terraform:init -- automation
32+
Initializes a Terraform or OpenTofu directory (selected via USE_TERRAFORM environment variable).
33+
The `terraform init` or `tofu init` arguments can be optionally passed in.
34+
Usage: task tf:init -- [optional init arguments]
35+
Example: task tf:init --
3736
dir: "{{.USER_WORKING_DIR}}"
3837
silent: true
3938
vars: *vars
40-
preconditions:
41-
- sh: test -f {{.BACKEND_CONFIG_FILE}}
42-
msg: "Backend configuration file does not exist: {{.BACKEND_CONFIG_FILE}}"
4339
cmds:
44-
- "{{.TF_CMD}} init -backend-config {{.BACKEND_CONFIG_FILE}} {{.TF_ARGS}}"
40+
- "{{.TF_CMD}} init {{.TF_ARGS}}"
4541

4642
plan:
47-
desc: Generate a Terraform execution plan Terraform loading variable values from the given file.
43+
desc: Generate a Terraform or OpenTofu execution plan, loading variable values from the given file.
4844
summary: |
49-
Generates a Terraform execution plan for a specified environment, using a file to load the variables.
50-
The `terraform plan` arguments can be optionally passed in.
45+
Generates a Terraform or OpenTofu execution plan for a specified environment, using a file to load the variables.
46+
The tool (Terraform or OpenTofu) is selected via the USE_TERRAFORM environment variable.
47+
The `terraform plan` or `tofu plan` arguments can be optionally passed in.
5148
Requires a variables file specific to the environment to be present.
52-
Usage: task terraform:plan -- ENVIRONMENT [terraform plan arguments]
53-
Example: task terraform:plan -- automation
49+
Usage: task tf:plan -- ENVIRONMENT [terraform/tofu plan arguments]
50+
Example: task tf:plan -- automation
5451
dir: "{{.USER_WORKING_DIR}}"
5552
silent: true
5653
vars: *vars
@@ -62,13 +59,14 @@ tasks:
6259
- "{{.TF_CMD}} plan -var-file {{.TFVARS_FILE}} {{.TF_ARGS}}"
6360

6461
apply:
65-
desc: Create or update infrastructure according to Terraform configuration files in the current directory.
62+
desc: Create or update infrastructure according to Terraform or OpenTofu configuration files in the current directory.
6663
summary: |
67-
Applies a Terraform execution plan for a specific environment, using a file to load the variables.
68-
The `terraform apply` arguments can be optionally passed in.
64+
Applies a Terraform or OpenTofu execution plan for a specific environment, using a file to load the variables.
65+
The tool (Terraform or OpenTofu) is selected via the USE_TERRAFORM environment variable.
66+
The `terraform apply` or `tofu apply` arguments can be optionally passed in.
6967
Requires a variables file specific to the environment to be present.
70-
Usage: task terraform:apply -- ENVIRONMENT [terraform apply arguments]
71-
Example: task terraform:apply -- automation
68+
Usage: task tf:apply -- ENVIRONMENT [terraform/tofu apply arguments]
69+
Example: task tf:apply -- automation
7270
dir: "{{.USER_WORKING_DIR}}"
7371
silent: true
7472
vars: *vars
@@ -80,13 +78,14 @@ tasks:
8078
- "{{.TF_CMD}} apply -var-file {{.TFVARS_FILE}} {{.TF_ARGS}}"
8179

8280
refresh:
83-
desc: Refresh the Terraform state to match the real-world infrastructure (safer via apply -refresh-only).
81+
desc: Refresh the Terraform or OpenTofu state to match the real-world infrastructure (safer via apply -refresh-only).
8482
summary: |
85-
Refreshes the Terraform state for a specified environment, using a file to load the variables.
86-
Note: Upstream deprecates `terraform refresh`; use `terraform apply -refresh-only` to review changes before writing state.
83+
Refreshes the Terraform or OpenTofu state for a specified environment, using a file to load the variables.
84+
The tool (Terraform or OpenTofu) is selected via the USE_TERRAFORM environment variable.
85+
Note: Upstream deprecates `terraform refresh`; use `terraform apply -refresh-only` or `tofu apply -refresh-only` to review changes before writing state.
8786
Requires a variables file specific to the environment to be present.
88-
Usage: task terraform:refresh -- ENVIRONMENT [terraform apply -refresh-only arguments]
89-
Example: task terraform:refresh -- automation
87+
Usage: task tf:refresh -- ENVIRONMENT [terraform/tofu apply -refresh-only arguments]
88+
Example: task tf:refresh -- automation
9089
dir: "{{.USER_WORKING_DIR}}"
9190
silent: true
9291
vars: *vars

0 commit comments

Comments
 (0)