Skip to content

Commit f6b2d06

Browse files
feat: tf lint and checkes workflow added (#45)
Co-authored-by: Deepak Verma <[email protected]>
1 parent 9e0c309 commit f6b2d06

File tree

6 files changed

+198
-51
lines changed

6 files changed

+198
-51
lines changed

.github/config/.tflint.hcl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://github.com/terraform-linters/tflint/blob/master/docs/guides/config.md
2+
config {
3+
module = false
4+
force = false
5+
}
6+
7+
plugin "aws" {
8+
enabled = true
9+
version = "0.17.0"
10+
source = "github.com/terraform-linters/tflint-ruleset-aws"
11+
deep_check = false
12+
}
13+
14+
rule "aws_instance_invalid_type" {
15+
enabled = false
16+
}
17+
18+
rule "aws_instance_previous_type" {
19+
enabled = false
20+
}
21+
22+
rule "terraform_required_providers" {
23+
enabled = false
24+
}

.github/workflows/tf-checks.yml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Tf check workflow checks for min, max version , terraform fmt , terraform init & terraform validate in your terraform code.
2+
name: tf-checks
3+
4+
on:
5+
workflow_call:
6+
# inputs can be defined to use during workflow call.
7+
inputs:
8+
working_directory:
9+
description: 'Directory where complete example exist of the module.'
10+
required: false
11+
type: string
12+
default: './_example/complete/'
13+
14+
jobs:
15+
# Terrafrom version extract as output.
16+
versionExtract:
17+
name: Get min/max versions
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
# Checkout the repository to the GitHub Actions runner
22+
- name: Checkout
23+
uses: actions/checkout@v3
24+
25+
# Checking terraform Max and Min version .
26+
- name: Terraform min/max versions
27+
id: minMax
28+
uses: clowdhaus/terraform-min-max@main
29+
with:
30+
directory: ${{ inputs.working_directory}}
31+
outputs:
32+
minVersion: ${{ steps.minMax.outputs.minVersion }}
33+
maxVersion: ${{ steps.minMax.outputs.maxVersion }}
34+
35+
# Evaluting terraform version based on version extract.
36+
versionEvaluate:
37+
name: Evaluate Terraform versions
38+
runs-on: ubuntu-latest
39+
needs: versionExtract
40+
strategy:
41+
fail-fast: false
42+
matrix:
43+
version:
44+
- ${{ needs.versionExtract.outputs.minVersion }}
45+
- ${{ needs.versionExtract.outputs.maxVersion }}
46+
directory:
47+
- ${{ inputs.working_directory}}
48+
49+
steps:
50+
# Checkout the repository to the GitHub Actions runner
51+
- name: Checkout
52+
uses: actions/checkout@v3
53+
54+
# Installing terraform version based on version extract.
55+
- name: Install Terraform v${{ matrix.version }}
56+
uses: hashicorp/setup-terraform@v1
57+
with:
58+
terraform_version: ${{ matrix.version }}
59+
60+
# Terraform checks to Init and Validate terraform code.
61+
- name: Init & validate v${{ matrix.version }}
62+
run: |
63+
cd ${{ matrix.directory }}
64+
terraform init
65+
terraform validate
66+
67+
# Action to verfiy terraform formatting .
68+
format:
69+
name: Check code format
70+
runs-on: ubuntu-latest
71+
needs: versionExtract
72+
73+
steps:
74+
# Checkout the repository to the GitHub Actions runner
75+
- name: Checkout
76+
uses: actions/checkout@v2
77+
78+
# Action added to install terraform
79+
- name: Install Terraform v${{ needs.versionExtract.outputs.maxVersion }}
80+
uses: hashicorp/setup-terraform@v1
81+
with:
82+
terraform_version: ${{ needs.versionExtract.outputs.maxVersion }}
83+
84+
# Running command to check terraform formatting changes.
85+
- name: Check Terraform format changes
86+
run: terraform fmt --recursive -check=true

.github/workflows/tf-lint.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Tf lint work flow checks for possible errors, best practices in your terraform code.
2+
name: tf-lint
3+
on:
4+
workflow_call:
5+
# Secrets can be defined to use during workflow call.
6+
secrets:
7+
GITHUB:
8+
required: true
9+
description: 'PAT of the user to run the jobs.'
10+
11+
jobs:
12+
tflint:
13+
runs-on: ubuntu-latest
14+
15+
# Checkout the repository to the GitHub Actions runner
16+
steps:
17+
- uses: actions/checkout@v3
18+
name: Checkout source code
19+
20+
# Action to add tflint plugin based in OS
21+
- uses: actions/cache@v3
22+
name: Cache plugin dir
23+
with:
24+
path: ~/.tflint.d/plugins
25+
key: ubuntu-latest-tflint-${{ hashFiles('.tflint.hcl') }}
26+
27+
#Setting up terraform lint
28+
- uses: terraform-linters/setup-tflint@v3
29+
name: Setup TFLint
30+
with:
31+
tflint_version: v0.44.1
32+
github_token: ${{ secrets.GITHUB }}
33+
34+
# Added tflint config to check tflint additional rules
35+
- uses: terraform-linters/tflint-load-config-action@v1
36+
name: Setup tflint-config
37+
with:
38+
source-repo: clouddrove/github-shared-workflows
39+
source-path: .github/config/.tflint.hcl
40+
41+
# Verfiy the installed tflint version.
42+
- name: Show version
43+
run: tflint --version
44+
45+
- name: init lint
46+
run: tflint --init
47+
env:
48+
# https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/plugins.md#avoiding-rate-limiting
49+
GITHUB_TOKEN: ${{ github.token }}
50+
51+
# command to check tf lint in terraform code.
52+
- name: Run lint
53+
run: tflint --recursive --color --force -f compact

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Above example is just a simple example to call workflow from github shared workf
5858
* [Example for terraform checks with azure cloud](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md#example-for-terraform-checks-with-azure-cloud)
5959
* [Example for terraform checks with aws cloud](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md#example-for-terraform-checks-with-aws-cloud)
6060
* [Example for terraform checks with digitalocean cloud](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-checks.md#example-for-terraform-checks-with-digitalocean-cloud)
61+
6. [Terraform Lint Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-lint.md)
62+
7. [Terraform Checks Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/docs/terraform-static-checks.md)
6163
6264
## Feedback
6365
If you come accross a bug or have any feedback, please log it in our [issue tracker](https://github.com/clouddrove/github-shared-workflows/issues), or feel free to drop us an email at [[email protected]](mailto:[email protected]).

docs/terraform-checks.md

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,21 @@
1-
## [Terraform Checks Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/terraform.yml)
1+
## [Terraform Checks Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/tf-checks.yml)
22

3-
This workflow is used to terraform checks. Workflows have been added in `.github/workflows/terraform.yml`
3+
This workflow automates terraform checks for min, max version , terraform fmt , terraform init & terraform validate in your terraform code. `.github/workflows/tf-checks.yml`
44

55
#### Usage
6-
This workflow is used to terraform checks. Workflows have been added in `.github/workflows/terraform.yml`
6+
There are several checks you can perform to ensure the accuracy and integrity of your infrastructure provisioning process for Major Cloud providers (AWS/Azure/GCP). Warn about version, fmt and terraform validate.
77

8-
#### Example with azure cloud
8+
#### Example
99
```yaml
10-
name: Terraform Checks
11-
12-
on:
13-
pull_request:
14-
15-
jobs:
16-
terraform:
17-
uses: clouddrove/github-shared-workflows/.github/workflows/terraform.yml@master
18-
secrets:
19-
GITHUB: ${{ secrets.GITHUB }}
20-
DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
21-
with:
22-
provider: 'azurerm'
23-
working_directory: './_example/'
24-
```
25-
#### Example with aws cloud
26-
```yaml
27-
name: Terraform Checks
28-
10+
name: tf-checks
2911
on:
12+
push:
13+
branches: [ master ]
3014
pull_request:
31-
32-
jobs:
33-
terraform:
34-
uses: clouddrove/github-shared-workflows/.github/workflows/terraform.yml@master
35-
secrets:
36-
GITHUB: ${{ secrets.GITHUB }}
37-
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
38-
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
39-
AWS_SESSION_TOKEN: ${{ secrets.AWS_SESSION_TOKEN }}
40-
with:
41-
provider: 'aws'
42-
working_directory: './_example/'
43-
```
44-
#### Example with digitalocean cloud
45-
```yaml
46-
name: Terraform Checks
47-
48-
on:
49-
pull_request:
50-
15+
workflow_dispatch:
5116
jobs:
52-
terraform:
53-
uses: clouddrove/github-shared-workflows/.github/workflows/terraform.yml@master
54-
secrets:
55-
GITHUB: ${{ secrets.GITHUB }}
56-
DIGITALOCEAN_ACCESS_TOKEN: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
57-
with:
58-
provider: 'digitalocean'
59-
working_directory: './_example/'
17+
tf-static-checks:
18+
uses: clouddrove/github-shared-workflows/.github/workflows/tf-checks.yml@master
19+
with:
20+
working_directory: './_example/complete/'
6021
```

docs/terraform-lint.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## [Auto Assign Assignee Workflow](https://github.com/clouddrove/github-shared-workflows/blob/master/.github/workflows/tflint.yml)
2+
3+
This workflow automates terraform linter that checks for possible errors, best practices, etc in your terraform code Workflows have been added in `.github/workflows/tflint.yml`
4+
5+
#### Usage
6+
TFLint is a framework and each feature is provided by plugins, the key features are as follows: Find possible errors (like invalid instance types) for Major Cloud providers (AWS/Azure/GCP). Warn about deprecated syntax, unused declarations. Enforce best practices, naming conventions.
7+
8+
#### Example
9+
```yaml
10+
name: TF-Lint
11+
on:
12+
push:
13+
branches: [ master ]
14+
pull_request:
15+
workflow_dispatch:
16+
jobs:
17+
tf-lint:
18+
uses: clouddrove/github-shared-workflows/.github/workflows/tflint.yml@master
19+
secrets:
20+
GITHUB: ${{ secrets.GITHUB }}
21+
```

0 commit comments

Comments
 (0)