Skip to content

Commit 7013199

Browse files
committed
feat(tf): Create a workflow for checking on terraform projects
* Setup terraform * Check formatting * Initialize the backend, caching providers * Validate the configuration * Generate a plan Project linting will: * Initialize tflint, caching plugins * Run tflint Terraform project is configured to serialize every run. As in-progress jobs may acquire a lock on the remote state while generating the plan, they are not cancelled. fix plan
1 parent 0a88c1b commit 7013199

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
name: "Terraform Project: Generate Plan"
3+
4+
"on":
5+
workflow_call:
6+
secrets:
7+
GH_TOKEN:
8+
description: "Github PAT. Used for managing Github ressources beyond the scope of short-live token (organization, other repositories, etc)"
9+
required: false
10+
TFE_TOKEN:
11+
description: "Terraform Cloud Token. Used for backend's authentication and ressource management"
12+
required: false
13+
jobs:
14+
terraform:
15+
name: "Terraform Project"
16+
runs-on: ubuntu-latest
17+
concurrency:
18+
group: ${{ github.repository }}
19+
cancel-in-progress: false
20+
steps:
21+
- id: repo_checkout
22+
name: "Repository: Checkout"
23+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
24+
- id: tf_version
25+
name: "Terraform: Get version"
26+
run: |
27+
TF_VERSION=$(cat .terraform-version 2>/dev/null || echo latest)
28+
echo "TF_VERSION=$TF_VERSION" >> $GITHUB_OUTPUT
29+
- id: tf_setup
30+
name: "Terraform: Setup"
31+
uses: hashicorp/setup-terraform@97f030cf6dc0b4f5e0da352c7bca9cca34579800 # v3.1.0
32+
with:
33+
cli_config_credentials_token: ${{ secrets.TFE_TOKEN }}
34+
terraform_version: ${{steps.tf_version.outputs.TF_VERSION}}
35+
- id: tf_fmt
36+
name: "Terraform: Check formatting"
37+
run: terraform fmt -check -diff -recursive ./
38+
- name: "Terraform: Cache providers directory"
39+
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
40+
with:
41+
path: ./.terraform/providers
42+
key: tf-${{ steps.tf_version.outputs.TF_VERSION }}-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('.terraform.lock.hcl') }}
43+
- id: tf_init
44+
name: "Terraform: Prepare working directory"
45+
run: terraform init
46+
- id: tf_validate
47+
name: "Terraform: Check configuration is valid"
48+
run: terraform validate
49+
- id: tf_plan
50+
name: "Terraform: Generate terraform plan"
51+
run: terraform plan -input=false -no-color
52+
env:
53+
TFE_TOKEN: ${{ secrets.TFE_TOKEN }}
54+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
55+
tflint:
56+
name: "Project linting"
57+
runs-on: ubuntu-latest
58+
steps:
59+
- id: repo_checkout
60+
name: "Repository: Checkout"
61+
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
62+
- id: tflint_cache
63+
name: "TFLint: Update cache"
64+
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
65+
with:
66+
path: ~/.tflint.d/plugins
67+
key: tflint-${{ hashFiles('.tflint.hcl') }}
68+
- id: tflint_setup
69+
name: "TFLint: Setup"
70+
uses: terraform-linters/setup-tflint@19a52fbac37dacb22a09518e4ef6ee234f2d4987 # v4.0.0
71+
- id: tflint_init
72+
name: "TFLint: Install plugins"
73+
run: tflint --init
74+
- id: tflint_inspect
75+
name: "TFLint: Inspect code"
76+
run: tflint -f compact --call-module-type=local

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ jobs:
6060

6161
```
6262

63+
## tf_project_plan.yaml
64+
65+
Run some checks on terraform code and generate a plan, usefull when creating pull request on a project:
66+
* Formatting check using `terraform fmt`.
67+
* Configuration validation using `terraform validate`.
68+
* Generate a plan using `terraform plan`.
69+
* Linting using [`tflint`](https://github.com/terraform-linters/tflint).
70+
71+
The job is configured to avoid deadlocks on the remote state. As such only one job will run at a time, and running jobs are not cancelled.
72+
73+
Include the following jobs in your existing workflows to use this workflow:
74+
```yaml
75+
[...]
76+
jobs:
77+
merge_tf:
78+
uses: scaleway-terraform-modules/wokflows/.github/workflows/tf_project_apply.yaml@main
79+
secrets: inherit
80+
81+
```
82+
6383
## yaml_check.yaml
6484

6585
Run [`yamllint`](https://www.yamllint.com/).

0 commit comments

Comments
 (0)