Skip to content

Commit

Permalink
feat: Pass custom arguments to terraform init in terraform_validate
Browse files Browse the repository at this point in the history
… hook (antonbabenko#293)
  • Loading branch information
maxbrunet authored Dec 11, 2021
1 parent 7c6ad7c commit 45575c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
10 changes: 8 additions & 2 deletions terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +40,11 @@ parse_cmdline_() {
ARGS+=("$1")
shift
;;
-i | --init-args)
shift
INIT_ARGS+=("$1")
shift
;;
-e | --envs)
shift
ENVS+=("$1")
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -123,6 +128,7 @@ terraform_validate_() {

# global arrays
declare -a ARGS
declare -a INIT_ARGS
declare -a ENVS
declare -a FILES

Expand Down

0 comments on commit 45575c3

Please sign in to comment.