-
-
Notifications
You must be signed in to change notification settings - Fork 19
116 lines (95 loc) · 3.52 KB
/
tf_tests.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
name: TF Tests
on:
# push:
pull_request:
paths: [.github/workflows/tf_tests.yaml, action.yml, tests/**]
types: [opened, reopened, synchronize, closed]
jobs:
tests:
runs-on: ubuntu-24.04
permissions:
actions: read # Required to download repository artifact.
checks: write # Required to add status summary.
contents: read # Required to checkout repository.
pull-requests: write # Required to add PR comment and label.
strategy:
fail-fast: false
matrix:
test:
- pass_one
# - pass_character_limit
# - fail_data_source_error
# - fail_format_diff
# - fail_invalid_resource_type
steps:
- name: Echo context
env:
GH_JSON: ${{ toJson(github) }}
run: echo "$GH_JSON"
- name: Checkout repository
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- name: Setup TF
uses: hashicorp/setup-terraform@v3
- name: Init TF
id: tf
if: ${{ github.event_name == 'pull_request' }}
uses: ./
with:
command: init
arg-lock: false
working-directory: tests/${{ matrix.test }}
format: true
validate: true
- name: Setup TFLint
if: ${{ github.event_name == 'pull_request' }}
uses: terraform-linters/setup-tflint@v4
with:
tflint_wrapper: true
- name: Run TFLint
id: tflint
if: ${{ github.event_name == 'pull_request' }}
working-directory: tests/${{ matrix.test }}
run: |
tflint --init
tflint --format compact
continue-on-error: true
- name: Comment if TFLint errors
if: ${{ steps.tflint.outputs.exitcode >= 0 }}
env:
GH_TOKEN: ${{ github.token }}
run: |
# Compose TFLint output.
# tflint=$(cat <<EOF
# <details><summary>TFLint error.</summary>
# \`\`\`hcl
# 2 issue(s) found:
# Warning: main.tf:1:1: Warning - Missing version constraint for provider "random" in `required_providers` (terraform_required_providers)
# Warning: main.tf:1:1: Warning - terraform "required_version" attribute is required (terraform_required_version)
# \`\`\`
# </details>
# EOF
# )
# tflint="
# <details><summary>TFLint error.</summary>
# \`\`\`hcl
# 2 issue(s) found:
# Warning: main.tf:1:1: Warning - Missing version constraint for provider "random" in `required_providers` (terraform_required_providers)
# Warning: main.tf:1:1: Warning - terraform "required_version" attribute is required (terraform_required_version)
# \`\`\`
# </details>"
tflint_raw=$(cat <<EOF
${{ steps.tflint.outputs.stdout }}
EOF
)
tflint=$(echo "$tflint_raw" | sed "s/\`/'/g")
# Get body of PR comment from tf step output.
comment=$(gh api /repos/{owner}/{repo}/issues/comments/${{ steps.tf.outputs.comment-id }} --method GET --jq '.body')
# Replace placeholder with TFLint output.
comment="${comment//<!-- placeholder-2 -->/$tflint}"
# Update PR comment combined with TFLint output.
gh api /repos/{owner}/{repo}/issues/comments/${{ steps.tf.outputs.comment-id }} --method PATCH --field body="$comment"
# Exit workflow due to TFLint error.
# exit 1