Skip to content

Commit b7f86e8

Browse files
committed
feat: moves Terraform + mixin tasks to where they belong
1 parent 9c02c6c commit b7f86e8

File tree

3 files changed

+119
-4
lines changed

3 files changed

+119
-4
lines changed

Taskfile.yaml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
version: "3"
22

33
includes:
4-
aqua: lib/aqua/Taskfile.yaml
5-
aws: lib/aws/Taskfile.yaml
6-
snaplet: lib/snaplet/Taskfile.yaml
7-
toolbox: lib/toolbox/Taskfile.yaml
4+
aqua: lib/aqua
5+
aws: lib/aws
6+
mixins: lib/mixins
7+
snaplet: lib/snaplet
8+
terraform:
9+
taskfile: lib/terraform
10+
aliases: [tf]
11+
toolbox: lib/toolbox
812

913
tasks: {}

lib/mixins/Taskfile.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: "3"
2+
3+
tasks:
4+
update-all:
5+
desc: "Update a mixin across the components. Example: `task mixins:update-all -- context.tf`."
6+
preconditions:
7+
- sh: test -f ./mixins/{{.CLI_ARGS}}
8+
msg: "File does not exist: ./mixins/{{.CLI_ARGS}}"
9+
- sh: test -d ./components && [ "$(ls -A ./components)" ]
10+
msg: ./components/ directory is empty or does not exist.
11+
sources:
12+
- ./mixins/{{.CLI_ARGS}}
13+
generates:
14+
- ./components/*/{{.CLI_ARGS}}
15+
cmds:
16+
- cmd: |
17+
for comp in ./components/*; do
18+
cp -v ./mixins/{{.CLI_ARGS}} $comp/{{.CLI_ARGS}}
19+
done;
20+
silent: true
21+
22+
pull-sops:
23+
desc: Pull SOPS secrets mixin from its external source into the working directory.
24+
dir: "{{.USER_WORKING_DIR}}"
25+
silent: true
26+
env:
27+
MIXIN_FILE: secrets.sops.tf
28+
cmds:
29+
- echo "Pulling secrets mixin from 'masterpointio/terraform-secrets-helper' Git repo..."
30+
- |
31+
if curl -sL https://raw.githubusercontent.com/masterpointio/terraform-secrets-helper/main/exports/$MIXIN_FILE -o $MIXIN_FILE; then
32+
echo "File $MIXIN_FILE was successfully pulled."
33+
else
34+
echo "File $MIXIN_FILE pull failed."
35+
fi;

lib/terraform/Taskfile.yml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
version: "3"
2+
3+
vars:
4+
BACKEND_CONFIG_PATH: '{{.BACKEND_CONFIG_PATH | default "./backend-configurations"}}'
5+
TFVARS_PATH: '{{.TFVARS_PATH | default "./tfvars"}}'
6+
7+
tasks:
8+
setup:
9+
desc: Internal task to templatize the variables for the other tasks.
10+
summary: |
11+
This is a workaround for the lack of support for sharing variables between the tasks for the sake of DRY.
12+
1. Global vars are being evaluated when the Taskfile.yml is first loaded and at that time,
13+
`.CLI_ARGS` is not yet available, that's why we have to duplicate the variables for every task.
14+
2. Taskfile commands run in their own shell environments, so if you set an environment variable in one task,
15+
it won't persist after that task completes or be available in other tasks.
16+
silent: true
17+
internal: true
18+
vars: &vars
19+
ENV:
20+
sh: echo "{{.CLI_ARGS}}" | cut -d ' ' -f1 | xargs
21+
TF_ARGS:
22+
sh: echo "{{.CLI_ARGS}}" | cut -s -d ' ' -f2- | xargs
23+
BACKEND_CONFIG_FILE:
24+
sh: echo "{{.BACKEND_CONFIG_PATH}}/{{.ENV}}.backend.tf"
25+
TFVARS_FILE:
26+
sh: echo "{{.TFVARS_PATH}}/{{.ENV}}.tfvars"
27+
28+
init:
29+
desc: Initialize Terraform working directory with a backend configuration file.
30+
summary: |
31+
Initializes Terraform directory using the backend config file for the specified environment.
32+
The `terraform init` arguments can be optionally passed in.
33+
Usage: task terraform:init -- ENVIRONMENT [terraform init arguments]
34+
Example: task terraform:init -- automation
35+
dir: "{{.USER_WORKING_DIR}}"
36+
silent: true
37+
vars: *vars
38+
preconditions:
39+
- sh: test -f {{.BACKEND_CONFIG_FILE}}
40+
msg: "Backend configuration file does not exist: {{.BACKEND_CONFIG_FILE}}"
41+
cmds:
42+
- terraform init -backend-config {{.BACKEND_CONFIG_FILE}} {{.TF_ARGS}}
43+
44+
plan:
45+
desc: Generate a Terraform execution plan Terraform loading variable values from the given file.
46+
summary: |
47+
Generates a Terraform execution plan for a specified environment, using a file to load the variables.
48+
The `terraform plan` arguments can be optionally passed in.
49+
Requires a variables file specific to the environment to be present.
50+
Usage: task terraform:plan -- ENVIRONMENT [terraform plan arguments]
51+
Example: task terraform:plan -- automation
52+
dir: "{{.USER_WORKING_DIR}}"
53+
silent: true
54+
vars: *vars
55+
preconditions:
56+
- sh: test -f {{.TFVARS_FILE}}
57+
msg: "Variables file does not exist: {{.TFVARS_FILE}}"
58+
cmds:
59+
- terraform plan -var-file {{.TFVARS_FILE}} {{.TF_ARGS}}
60+
61+
apply:
62+
desc: Create or update infrastructure according to Terraform configuration files in the current directory.
63+
summary: |
64+
Applies a Terraform execution plan for a specific environment, using a file to load the variables.
65+
The `terraform apply` arguments can be optionally passed in.
66+
Requires a variables file specific to the environment to be present.
67+
Usage: task terraform:apply -- ENVIRONMENT [terraform apply arguments]
68+
Example: task terraform:apply -- automation
69+
dir: "{{.USER_WORKING_DIR}}"
70+
silent: true
71+
vars: *vars
72+
preconditions:
73+
- sh: test -f {{.TFVARS_FILE}}
74+
msg: "Variables file does not exist: {{.TFVARS_FILE}}"
75+
cmds:
76+
- terraform apply -var-file {{.TFVARS_FILE}} {{.TF_ARGS}}

0 commit comments

Comments
 (0)