-
-
Notifications
You must be signed in to change notification settings - Fork 542
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: Add terraform_validate support to init modules and providers if needed #301
Conversation
Looks pretty good. Let's see what @yermulnik or @MaxymVlasov have to say after they test it locally. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch on using terraform providers
to validate modules installation.
Code looks good to me.
Co-authored-by: George L. Yermulnik <[email protected]>
@@ -90,19 +90,20 @@ terraform_validate_() { | |||
|
|||
pushd "$(realpath "$path_uniq")" > /dev/null | |||
|
|||
if [[ ! -d .terraform ]]; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should return that check and add check that checks that any --init-args
provided.
Because, time execution increased up to 80-90% for:
repos:
- repo: [email protected]:jgrumboe/pre-commit-terraform.git
rev: '4086982'
hooks:
- id: terraform_validate
compare with:
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.62.0
hooks:
- id: terraform_validate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to run terraform init
each time if it is not specified by arguments.
Or need to explicitly provide force-init: true
etc. for that.
For now, it just looks like no needed speed violation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your input.
I will try to come up with a better solution, which is fast as now when no modules/providers version changes were made in the code and still eliminates the need for manual deletion of the .terraform
folder in case modules/providers version changes are made.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jgrumboe
Any updates? Do you need any help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @MaxymVlasov
Sorry, this one slipped out of my mind. I will have a look next week.
Thanks for the reminder.
Close as too outdated. Simpler to create a new branch with specified changes than try to resolve conflicts |
Put an
x
into the box if that apply:Description of your changes
This PR adds logic to
terraform_validate
to detect if module initialization is needed and run it. It's also possible now to force an initialization and/or provider upgrade by just adding the documentedinit-args
to the hook call.The new logic works as follows:
terraform providers
is called on every run and used to detect if all needed modules are initialized correctly. If it succeeds it will pass-get=false
to theterraform init
call, which prevents modules to be re-downloaded. On the other hand, ifterraform providers
fails, this means modules or providers are not initialized and-get=true
will be passed toterraform init
- a full initialization will happen.Since
terraform init
will be called every time, the already existing--init-args
feature will give us to additional benefits:--init-args=-get=true
will result in a forced initialization on every run--init-args=-upgrade
will allow to upgrade providers in addition to the modules if neededFixes #224
How has this code been tested
.terraform
folder if presentpre-commit run terraform_validate
and you see that it will initialize the.terraform
folder newly and validate your code.pre-commit run terraform_validate
will be faster, as long as no module version changes happenpre-commit run terraform_validate
and you see that it will reinitialize for the new moduleRepeat the above steps with changing provider version and setting the
--init-args=-upgrade
argument. You will notice that only providers or both are initialized if needed.