Skip to content

Commit

Permalink
fix: add workspace parameter to validate (#78)
Browse files Browse the repository at this point in the history
* fix: add workspace parameter to validate
  • Loading branch information
KyleTryon authored May 19, 2022
1 parent 58a05dc commit 81c0aad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/commands/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ parameters:
description: Configure a custom timeout limit
type: string
default: 10m

workspace:
type: "string"
description: "Name of the terraform workspace"
default: ""

steps:
- run:
Expand All @@ -23,4 +26,5 @@ steps:
environment:
TF_PARAM_PATH: << parameters.path >>
TF_PARAM_CLI_CONFIG_FILE: <<parameters.cli_config_file>>
TF_PARAM_WORKSPACE: <<parameters.workspace>>
command: <<include(scripts/validate.sh)>>
5 changes: 5 additions & 0 deletions src/jobs/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ parameters:
default: "."
description: "Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory)"
type: string
workspace:
type: "string"
description: "Name of the terraform workspace"
default: ""
path:
type: "string"
description: "Path to the terraform module"
Expand Down Expand Up @@ -74,3 +78,4 @@ steps:
path: << parameters.path >>
cli_config_file: << parameters.cli_config_file >>
timeout: <<parameters.timeout>>
workspace: <<parameters.workspace>>
14 changes: 14 additions & 0 deletions src/scripts/validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@ if [[ ! -d "$TF_PARAM_PATH" ]]; then
echo "Path does not exist: $TF_PARAM_PATH"
exit 1
fi

workspace_parameter="$(eval echo "${TF_PARAM_WORKSPACE}")"
readonly workspace="${TF_WORKSPACE:-$workspace_parameter}"
export workspace
unset TF_WORKSPACE

terraform -chdir="$TF_PARAM_PATH" init -input=false -backend=false

if [[ -n "$workspace_parameter" ]]; then
echo "[INFO] Provisioning local workspace: $workspace"
terraform -chdir="${TF_PARAM_PATH}" workspace select "$workspace" || terraform -chdir="${TF_PARAM_PATH}" workspace new "$workspace"
else
echo "[INFO] Remote State Backend Enabled"
fi

terraform -chdir="$TF_PARAM_PATH" validate

0 comments on commit 81c0aad

Please sign in to comment.