1+ name : Pre-commit
2+
3+ on :
4+ pull_request :
5+ branches : [master]
6+ paths :
7+ - ' **.tf'
8+ - ' **.tfvars'
9+ - ' **.md'
10+ - ' .pre-commit-config.yaml'
11+ push :
12+ branches : [master]
13+ paths :
14+ - ' **.tf'
15+ - ' **.tfvars'
16+ - ' **.md'
17+ - ' .pre-commit-config.yaml'
18+
19+ jobs :
20+ pre-commit :
21+ runs-on : ubuntu-latest
22+ timeout-minutes : 15
23+ permissions :
24+ contents : read
25+ pull-requests : read
26+
27+ steps :
28+ - name : Checkout repository
29+ uses : actions/checkout@v4
30+ with :
31+ fetch-depth : 0
32+
33+ - name : Set up Python
34+ uses : actions/setup-python@v4
35+ with :
36+ python-version : ' 3.11'
37+
38+ - name : Set up Terraform
39+ uses : hashicorp/setup-terraform@v3
40+ with :
41+ terraform_version : ' 1.3.0'
42+
43+ - name : Cache terraform tools
44+ uses : actions/cache@v3
45+ with :
46+ path : |
47+ ~/.local/bin/terraform-docs
48+ ~/.local/bin/tflint
49+ key : terraform-tools-${{ runner.os }}-v1
50+ restore-keys : |
51+ terraform-tools-${{ runner.os }}-
52+
53+ - name : Install terraform-docs
54+ run : |
55+ if [ ! -f ~/.local/bin/terraform-docs ]; then
56+ echo "Installing terraform-docs..."
57+ mkdir -p ~/.local/bin
58+ curl -sSLo ./terraform-docs.tar.gz https://terraform-docs.io/dl/v0.16.0/terraform-docs-v0.16.0-$(uname)-amd64.tar.gz
59+ tar -xzf terraform-docs.tar.gz
60+ chmod +x terraform-docs
61+ mv terraform-docs ~/.local/bin/
62+ rm terraform-docs.tar.gz
63+ fi
64+ echo "$HOME/.local/bin" >> $GITHUB_PATH
65+
66+ - name : Install tflint
67+ run : |
68+ if ! command -v tflint &> /dev/null; then
69+ echo "Installing tflint..."
70+ curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
71+ fi
72+
73+ - name : Install pre-commit
74+ run : |
75+ python -m pip install --upgrade pip
76+ pip install pre-commit
77+
78+ - name : Cache pre-commit hooks
79+ uses : actions/cache@v3
80+ with :
81+ path : ~/.cache/pre-commit
82+ key : pre-commit-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }}
83+ restore-keys : |
84+ pre-commit-${{ runner.os }}-
85+
86+ - name : Install pre-commit hooks
87+ run : pre-commit install-hooks
88+
89+ - name : Run pre-commit on all files (push to master)
90+ if : github.event_name == 'push' && github.ref == 'refs/heads/master'
91+ run : pre-commit run --all-files
92+
93+ - name : Run pre-commit on changed files (pull request)
94+ if : github.event_name == 'pull_request'
95+ run : |
96+ # Get the list of changed files
97+ git fetch origin ${{ github.base_ref }}
98+ CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- '*.tf' '*.tfvars' '*.md')
99+
100+ if [ -n "$CHANGED_FILES" ]; then
101+ echo "Running pre-commit on changed files:"
102+ echo "$CHANGED_FILES"
103+ pre-commit run --files $CHANGED_FILES
104+ else
105+ echo "No relevant files changed, skipping pre-commit checks"
106+ fi
107+
108+ - name : Pre-commit summary
109+ if : always()
110+ run : |
111+ echo "## 🔍 Pre-commit Results" >> $GITHUB_STEP_SUMMARY
112+ echo "" >> $GITHUB_STEP_SUMMARY
113+
114+ if [ "${{ job.status }}" == "success" ]; then
115+ echo "✅ All pre-commit checks passed!" >> $GITHUB_STEP_SUMMARY
116+ echo "" >> $GITHUB_STEP_SUMMARY
117+ echo "**Tools verified:**" >> $GITHUB_STEP_SUMMARY
118+ echo "- 🔧 Terraform formatting" >> $GITHUB_STEP_SUMMARY
119+ echo "- ✅ Terraform validation" >> $GITHUB_STEP_SUMMARY
120+ echo "- 📚 Documentation generation" >> $GITHUB_STEP_SUMMARY
121+ echo "- 🔍 TFLint analysis" >> $GITHUB_STEP_SUMMARY
122+ echo "- 🧹 File formatting" >> $GITHUB_STEP_SUMMARY
123+ else
124+ echo "❌ Pre-commit checks failed" >> $GITHUB_STEP_SUMMARY
125+ echo "" >> $GITHUB_STEP_SUMMARY
126+ echo "Please check the logs above for specific failures." >> $GITHUB_STEP_SUMMARY
127+ echo "You can run \`pre-commit run --all-files\` locally to fix issues." >> $GITHUB_STEP_SUMMARY
128+ fi
129+
130+ echo "" >> $GITHUB_STEP_SUMMARY
131+ echo "**Configured hooks:**" >> $GITHUB_STEP_SUMMARY
132+ echo "- trailing-whitespace" >> $GITHUB_STEP_SUMMARY
133+ echo "- end-of-file-fixer" >> $GITHUB_STEP_SUMMARY
134+ echo "- check-yaml" >> $GITHUB_STEP_SUMMARY
135+ echo "- terraform_fmt" >> $GITHUB_STEP_SUMMARY
136+ echo "- terraform_validate" >> $GITHUB_STEP_SUMMARY
137+ echo "- terraform_docs" >> $GITHUB_STEP_SUMMARY
138+ echo "- terraform_tflint" >> $GITHUB_STEP_SUMMARY
0 commit comments