Skip to content

Commit

Permalink
feat: enhance Terraform Validate action to include testing results
Browse files Browse the repository at this point in the history
  • Loading branch information
arnol377 committed Mar 27, 2025
1 parent e686fba commit e973f29
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Terraform Validate'
description: 'Runs terraform validate on a workspace'
description: 'Runs terraform validate and test on a workspace'

inputs:
working-directory:
Expand All @@ -16,10 +16,12 @@ inputs:
outputs:
is_valid:
description: 'Whether the Terraform configuration is valid (true/false)'
tests_passed:
description: 'Whether the Terraform tests passed (true/false)'
stdout:
description: 'Output from terraform validate command'
description: 'Output from terraform validate and test commands'
stderr:
description: 'Any error output from terraform validate command'
description: 'Any error output from terraform validate or test commands'

runs:
using: "composite"
Expand Down Expand Up @@ -48,20 +50,45 @@ runs:
# Capture both stdout and stderr
OUTPUT=$(terraform validate -no-color $VALIDATE_ARGS 2>&1)
EXIT_CODE=$?
VALIDATE_EXIT_CODE=$?
# Store validation output
VALIDATE_OUTPUT="$OUTPUT"
# Run terraform test if validation passed
TEST_OUTPUT=""
TEST_EXIT_CODE=0
if [ $VALIDATE_EXIT_CODE -eq 0 ]; then
TEST_OUTPUT=$(terraform test 2>&1)
TEST_EXIT_CODE=$?
fi
# Combine outputs
COMBINED_OUTPUT="${VALIDATE_OUTPUT}"
if [ -n "$TEST_OUTPUT" ]; then
COMBINED_OUTPUT="${COMBINED_OUTPUT}
=== Terraform Test Results ===
${TEST_OUTPUT}"
fi

# Set outputs
echo "stdout<<EOF" >> $GITHUB_OUTPUT
echo "$OUTPUT" >> $GITHUB_OUTPUT
echo "$COMBINED_OUTPUT" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

echo "stderr<<EOF" >> $GITHUB_OUTPUT
if [ $EXIT_CODE -ne 0 ]; then
echo "$OUTPUT" >> $GITHUB_OUTPUT
if [ $VALIDATE_EXIT_CODE -ne 0 ]; then
echo "$VALIDATE_OUTPUT" >> $GITHUB_OUTPUT
elif [ $TEST_EXIT_CODE -ne 0 ]; then
echo "$TEST_OUTPUT" >> $GITHUB_OUTPUT
fi
echo "EOF" >> $GITHUB_OUTPUT

echo "is_valid=$([ $EXIT_CODE -eq 0 ] && echo 'true' || echo 'false')" >> $GITHUB_OUTPUT
echo "is_valid=$([ $VALIDATE_EXIT_CODE -eq 0 ] && echo 'true' || echo 'false')" >> $GITHUB_OUTPUT
echo "tests_passed=$([ $TEST_EXIT_CODE -eq 0 ] && echo 'true' || echo 'false')" >> $GITHUB_OUTPUT

# Exit with the original exit code
exit $EXIT_CODE
# Exit with failure if either validate or test failed
if [ $VALIDATE_EXIT_CODE -ne 0 ] || [ $TEST_EXIT_CODE -ne 0 ]; then
exit 1
fi

0 comments on commit e973f29

Please sign in to comment.