From ae68ce080bca2300980d05c01297b35e63dc5bb9 Mon Sep 17 00:00:00 2001 From: Maxime Brunet Date: Fri, 10 Dec 2021 17:39:12 -0800 Subject: [PATCH] feat(terraform_validate): Pass custom arguments to terraform init --- README.md | 10 +++++++++- terraform_validate.sh | 10 ++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2bc67f054..45f8b3049 100644 --- a/README.md +++ b/README.md @@ -526,7 +526,15 @@ Example: - --envs=AWS_SECRET_ACCESS_KEY="asecretkey" ``` -3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository: +3. `terraform_validate` also supports passing custom arguments to its `terraform init`: + + ```yaml + - id: terraform_validate + args: + - --init-args=-lockfile=readonly + ``` + +4. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository: ```bash echo " diff --git a/terraform_validate.sh b/terraform_validate.sh index 236b35138..ea23dac7f 100755 --- a/terraform_validate.sh +++ b/terraform_validate.sh @@ -30,7 +30,7 @@ initialize_() { parse_cmdline_() { declare argv - argv=$(getopt -o e:a: --long envs:,args: -- "$@") || return + argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return eval "set -- $argv" for argv; do @@ -40,6 +40,11 @@ parse_cmdline_() { ARGS+=("$1") shift ;; + -i | --init-args) + shift + INIT_ARGS+=("$1") + shift + ;; -e | --envs) shift ENVS+=("$1") @@ -87,7 +92,7 @@ terraform_validate_() { if [[ ! -d .terraform ]]; then set +e - init_output=$(terraform init -backend=false 2>&1) + init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1) init_code=$? set -e @@ -123,6 +128,7 @@ terraform_validate_() { # global arrays declare -a ARGS +declare -a INIT_ARGS declare -a ENVS declare -a FILES