diff --git a/README.md b/README.md index c13b81e..63064fb 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..4cbecdb --- /dev/null +++ b/action.yml @@ -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<> $GITHUB_OUTPUT + echo "$OUTPUT" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + + echo "stderr<> $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 \ No newline at end of file