Skip to content

Commit cff42e6

Browse files
authored
feat: Add infracost_breakdown hook (antonbabenko#252)
1 parent cc59119 commit cff42e6

File tree

5 files changed

+344
-6
lines changed

5 files changed

+344
-6
lines changed

.github/CONTRIBUTING.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Enjoy the clean, valid, and documented code!
1212
* [Run via Docker](#run-via-docker)
1313
* [Check results](#check-results)
1414
* [Cleanup](#cleanup)
15+
* [Add new hook](#add-new-hook)
16+
* [Before write code](#before-write-code)
17+
* [Prepare basic documentation](#prepare-basic-documentation)
18+
* [Add code](#add-code)
19+
* [Finish with the documentation](#finish-with-the-documentation)
1520

1621
## Run and debug hooks locally
1722

@@ -41,14 +46,15 @@ For example, to test that the [`terraform_fmt`](../README.md#terraform_fmt) hook
4146
To check is your improvement not violate performance, we have dummy execution time tests.
4247

4348
Script accept next options:
44-
49+
<!-- markdownlint-disable no-inline-html -->
4550
| # | Name | Example value | Description |
4651
| --- | ---------------------------------- | ------------------------------------------------------------------------ | ---------------------------------------------------- |
4752
| 1 | `TEST_NUM` | `200` | How many times need repeat test |
4853
| 2 | `TEST_COMMAND` | `'pre-commit try-repo -a /tmp/159/pre-commit-terraform terraform_tfsec'` | Valid pre-commit command |
4954
| 3 | `TEST_DIR` | `'/tmp/infrastructure'` | Dir on what you run tests. |
5055
| 4 | `TEST_DESCRIPTION` | ```'`terraform_tfsec` PR #123:'``` | Text that you'd like to see in result |
5156
| 5 | `RAW_TEST_`<br>`RESULTS_FILE_NAME` | `terraform_tfsec_pr123` | (Temporary) File where all test data will be stored. |
57+
<!-- markdownlint-enable no-inline-html -->
5258

5359
### Run via BASH
5460

@@ -87,3 +93,46 @@ Results will be located at `./test/results` dir.
8793
```bash
8894
sudo rm -rf tests/results
8995
```
96+
97+
## Add new hook
98+
99+
You can use [this PR](https://github.com/antonbabenko/pre-commit-terraform/pull/252) as an example.
100+
101+
### Before write code
102+
103+
1. Try to figure out future hook usage.
104+
2. Confirm the concept with [Anton Babenko](https://github.com/antonbabenko).
105+
106+
### Prepare basic documentation
107+
108+
1. Identify and describe dependencies in [Install dependencies](../README.md#1-install-dependencies) and [Available Hooks](../README.md#available-hooks) sections
109+
110+
### Add code
111+
112+
1. Based on prev. block, add hook dependencies installation to [Dockerfile](../Dockerfile).
113+
Check that works:
114+
* `docker build -t pre-commit --build-arg INSTALL_ALL=true .`
115+
* `docker build -t pre-commit --build-arg <NEW_HOOK>_VERSION=latest .`
116+
* `docker build -t pre-commit --build-arg <NEW_HOOK>_VERSION=<1.2.3> .`
117+
2. Add new hook to [`.pre-commit-hooks.yaml`](../.pre-commit-hooks.yaml)
118+
3. Create hook file. Don't forget to make it executable via `chmod +x /path/to/hook/file`.
119+
4. Test hook. How to do it is described in [Run and debug hooks locally](#run-and-debug-hooks-locally) section.
120+
5. Test hook one more time.
121+
1. Push commit with hook file to GitHub
122+
2. Grab SHA hash of the commit
123+
3. Test hook using `.pre-commit-config.yaml`:
124+
125+
```yaml
126+
repos:
127+
- repo: https://github.com/antonbabenko/pre-commit-terraform # Your repo
128+
rev: 3d76da3885e6a33d59527eff3a57d246dfb66620 # Your commit SHA
129+
hooks:
130+
- id: terraform_docs # New hook name
131+
args:
132+
- --args=--config=.terraform-docs.yml # Some args that you'd like to test
133+
```
134+
135+
### Finish with the documentation
136+
137+
1. Add hook description to [Available Hooks](../README.md#available-hooks).
138+
2. Create and populate a new hook section in [Hooks usage notes and examples](../README.md#hooks-usage-notes-and-examples).

.pre-commit-hooks.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
- id: infracost_breakdown
2+
name: Infracost breakdown
3+
description: Check terraform infrastructure cost
4+
entry: infracost_breakdown.sh
5+
language: script
6+
require_serial: true
7+
files: \.(tf(vars)?|hcl)$
8+
exclude: \.terraform\/.*$
9+
110
- id: terraform_fmt
211
name: Terraform fmt
312
description: Rewrites all Terraform configuration files to a canonical format.

Dockerfile

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ RUN apt update && \
1111
software-properties-common \
1212
curl \
1313
python3 \
14-
python3-pip && \
14+
python3-pip \
15+
# infracost deps
16+
jq && \
1517
# Upgrade pip for be able get latest Checkov
1618
python3 -m pip install --upgrade pip && \
1719
# Cleanup
@@ -41,6 +43,7 @@ RUN curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add - && \
4143
WORKDIR /bin_dir
4244

4345
ARG CHECKOV_VERSION=${CHECKOV_VERSION:-false}
46+
ARG INFRACOST_VERSION=${INFRACOST_VERSION:-false}
4447
ARG TERRAFORM_DOCS_VERSION=${TERRAFORM_DOCS_VERSION:-false}
4548
ARG TERRAGRUNT_VERSION=${TERRAGRUNT_VERSION:-false}
4649
ARG TERRASCAN_VERSION=${TERRASCAN_VERSION:-false}
@@ -54,6 +57,7 @@ ARG TFSEC_VERSION=${TFSEC_VERSION:-false}
5457
ARG INSTALL_ALL=${INSTALL_ALL:-false}
5558
RUN if [ "$INSTALL_ALL" != "false" ]; then \
5659
echo "export CHECKOV_VERSION=latest" >> /.env && \
60+
echo "export INFRACOST_VERSION=latest" >> /.env && \
5761
echo "export TERRAFORM_DOCS_VERSION=latest" >> /.env && \
5862
echo "export TERRAGRUNT_VERSION=latest" >> /.env && \
5963
echo "export TERRASCAN_VERSION=latest" >> /.env && \
@@ -73,6 +77,16 @@ RUN . /.env && \
7377
) \
7478
; fi
7579

80+
# infracost
81+
RUN . /.env && \
82+
if [ "$INFRACOST_VERSION" != "false" ]; then \
83+
( \
84+
INFRACOST_RELEASES="https://api.github.com/repos/infracost/infracost/releases" && \
85+
[ "$INFRACOST_VERSION" = "latest" ] && curl -L "$(curl -s ${INFRACOST_RELEASES}/latest | grep -o -E -m 1 "https://.+?-linux-amd64.tar.gz")" > infracost.tgz \
86+
|| curl -L "$(curl -s ${INFRACOST_RELEASES} | grep -o -E "https://.+?v${INFRACOST_VERSION}/infracost-linux-amd64.tar.gz")" > infracost.tgz \
87+
) && tar -xzf infracost.tgz && rm infracost.tgz && mv infracost-linux-amd64 infracost \
88+
; fi
89+
7690
# Terraform docs
7791
RUN . /.env && \
7892
if [ "$TERRAFORM_DOCS_VERSION" != "false" ]; then \
@@ -131,6 +145,7 @@ RUN . /.env && \
131145
pre-commit --version >> $F && \
132146
terraform --version | head -n 1 >> $F && \
133147
(if [ "$CHECKOV_VERSION" != "false" ]; then echo "checkov $(checkov --version)" >> $F; else echo "checkov SKIPPED" >> $F ; fi) && \
148+
(if [ "$INFRACOST_VERSION" != "false" ]; then echo "$(./infracost --version)" >> $F; else echo "infracost SKIPPED" >> $F ; fi) && \
134149
(if [ "$TERRAFORM_DOCS_VERSION" != "false" ]; then ./terraform-docs --version >> $F; else echo "terraform-docs SKIPPED" >> $F; fi) && \
135150
(if [ "$TERRAGRUNT_VERSION" != "false" ]; then ./terragrunt --version >> $F; else echo "terragrunt SKIPPED" >> $F ; fi) && \
136151
(if [ "$TERRASCAN_VERSION" != "false" ]; then echo "terrascan $(./terrascan version)" >> $F; else echo "terrascan SKIPPED" >> $F ; fi) && \
@@ -159,10 +174,14 @@ COPY --from=builder \
159174
/usr/local/bin/pre-commit \
160175
/usr/bin/git \
161176
/usr/bin/git-shell \
177+
/usr/bin/jq \
162178
/usr/bin/
163179
# Copy terrascan policies
164180
COPY --from=builder /root/ /root/
165181

166182
ENV PRE_COMMIT_COLOR=${PRE_COMMIT_COLOR:-always}
167183

184+
ENV INFRACOST_API_KEY=${INFRACOST_API_KEY:-}
185+
ENV INFRACOST_SKIP_UPDATE_CHECK=${INFRACOST_SKIP_UPDATE_CHECK:-false}
186+
168187
ENTRYPOINT [ "pre-commit" ]

0 commit comments

Comments
 (0)