Commit eada2c7b authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

feat: add implicit tfvars support

parent 66cf5557
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -293,10 +293,13 @@ You have to be aware that your Terraform code has to be able to cope with variou

In order to be able to implement some **genericity** in your code, you should use [Terraform variables](https://www.terraform.io/docs/language/values/variables.html) (in your Terraform files), and environment variables (in your hook scripts):

1. any [predefined GitLab CI variable](https://docs.gitlab.com/ee/ci/variables/#predefined-environment-variables) may be freedly used in your hook scripts or extra options variables (ex: `TF_EXTRA_OPTS: "-var project_name=$CI_PROJECT_NAME"`)
2. you may also use [custom GitLab variables](https://docs.gitlab.com/ee/ci/variables/#custom-cicd-variables) to pass values to your hook script or even directly as Terraform variables [using the right syntax](https://www.terraform.io/docs/cli/config/environment-variables.html#tf_var_name)
1. Use [`tfvars` files](https://www.terraform.io/language/values/variables#variable-definitions-tfvars-files) for non-secret configuration:
    * default `terraform.tfvars[.json]` and `*.auto.tfvars[.json]` files are obviously supported by Terraform,
    * the template also auto-detects any file named `$env.env.tfvars[.json]` (ex: `staging.env.tfvars` for staging environment) and uses it with all related `terraform` commands.
2. any [predefined GitLab CI variable](https://docs.gitlab.com/ee/ci/variables/#predefined-environment-variables) may be freedly used in your hook scripts or extra options variables (ex: `TF_EXTRA_OPTS: "-var project_name=$CI_PROJECT_NAME"`)
3. you may also use [custom GitLab variables](https://docs.gitlab.com/ee/ci/variables/#custom-cicd-variables) to pass values to your hook script or even directly as Terraform variables [using the right syntax](https://www.terraform.io/docs/cli/config/environment-variables.html#tf_var_name)
    (ex: env variable `$TF_VAR_ssh_private_key_file` will be visible as `ssh_private_key_file` Terraform variable in your code)
3. **dynamic variables** provided by the template:
4. **dynamic variables** provided by the template:
    * `environment_type`: the environment type (`review`, `integration`, `staging` or `production`)
    * `environment_name` (set as `$CI_ENVIRONMENT_NAME`): the full environment name (ex: `review/fix-prometheus-configuration`, `integration`, `staging` or `production`)
    * `environment_slug` (set as `$CI_ENVIRONMENT_SLUG`): the _slugified_ environment name (ex: `review-fix-promet-r13zmu`, `integration`, `staging` or `production`)
+38 −10
Original line number Diff line number Diff line
@@ -383,8 +383,18 @@ stages:

    # shellcheck disable=SC2154
    log_info "--- \\e[32mplan\\e[0m"

    # implicit tfvars
    env_vars=$(ls -1 "${environment_type}.env.tfvars" 2>/dev/null || ls -1 "${environment_type}.env.tfvars.json" 2>/dev/null || echo "")
    if [[ -f "$env_vars" ]]
    then
      log_info "--- environment-specific tfvars file (\\e[33;1m${env_vars}\\e[0m) found: use"
    else
      log_info "--- no environment-specific tfvars file found: ignore"
    fi

    # shellcheck disable=SC2154,SC2086,SC2046
    terraform plan -out "$tf_plan" $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)
    terraform plan ${env_vars:+-var-file=${env_vars}} -out "$tf_plan" $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)

    # then generate GitLab TF report
    if ! command -v jq > /dev/null
@@ -428,13 +438,22 @@ stages:
      log_info "--- \\e[32mpre-apply\\e[0m hook (\\e[33;1m${prescript}\\e[0m) not found: skip"
    fi

    # implicit tfvars
    env_vars=$(ls -1 "${environment_type}.env.tfvars" 2>/dev/null || ls -1 "${environment_type}.env.tfvars.json" 2>/dev/null || echo "")
    if [[ -f "$env_vars" ]]
    then
      log_info "--- environment-specific tfvars file (\\e[33;1m${env_vars}\\e[0m) found: use"
    else
      log_info "--- no environment-specific tfvars file found: ignore"
    fi

    if [[ "$tf_plan" ]]; then
      log_info "--- applying upstream plan: \\e[33;1m${tf_plan}\\e[0m"
      terraform apply -auto-approve "$tf_plan"
      terraform apply -auto-approve ${env_vars:+-var-file=${env_vars}} "$tf_plan"
    else
      log_info "--- auto plan + apply"
      # shellcheck disable=SC2154,SC2086,SC2046
      terraform apply -auto-approve $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)
      terraform apply -auto-approve ${env_vars:+-var-file=${env_vars}} $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)
    fi

    # maybe execute post apply script
@@ -468,8 +487,17 @@ stages:
      log_info "--- \\e[32mpre-destroy\\e[0m hook (\\e[33;1m${prescript}\\e[0m) not found: skip"
    fi

    # implicit tfvars
    env_vars=$(ls -1 "${environment_type}.env.tfvars" 2>/dev/null || ls -1 "${environment_type}.env.tfvars.json" 2>/dev/null || echo "")
    if [[ -f "$env_vars" ]]
    then
      log_info "--- environment-specific tfvars file (\\e[33;1m${env_vars}\\e[0m) found: use"
    else
      log_info "--- no environment-specific tfvars file found: ignore"
    fi

    # shellcheck disable=SC2154,SC2086,SC2046
    terraform destroy -auto-approve $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)
    terraform destroy -auto-approve ${env_vars:+-var-file=${env_vars}} $(echo "$extra_opts" | envsubst_cli) $(echo "$opts" | envsubst_cli)

    # remove gitlab-managed tf state
    if [[ "$TF_GITLAB_BACKEND_DISABLED" != "true" ]]