Skip to content

Commit 45575c3

Browse files
authored
feat: Pass custom arguments to terraform init in terraform_validate hook (antonbabenko#293)
1 parent 7c6ad7c commit 45575c3

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,15 @@ Example:
526526
- --envs=AWS_SECRET_ACCESS_KEY="asecretkey"
527527
```
528528

529-
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:
529+
3. `terraform_validate` also supports passing custom arguments to its `terraform init`:
530+
531+
```yaml
532+
- id: terraform_validate
533+
args:
534+
- --init-args=-lockfile=readonly
535+
```
536+
537+
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:
530538

531539
```bash
532540
echo "

terraform_validate.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ initialize_() {
3030

3131
parse_cmdline_() {
3232
declare argv
33-
argv=$(getopt -o e:a: --long envs:,args: -- "$@") || return
33+
argv=$(getopt -o e:i:a: --long envs:,init-args:,args: -- "$@") || return
3434
eval "set -- $argv"
3535

3636
for argv; do
@@ -40,6 +40,11 @@ parse_cmdline_() {
4040
ARGS+=("$1")
4141
shift
4242
;;
43+
-i | --init-args)
44+
shift
45+
INIT_ARGS+=("$1")
46+
shift
47+
;;
4348
-e | --envs)
4449
shift
4550
ENVS+=("$1")
@@ -87,7 +92,7 @@ terraform_validate_() {
8792

8893
if [[ ! -d .terraform ]]; then
8994
set +e
90-
init_output=$(terraform init -backend=false 2>&1)
95+
init_output=$(terraform init -backend=false "${INIT_ARGS[@]}" 2>&1)
9196
init_code=$?
9297
set -e
9398

@@ -123,6 +128,7 @@ terraform_validate_() {
123128

124129
# global arrays
125130
declare -a ARGS
131+
declare -a INIT_ARGS
126132
declare -a ENVS
127133
declare -a FILES
128134

0 commit comments

Comments
 (0)