-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add README and action.yml for Terraform Validate composite action
- Loading branch information
Showing
2 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,80 @@ | ||
| # terraform-validate | ||
| Composite Action for Terraform-Validate | ||
|
|
||
| A GitHub composite action that runs `terraform validate` on a workspace after initializing it. | ||
|
|
||
| ## Description | ||
|
|
||
| The `terraform-validate` action automates the validation of Terraform configurations by running `terraform init` followed by `terraform validate`. It handles backend configuration and variable files, making it easy to validate Terraform code in your CI/CD pipelines. | ||
|
|
||
| ## Features | ||
|
|
||
| - Automatically runs `terraform init` before validation | ||
| - Supports custom working directories | ||
| - Handles backend configuration variables | ||
| - Supports tfvars files | ||
| - Provides detailed validation output | ||
| - Returns validation status as an output | ||
|
|
||
| ## Inputs | ||
|
|
||
| | Name | Description | Required | Default | | ||
| |------|-------------|----------|---------| | ||
| | `working-directory` | Directory containing Terraform configuration files | false | `.` | | ||
| | `backend-config` | Backend configuration variables, one per line | false | N/A | | ||
| | `var-file` | Path to a Terraform tfvars file | false | N/A | | ||
|
|
||
| ## Outputs | ||
|
|
||
| | Name | Description | | ||
| |------|-------------| | ||
| | `is_valid` | Whether the Terraform configuration is valid (true/false) | | ||
| | `stdout` | Output from terraform validate command | | ||
| | `stderr` | Any error output from terraform validate command | | ||
|
|
||
| ## Usage Examples | ||
|
|
||
| ### Basic Validation | ||
|
|
||
| ```yaml | ||
| jobs: | ||
| validate: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Terraform | ||
| uses: hashicorp/setup-terraform@v3 | ||
|
|
||
| - name: Validate Terraform Configuration | ||
| uses: CSVD/terraform-validate@v1 | ||
| with: | ||
| working-directory: './terraform' | ||
| ``` | ||
| ### With Backend Configuration | ||
| ```yaml | ||
| jobs: | ||
| validate: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Setup Terraform | ||
| uses: hashicorp/setup-terraform@v3 | ||
|
|
||
| - name: Validate Terraform Configuration | ||
| uses: CSVD/terraform-validate@v1 | ||
| with: | ||
| working-directory: './terraform' | ||
| backend-config: | | ||
| bucket=my-terraform-state | ||
| key=prod/terraform.tfstate | ||
| region=us-east-1 | ||
| ``` | ||
| ## Requirements | ||
| - Terraform must be installed in the runner environment (typically using `hashicorp/setup-terraform` action) | ||
| - Repository must be checked out before running this action | ||
| - Appropriate AWS credentials if using remote state in S3 (or similar backend configuration) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| name: 'Terraform Validate' | ||
| description: 'Runs terraform validate on a workspace' | ||
|
|
||
| inputs: | ||
| working-directory: | ||
| description: 'Directory containing Terraform configuration files' | ||
| required: false | ||
| default: '.' | ||
| backend-config: | ||
| description: 'Backend configuration variables, one per line' | ||
| required: false | ||
| var-file: | ||
| description: 'Path to a Terraform tfvars file' | ||
| required: false | ||
|
|
||
| outputs: | ||
| is_valid: | ||
| description: 'Whether the Terraform configuration is valid (true/false)' | ||
| stdout: | ||
| description: 'Output from terraform validate command' | ||
| stderr: | ||
| description: 'Any error output from terraform validate command' | ||
|
|
||
| runs: | ||
| using: "composite" | ||
| steps: | ||
| - name: Terraform Init | ||
| shell: bash | ||
| run: | | ||
| cd ${{ inputs.working-directory }} | ||
| INIT_ARGS="" | ||
| if [[ -n "${{ inputs.backend-config }}" ]]; then | ||
| while IFS= read -r config; do | ||
| INIT_ARGS="$INIT_ARGS -backend-config='$config'" | ||
| done <<< "${{ inputs.backend-config }}" | ||
| fi | ||
| terraform init -input=false $INIT_ARGS | ||
| - name: Terraform Validate | ||
| id: validate | ||
| shell: bash | ||
| run: | | ||
| cd ${{ inputs.working-directory }} | ||
| VALIDATE_ARGS="" | ||
| if [[ -n "${{ inputs.var-file }}" ]]; then | ||
| VALIDATE_ARGS="$VALIDATE_ARGS -var-file=${{ inputs.var-file }}" | ||
| fi | ||
| # Capture both stdout and stderr | ||
| OUTPUT=$(terraform validate -no-color $VALIDATE_ARGS 2>&1) | ||
| EXIT_CODE=$? | ||
| # Set outputs | ||
| echo "stdout<<EOF" >> $GITHUB_OUTPUT | ||
| echo "$OUTPUT" >> $GITHUB_OUTPUT | ||
| echo "EOF" >> $GITHUB_OUTPUT | ||
| echo "stderr<<EOF" >> $GITHUB_OUTPUT | ||
| if [ $EXIT_CODE -ne 0 ]; then | ||
| echo "$OUTPUT" >> $GITHUB_OUTPUT | ||
| fi | ||
| echo "EOF" >> $GITHUB_OUTPUT | ||
| echo "is_valid=$([ $EXIT_CODE -eq 0 ] && echo 'true' || echo 'false')" >> $GITHUB_OUTPUT | ||
| # Exit with the original exit code | ||
| exit $EXIT_CODE |