Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pass custom arguments to terraform init in terraform_validate hook #293

Merged
merged 1 commit into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI The plan was and still is to externalize parse_cmdline as well as other common functions outside and include them in all hooks. Can this function be reused without side effects in all hooks in the future? I think so.

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