Skip to content

Commit 6caa671

Browse files
committed
chore(ci): enhance Terraform CI workflow with caching and PR plan comments
Signed-off-by: Bryan Dady <bryan@dady.us>
1 parent 4a9a4d9 commit 6caa671

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

.github/workflows/terraform.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
jobs:
99
terraform:
1010
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
pull-requests: write
1114
env:
1215
TF_VAR_cf_api_token: ${{ secrets.TF_VAR_CF_API_TOKEN }}
1316
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }}
@@ -19,6 +22,14 @@ jobs:
1922
with:
2023
terraform_version: ~1.0
2124

25+
- name: Cache Terraform plugins
26+
uses: actions/cache@v4
27+
with:
28+
path: ~/.terraform.d/plugin-cache
29+
key: ${{ runner.os }}-terraform-${{ hashFiles('**/.terraform.lock.hcl') }}
30+
restore-keys: |
31+
${{ runner.os }}-terraform-
32+
2233
- name: Terraform Format
2334
run: terraform fmt -check -recursive
2435

@@ -28,6 +39,38 @@ jobs:
2839
- name: Terraform Validate
2940
run: terraform validate
3041

42+
- name: Terraform Plan
43+
id: plan
44+
if: github.event_name == 'pull_request'
45+
run: terraform plan -no-color
46+
continue-on-error: true
47+
48+
- name: Comment PR with Plan
49+
if: github.event_name == 'pull_request'
50+
uses: actions/github-script@v7
51+
env:
52+
PLAN: "${{ steps.plan.outputs.stdout }}"
53+
with:
54+
script: |
55+
const output = `#### Terraform Plan 📖
56+
57+
<details><summary>Show Plan</summary>
58+
59+
\`\`\`terraform
60+
${process.env.PLAN}
61+
\`\`\`
62+
63+
</details>
64+
65+
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;
66+
67+
github.rest.issues.createComment({
68+
issue_number: context.issue.number,
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
body: output
72+
})
73+
3174
- name: Setup TFLint
3275
uses: terraform-linters/setup-tflint@v4
3376

@@ -38,9 +81,11 @@ jobs:
3881
3982
- name: TFSec Security Scan
4083
uses: aquasecurity/tfsec-action@v1.0.3
84+
continue-on-error: true
4185

4286
- name: Checkov Security Scan
4387
uses: bridgecrewio/checkov-action@v12
88+
continue-on-error: true
4489
with:
4590
directory: .
4691
framework: terraform

0 commit comments

Comments
 (0)