tf-validate: fix cached state config workaround when TF_PROJECT_DIR/project-dir is overridden

When fixing #80 (closed) (see !113 (merged)) the workaround has not been tested with non-default project directory.

The tf-validate tests the relative location of the file in $TF_PROJECT_DIR/.terraform to prevent side effect with external backend configuration.

However we changed the working directory in .tf-base job through the cd $TF_PROJECT_DIR command.

This does not work as intended for $TF_PROJECT_DIR values other than '.' as we are actually testing for the presence of the file at $TF_PROJECT_DIR/$TF_PROJECT_DIR/.terraform/terraform.tfstate

Expected behavior

tf-validate detects the condition, remove the cached file and succeed when TF_PROJECT_DIR/project-dir is overridden

Actual behavior

tf-validate fails when cached terraform.tfsate is configured to uses credentials for external backend (eg. GCS bucket) and overriding the root module directory path with TF_PROJECT_DIR/project-dir set

Logs and/or screenshots

N/A

Context & Configuration

N/A

The issue was reproduced using:

  • Version of the template: 5.5.0 Configured GitLab CI project or group variables:

  • TF_PROJECT_DIR=./terraform/