Skip to content

Commit

Permalink
[autoSync] CI: add fmt, validate, tflint check
Browse files Browse the repository at this point in the history
  • Loading branch information
shanye997 committed Sep 12, 2024
1 parent f9ed4e6 commit cb93b1e
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,107 @@ on:
- '**/*.tf'

jobs:
terraform-fmt:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: fmt-check
run: |
if [ ! -f /usr/local/bin/terraform ]; then
wget -q https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip
unzip terraform_1.6.0_linux_amd64.zip -d /usr/local/bin/
fi
error=false
echo "===> Terraform fmt -diff checking"
terraform fmt -diff -recursive -check
if [[ $? -ne 0 ]]; then
echo -e "\033[31m[ERROR]\033[0m: Some codes has not been formatted, and please running terraform fmt --recursive command before pushing."
exit 1
fi
terraform-validate:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: validate-check
run: |
if [ ! -f /usr/local/bin/terraform ]; then
wget -q https://releases.hashicorp.com/terraform/1.6.0/terraform_1.6.0_linux_amd64.zip
unzip terraform_1.6.0_linux_amd64.zip -d /usr/local/bin/
fi
exp="examples"
submod="modules"
output_file="combined_output.txt"
echo "./" > "$output_file"
if [[ -d "$exp" ]]; then
find $exp -type d -print -mindepth 1 -maxdepth 1 >> $output_file
fi
if [[ -d "$submod" ]]; then
find $submod -type d -print -mindepth 1 -maxdepth 1 >> $output_file
fi
exitCode=0
while IFS= read -r line
do
echo "===> Terraform validate checking in $line"
terraform -chdir=$line init -upgrade
terraform -chdir=$line validate
if [[ $? -ne 0 ]]; then
echo -e "\033[31m[ERROR]\033[0m: Some codes contain errors, and please running terraform validate command before pushing."
exitCode=1
fi
done < $output_file
rm $output_file
exit $exitCode
tflint:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3

- uses: actions/checkout@v4
name: Checkout source code

- uses: actions/cache@v4
name: Cache plugin dir
with:
path: ~/.tflint.d/plugins
key: ${{ matrix.os }}-tflint-${{ hashFiles('.tflint.hcl') }}

- uses: terraform-linters/setup-tflint@v4
name: Setup TFLint
with:
tflint_version: v0.52.0

- name: Init TFLint
run: tflint --init
env:
GITHUB_TOKEN: ${{ github.token }}

- name: tflint
run: |
tflint --recursive \
--enable-rule=terraform_comment_syntax \
--enable-rule=terraform_deprecated_index \
--enable-rule=terraform_deprecated_interpolation \
--enable-rule=terraform_deprecated_lookup \
--enable-rule=terraform_documented_outputs \
--enable-rule=terraform_documented_variables \
--enable-rule=terraform_typed_variables \
--enable-rule=terraform_unused_declarations \
--enable-rule=terraform_required_version \
--enable-rule=terraform_standard_module_structure \
--disable-rule=terraform_required_providers
if [[ $? -ne 0 ]]; then
exit_code=1
fi
e2e-check:
# if: github.event.review.state == 'approved' || github.event.review.body == 'approved'
needs: [terraform-fmt, terraform-validate, tflint]
runs-on: ubuntu-latest
name: 'e2e check'
steps:
Expand Down

0 comments on commit cb93b1e

Please sign in to comment.