Commit 57a2919d authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

refactor: use $environment_type & $environment_name

Deprecated $appname and $env dynamic variables with $environment_name & $environment_type
With backward compatibility (legacy vars still usable)
parent e9fead76
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -162,7 +162,7 @@ terraform init \
If you disabled the GitLab-managed Terraform state (by setting `$TF_GITLAB_BACKEND_DISABLED` to `true`),
the template supports an implicit [backend configuration](https://www.terraform.io/language/settings/backends/configuration#file) mechanism:

1. Looks for a `$env.tfbackend` file (ex: `staging.tfbackend` for staging environment),
1. Looks for a `$environment_type.tfbackend` file (ex: `staging.tfbackend` for staging environment),
2. Fallbacks to `default.tfbackend` file.

If one of those files are found, it is automatically used by the template in the `terraform init` command (using the `-backend-config` CLI option).
@@ -295,8 +295,8 @@ In order to be able to implement some **genericity** in your code, you should us

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"`)
    * the template also auto-detects any file named `$environment_type.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_variables.html) 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)
4. **dynamic variables** provided by the template:
+7 −3
Original line number Diff line number Diff line
@@ -306,11 +306,11 @@ stages:
    log_info "--- \\e[32minit\\e[0m"
    log_info "--- Positioned environment (can also be used as TF vars):"
    # shellcheck disable=SC2154
    log_info "--- environment_type: \\e[33;1m${environment_type}\\e[0m"
    log_info "--- \$environment_type: \\e[33;1m${environment_type}\\e[0m"
    # shellcheck disable=SC2154
    log_info "--- environment_name: \\e[33;1m${environment_name}\\e[0m"
    log_info "--- \$environment_name: \\e[33;1m${environment_name}\\e[0m"
    # shellcheck disable=SC2154
    log_info "--- environment_slug: \\e[33;1m${environment_slug}\\e[0m"
    log_info "--- \$environment_slug: \\e[33;1m${environment_slug}\\e[0m"

    # maybe enable debug log
    if [[ "$TRACE" ]]; then
@@ -437,6 +437,9 @@ stages:
    # shellcheck disable=SC2154
    log_info "--- \\e[32mapply\\e[0m"

    # unset any upstream deployment env & artifacts
    rm -f terraform.env

    # maybe execute pre apply script
    prescript="$TF_SCRIPTS_DIR/tf-pre-apply.sh"
    if [[ -f "$prescript" ]]; then
@@ -475,6 +478,7 @@ stages:
    fi

    # finally propagate environment info
    # /!\ append is important here as it may have been created by a hook script
    echo -e "environment_type=$environment_type\\nenvironment_name=$CI_ENVIRONMENT_NAME\\nenvironment_slug=$CI_ENVIRONMENT_SLUG" >> terraform.env
  }