-
-
Notifications
You must be signed in to change notification settings - Fork 583
feat: Add support for set env vars inside hook runtime #408
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ If you are using `pre-commit-terraform` already or want to support its developme | |
| * [Available Hooks](#available-hooks) | ||
| * [Hooks usage notes and examples](#hooks-usage-notes-and-examples) | ||
| * [All hooks: Usage of environment variables in `--args`](#all-hooks-usage-of-environment-variables-in---args) | ||
| * [All hooks: Set env vars inside hook runtime](#all-hooks-set-env-vars-inside-hook-runtime) | ||
| * [checkov (deprecated) and terraform_checkov](#checkov-deprecated-and-terraform_checkov) | ||
| * [infracost_breakdown](#infracost_breakdown) | ||
| * [terraform_docs](#terraform_docs) | ||
|
|
@@ -283,6 +284,22 @@ Config example: | |
|
|
||
| If for config above set up `export CONFIG_NAME=.tflint; export CONFIG_EXT=hcl` before `pre-commit run`, args will be expanded to `--config=.tflint.hcl --module`. | ||
|
|
||
| ### All hooks: Set env vars inside hook runtime | ||
|
MaxymVlasov marked this conversation as resolved.
Outdated
|
||
|
|
||
| > All, except deprecated hooks: `checkov`, `terraform_docs_replace` | ||
|
|
||
| You can specify environment variables that will be passed to the hook runtime. | ||
|
MaxymVlasov marked this conversation as resolved.
Outdated
|
||
|
|
||
| Config example: | ||
|
|
||
| ```yaml | ||
| - id: terraform_validate | ||
| args: | ||
| - --envs=AWS_DEFAULT_REGION="us-west-2" | ||
| - --envs=AWS_ACCESS_KEY_ID="anaccesskey" | ||
| - --envs=AWS_SECRET_ACCESS_KEY="asecretkey" | ||
|
Comment on lines
+298
to
+300
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed privately we'll get back to reconsidering this option's name later (e.g. to |
||
| ``` | ||
|
|
||
| ### checkov (deprecated) and terraform_checkov | ||
|
|
||
| > `checkov` hook is deprecated, please use `terraform_checkov`. | ||
|
|
@@ -614,25 +631,15 @@ Example: | |
| - --args=-no-color | ||
| ``` | ||
|
|
||
| 2. `terraform_validate` also supports custom environment variables passed to the pre-commit runtime: | ||
|
|
||
| ```yaml | ||
| - id: terraform_validate | ||
| args: | ||
| - --envs=AWS_DEFAULT_REGION="us-west-2" | ||
| - --envs=AWS_ACCESS_KEY_ID="anaccesskey" | ||
| - --envs=AWS_SECRET_ACCESS_KEY="asecretkey" | ||
| ``` | ||
|
|
||
| 3. `terraform_validate` also supports passing custom arguments to its `terraform init`: | ||
| 2. `terraform_validate` also supports passing custom arguments to its `terraform init`: | ||
|
|
||
| ```yaml | ||
| - id: terraform_validate | ||
| args: | ||
| - --tf-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: | ||
| 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: | ||
|
|
||
| ```bash | ||
| echo " | ||
|
|
@@ -648,7 +655,7 @@ Example: | |
|
|
||
| **Warning:** If you use Terraform workspaces, DO NOT use this workaround ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation. | ||
|
|
||
| 5. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out. | ||
| 4. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out. | ||
|
|
||
| When running the hook against Terraform code where you have provider `configuration_aliases` defined in a `required_providers` configuration block, terraform will throw an error like: | ||
| > | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ function common::initialize { | |
| # ARGS (array) arguments that configure wrapped tool behavior | ||
| # HOOK_CONFIG (array) arguments that configure hook behavior | ||
| # TF_INIT_ARGS (array) arguments for `terraform init` command | ||
| # ENVS (array) environment variables that will be with all | ||
| # 3rd-party tools that will read them. | ||
|
MaxymVlasov marked this conversation as resolved.
Outdated
|
||
| # FILES (array) filenames to check | ||
| # Arguments: | ||
| # $@ (array) all specified in `hooks.[].args` in | ||
|
|
@@ -37,9 +39,11 @@ function common::parse_cmdline { | |
| ARGS=() HOOK_CONFIG=() FILES=() | ||
| # Used inside `common::terraform_init` function | ||
| TF_INIT_ARGS=() | ||
| # Used inside `common::export_provided_env_vars` function | ||
| ENVS=() | ||
|
|
||
| local argv | ||
| argv=$(getopt -o a:,h:,i: --long args:,hook-config:,init-args:,tf-init-args: -- "$@") || return | ||
| argv=$(getopt -o a:,h:,i:,e: --long args:,hook-config:,init-args:,tf-init-args:,envs: -- "$@") || return | ||
| eval "set -- $argv" | ||
|
|
||
| for argv; do | ||
|
|
@@ -60,6 +64,11 @@ function common::parse_cmdline { | |
| TF_INIT_ARGS+=("$1") | ||
| shift | ||
| ;; | ||
| -e | --envs) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @antonbabenko Any ideas on how it can be better named? Main idea:
Config example for now - id: terraform_validate
args:
- --envs=AWS_DEFAULT_REGION="us-west-2"
- --envs=AWS_ACCESS_KEY_ID="anaccesskey"
- --envs=AWS_SECRET_ACCESS_KEY="asecretkey"
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yermulnik proposed
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. env-vars sounds perfect to me |
||
| shift | ||
| ENVS+=("$1") | ||
| shift | ||
| ;; | ||
| --) | ||
| shift | ||
| # shellcheck disable=SC2034 # Variable is used | ||
|
|
@@ -270,3 +279,25 @@ function common::terraform_init { | |
|
|
||
| return $exit_code | ||
| } | ||
|
|
||
| ####################################################################### | ||
| # Export provided K/V as environment variables. | ||
| # Arguments: | ||
| # env_vars (array) environment variables that will be with all | ||
| # 3rd-party tools that will read them. | ||
|
MaxymVlasov marked this conversation as resolved.
Outdated
|
||
| ####################################################################### | ||
| function common::export_provided_env_vars { | ||
| local -a -r env_vars=("$@") | ||
|
|
||
| local var | ||
| local var_name | ||
| local var_value | ||
|
|
||
| for var in "${env_vars[@]}"; do | ||
| var_name="${var%%=*}" | ||
| var_value="${var#*=}" | ||
| # shellcheck disable=SC2086 | ||
| export $var_name="$var_value" | ||
| done | ||
| exit 1 | ||
|
MaxymVlasov marked this conversation as resolved.
Outdated
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.