Skip to content

Conversation

@actuarysailor
Copy link
Owner

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

This PR fixes the terraform_validate_docker hook to properly handle missing providers and modules by implementing automatic retry logic with terraform init.

Problem: The terraform_validate_docker hook was previously configured to run terraform validate directly, without the retry logic present in the script-based terraform_validate hook. As a result, if validation failed due to missing providers or uninitialized modules, the Docker hook would fail immediately instead of attempting to run terraform init and retry.

Solution: The hook is now configured to:

  • Use the /usr/bin/hooks/terraform_validate.sh script (already present in the Docker image) instead of calling terraform validate directly.
  • Enable --retry-once-with-cleanup=true by default to automatically handle missing provider/module scenarios.
  • Provide the same retry logic as the script-based hook:
    • First attempt terraform validate
    • If it fails, run terraform init and retry
    • If specific error patterns are detected, clean up .terraform/modules and .terraform/providers directories and retry once more

This ensures feature parity between the script-based and Docker-based validation hooks, providing a consistent user experience regardless of which version is used.

How can we test changes

  1. Test with missing providers:

    • Create a Terraform configuration that references providers not yet downloaded.
    • Remove any existing .terraform directory.
    • Run the terraform_validate_docker hook and verify it automatically runs terraform init and succeeds.
  2. Test retry logic with corrupted state:

    • Create a scenario with partially corrupted .terraform/providers or .terraform/modules.
    • Verify the hook detects the issue, cleans up, re-initializes, and retries validation.
  3. Compare with script-based hook:

    • Run the same test scenarios with both terraform_validate and terraform_validate_docker hooks.
    • Verify both hooks handle missing providers/modules scenarios identically.
  4. Test in CI/CD environments:

    • Verify the Docker hook works correctly in GitHub Actions or other CI environments where Docker is available.
    • Confirm the hook can be properly skipped in environments without Docker (like pre-commit.ci) using the `ci:

Copilot AI review requested due to automatic review settings August 16, 2025 02:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the terraform_validate_docker hook to provide feature parity with the script-based terraform_validate hook by implementing automatic retry logic for handling missing providers and modules.

  • Replaces direct terraform validate call with the existing validation script that includes retry logic
  • Enables automatic terraform init and retry behavior when validation fails due to missing dependencies
  • Updates hook description to clarify the new automatic initialization behavior

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@actuarysailor actuarysailor merged commit 4050d10 into main Aug 16, 2025
59 checks passed
@actuarysailor actuarysailor deleted the fix/terraform-validate branch August 16, 2025 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants