|
| 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