Skip to content

Commit 98c45ae

Browse files
authored
Merge pull request #318 from Staffbase/CIV-812-Add-Reusable-Workflow-for-Terraform-Format
CIV-812 Add Reusable Workflow for Terraform Format
2 parents b257f8c + ebb2856 commit 98c45ae

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Terraform
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
terraform-version:
7+
required: false
8+
type: string
9+
default: "latest"
10+
comitter-name:
11+
required: false
12+
type: string
13+
default: "staffbase-actions[bot]"
14+
comitter-email:
15+
required: false
16+
type: string
17+
default: "staffbase-actions[bot]@users.noreply.github.com"
18+
secrets:
19+
app-id:
20+
required: true
21+
private-key:
22+
required: true
23+
24+
jobs:
25+
format:
26+
name: Format
27+
runs-on: ubuntu-24.04
28+
steps:
29+
- name: Get App Token
30+
uses: actions/create-github-app-token@v1.11.0
31+
id: get_token
32+
with:
33+
app-id: ${{ secrets.app-id }}
34+
private-key: ${{ secrets.private-key }}
35+
36+
- name: Checkout
37+
uses: actions/checkout@v4.2.2
38+
with:
39+
token: ${{ steps.get_token.outputs.token }}
40+
41+
- name: Setup Terraform
42+
uses: hashicorp/setup-terraform@v3.1.2
43+
with:
44+
terraform_version: ${{ inputs.terraform-version }}
45+
46+
- name: Terraform Format
47+
id: format
48+
run: terraform fmt -recursive -diff
49+
50+
- name: Find Format Comment
51+
if: ${{ steps.format.outputs.stdout != '' }}
52+
uses: peter-evans/find-comment@v3
53+
id: comment
54+
with:
55+
issue-number: ${{ github.event.number }}
56+
comment-author: "github-actions[bot]"
57+
body-includes: "Terraform `format` "
58+
59+
- name: Comment Hint when Terraform Format failed
60+
if: ${{ steps.format.outputs.stdout != '' }}
61+
uses: peter-evans/create-or-update-comment@v4
62+
with:
63+
comment-id: ${{ steps.comment.outputs.comment-id }}
64+
issue-number: ${{ github.event.number }}
65+
edit-mode: replace
66+
body: |
67+
### Terraform `format` applied changes
68+
We found some linting issues in your files. We automatically fixed them for you. See the diff below for details.
69+
70+
<details><summary>Show Details</summary>
71+
72+
```diff
73+
${{ steps.format.outputs.stdout }}
74+
```
75+
76+
</details>
77+
78+
Hint: You can install Terraform Support in your editor to avoid such issues in the future:
79+
- [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform)
80+
- [Jetbrains](https://plugins.jetbrains.com/plugin/7808-terraform-and-hcl)
81+
82+
- name: Commit changes
83+
if: ${{ steps.format.outputs.stdout != '' }}
84+
run: |
85+
git config --global user.name "${{ inputs.comitter-name }}"
86+
git config --global user.email "${{ inputs.comitter-email }}"
87+
git add .
88+
git commit -m "🚨 Format Terraform files"
89+
git push
90+
91+
- name: Fail if format failed
92+
if: ${{ steps.format.outputs.stdout != '' }}
93+
run: exit 1

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,35 @@ jobs:
493493

494494
</details>
495495

496+
### Terraform Format
497+
498+
<details>
499+
500+
<summary>This GitHub Action checks the formatting of Terraform files and commit fixes if necessary.</summary>
501+
502+
```yml
503+
name: Terraform
504+
505+
on: [pull_request]
506+
507+
jobs:
508+
terraform:
509+
uses: Staffbase/gha-workflows/.github/workflows/template_terraform_format.yml@v7.2.0
510+
with:
511+
# optional: Terraform version, default: latest
512+
terraform-version: latest
513+
# optional: Committer name, default: staffbase-actions[bot]
514+
committer-name: staffbase-actions[bot]
515+
# optional: Committer email, default: staffbase-actions[bot]@users.noreply.github.com
516+
committer-email: staffbase-actions[bot]@users.noreply.github.com
517+
secrets:
518+
# GitHub App is required because GITHUB_TOKEN will not trigger new actions
519+
app-id: ${{ <your-app-id> }}
520+
private-key: ${{ <your-private-key> }}
521+
```
522+
523+
</details>
524+
496525
### TestIO
497526

498527
<details>

0 commit comments

Comments
 (0)