Skip to content

Commit 117b35c

Browse files
authored
Merge pull request #3 from tomarv2/develop
adding pre-commit file
2 parents 29da153 + 7a6088d commit 117b35c

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

.github/workflows/pre-commit.yml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
name: Pre-Commit
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
- develop
9+
10+
jobs:
11+
# Min Terraform version(s)
12+
getDirectories:
13+
name: Get root directories
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v2
18+
19+
- name: Install Python
20+
uses: actions/setup-python@v2
21+
22+
- name: Build matrix
23+
id: matrix
24+
run: |
25+
DIRS=$(python -c "import json; import glob; print(json.dumps([x.replace('/versions.tf', '') for x in glob.glob('./**/versions.tf', recursive=True)]))")
26+
echo "::set-output name=directories::$DIRS"
27+
outputs:
28+
directories: ${{ steps.matrix.outputs.directories }}
29+
30+
preCommitMinVersions:
31+
name: Min TF validate
32+
needs: getDirectories
33+
runs-on: ubuntu-latest
34+
strategy:
35+
matrix:
36+
directory: ${{ fromJson(needs.getDirectories.outputs.directories) }}
37+
steps:
38+
- name: Checkout
39+
uses: actions/checkout@v2
40+
41+
- name: Install Python
42+
uses: actions/setup-python@v2
43+
44+
- name: Terraform min/max versions
45+
id: minMax
46+
uses: clowdhaus/terraform-min-max@v1.0.1
47+
with:
48+
directory: ${{ matrix.directory }}
49+
50+
- name: Install Terraform v${{ steps.minMax.outputs.minVersion }}
51+
uses: hashicorp/setup-terraform@v1
52+
with:
53+
terraform_version: ${{ steps.minMax.outputs.minVersion }}
54+
55+
- name: Install pre-commit dependencies
56+
run: pip install pre-commit
57+
58+
# - name: Execute pre-commit
59+
# # Run only validate pre-commit check on min version supported
60+
# if: ${{ matrix.directory != '.' }}
61+
# run:
62+
# pre-commit run terraform_validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/*
63+
#
64+
# - name: Execute pre-commit
65+
# # Run only validate pre-commit check on min version supported
66+
# if: ${{ matrix.directory == '.' }}
67+
# run:
68+
# pre-commit run terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)
69+
70+
71+
# Max Terraform version
72+
getBaseVersion:
73+
name: Module max TF version
74+
runs-on: ubuntu-latest
75+
steps:
76+
- name: Checkout
77+
uses: actions/checkout@v2
78+
79+
- name: Terraform min/max versions
80+
id: minMax
81+
uses: clowdhaus/terraform-min-max@v1.0.1
82+
outputs:
83+
minVersion: ${{ steps.minMax.outputs.minVersion }}
84+
maxVersion: ${{ steps.minMax.outputs.maxVersion }}
85+
86+
preCommitMaxVersion:
87+
name: Max TF pre-commit
88+
runs-on: ubuntu-latest
89+
needs: getBaseVersion
90+
strategy:
91+
fail-fast: false
92+
matrix:
93+
version:
94+
- ${{ needs.getBaseVersion.outputs.maxVersion }}
95+
steps:
96+
- name: Checkout
97+
uses: actions/checkout@v2
98+
99+
- name: Install Python
100+
uses: actions/setup-python@v2
101+
102+
- name: Install Terraform v${{ matrix.version }}
103+
uses: hashicorp/setup-terraform@v1
104+
with:
105+
terraform_version: ${{ matrix.version }}
106+
107+
- name: Install pre-commit dependencies
108+
run: |
109+
pip install pre-commit
110+
pip install checkov
111+
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v0.12.1-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
112+
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
113+
114+
- name: Execute pre-commit
115+
# Run all pre-commit checks on max version supported
116+
if: ${{ matrix.version == needs.getBaseVersion.outputs.maxVersion }}
117+
run: pre-commit run --color=always --show-diff-on-failure --all-files

.pre-commit-config.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
repos:
2+
- repo: git://github.com/antonbabenko/pre-commit-terraform
3+
rev: v1.48.0
4+
hooks:
5+
- id: terraform_fmt
6+
# - id: terraform_validate
7+
- id: terraform_tflint
8+
args:
9+
- '--args=--only=terraform_deprecated_interpolation'
10+
- '--args=--only=terraform_deprecated_index'
11+
# - '--args=--only=terraform_unused_declarations'
12+
- '--args=--only=terraform_comment_syntax'
13+
- '--args=--only=terraform_documented_outputs'
14+
- '--args=--only=terraform_documented_variables'
15+
- '--args=--only=terraform_typed_variables'
16+
- '--args=--only=terraform_module_pinned_source'
17+
- '--args=--only=terraform_naming_convention'
18+
- '--args=--only=terraform_required_providers'
19+
# - '--args=--only=terraform_standard_module_structure'
20+
- '--args=--only=terraform_workspace_remote'
21+
22+
- repo: https://github.com/pre-commit/pre-commit-hooks
23+
rev: v3.2.0
24+
hooks:
25+
- id: trailing-whitespace
26+
- id: check-merge-conflict
27+
- id: end-of-file-fixer
28+
- id: check-yaml
29+
30+
- repo: https://github.com/bridgecrewio/checkov.git
31+
rev: '1.0.864' # change to tag or sha
32+
hooks:
33+
- id: checkov
34+
verbose: true
35+
args:
36+
- -d . --framework terraform -o output_format json

0 commit comments

Comments
 (0)