Repository navigation
238 lines (225 loc) · 8.39 KB
/
Copy pathterraform-ci.yml
File metadata and controls
238 lines (225 loc) · 8.39 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
name: Terraform CI
on:
workflow_call:
inputs:
aws-region:
type: string
default: 'eu-west-1'
aws-role:
type: string
default: ''
disable-sudo:
type: boolean
default: true
environment:
type: string
default: ''
egress-policy-allowlist:
type: string
default: ''
runs-on:
type: string
default: 'ubuntu-latest'
sops-encrypted-tfvars:
type: string
default: './terraform.tfvars.enc'
sops-plaintext-tfvars:
type: string
default: './terraform.tfvars'
upload-sarif:
type: boolean
default: true
working-directory:
type: string
default: '.'
secrets:
sops-age-key:
required: false
jobs:
terraform-lint:
name: Terraform Linting
runs-on: ${{ inputs.runs-on }}
environment: ${{ inputs.environment != '' && inputs.environment || null }}
permissions:
contents: read
pull-requests: write
security-events: write
id-token: write
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: step-security/harden-runner@b09bb98e06d4d774595224525879c09bc6e98c40 # v2.20.1
with:
disable-sudo: ${{ inputs.disable-sudo }}
egress-policy: audit
allowed-endpoints: >
codeload.github.com:443
raw.githubusercontent.com:443
release-assets.githubusercontent.com:443
${{ inputs.egress-policy-allowlist }}
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: aws-actions/configure-aws-credentials@e6de054238d6b7531b4efff3b6587d9aade6a06c # v6.2.3
if: ${{ inputs.aws-role != '' }}
with:
role-to-assume: ${{ inputs.aws-role }}
aws-region: ${{ inputs.aws-region }}
retry-max-attempts: 2
- run: |
echo "plugin_cache_dir = '$HOME/.terraform.d/plugin-cache'" > ~/.terraformrc
mkdir -p ~/.terraform.d/plugin-cache
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.terraform.d/plugin-cache
key: terraform-providers-${{ hashFiles('**/.terraform.lock.hcl') }}
restore-keys: terraform-providers-
- uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1
- name: Sops Binary Installer
uses: mdgreenwald/mozilla-sops-action@55177c1ff20a2b396280e4c5f53c32b69f0c1e40
- name: Decrypt Secrets
env:
SOPS_AGE_KEY: ${{ secrets.sops-age-key }}
ENCRYPTED_FILE: '${{ inputs.sops-encrypted-tfvars }}'
PLAINTEXT_FILE: '${{ inputs.sops-plaintext-tfvars }}'
run: |
sops decrypt "$ENCRYPTED_FILE" --output "$PLAINTEXT_FILE"
- name: Terraform fmt
id: fmt
run: terraform fmt -check
- name: Terraform Init
id: init
run: terraform init -input=false
- name: Terraform Validate
id: validate
run: terraform validate -no-color
- name: tflint
uses: reviewdog/action-tflint@54a5e5aed57dcfbb4662ec548de876df33d6288d # v1.25.0
with:
working_directory: ${{ inputs.working-directory }}
fail_level: any
filter_mode: nofilter
- name: Run Trivy Scan
uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
with:
hide-progress: true
format: sarif
scan-type: config
exit-code: '1'
output: ${{ inputs.working-directory }}/trivy_results.sarif
github-pat: ${{ secrets.GITHUB_TOKEN }}
- uses: reviewdog/action-setup@d8a7baabd7f3e8544ee4dbde3ee41d0011c3a93f # v1.5.0
- name: Run reviewdog
continue-on-error: true
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo -n "$(cat ./trivy_results.sarif)" | reviewdog -reporter=github-check -f=sarif -level=error -diff="git diff FETCH_HEAD"
- name: Upload results
uses: github/codeql-action/upload-sarif@5595ccaf912efad79be6eef63a5619ff05969be3 # v4.37.6
with:
sarif_file: ${{ inputs.working-directory }}/trivy_results.sarif
category: devops
if: ${{ inputs.upload-sarif }}
terraform-plan:
name: Terraform Plan
runs-on: ${{ inputs.runs-on }}
needs: terraform-lint
environment: ${{ inputs.environment != '' && inputs.environment || null }}
permissions:
contents: read
pull-requests: write
id-token: write
defaults:
run:
working-directory: ${{ inputs.working-directory }}
steps:
- uses: step-security/harden-runner@b09bb98e06d4d774595224525879c09bc6e98c40 # v2.20.1
with:
disable-sudo-and-containers: ${{ inputs.disable-sudo }}
egress-policy: audit
allowed-endpoints: >
${{ inputs.egress-policy-allowlist }}
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: aws-actions/configure-aws-credentials@e6de054238d6b7531b4efff3b6587d9aade6a06c # v6.2.3
if: ${{ inputs.aws-role != '' }}
with:
role-to-assume: ${{ inputs.aws-role }}
aws-region: ${{ inputs.aws-region }}
retry-max-attempts: 2
- run: |
echo "plugin_cache_dir = '$HOME/.terraform.d/plugin-cache'" > ~/.terraformrc
mkdir -p ~/.terraform.d/plugin-cache
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ~/.terraform.d/plugin-cache
key: terraform-providers-${{ hashFiles('**/.terraform.lock.hcl') }}
restore-keys: terraform-providers-
- uses: hashicorp/setup-terraform@dfe3c3f87815947d99a8997f908cb6525fc44e9e # v4.0.1
- name: Sops Binary Installer
uses: mdgreenwald/mozilla-sops-action@55177c1ff20a2b396280e4c5f53c32b69f0c1e40
- name: Decrypt Secrets
env:
SOPS_AGE_KEY: ${{ secrets.sops-age-key }}
ENCRYPTED_FILE: '${{ inputs.sops-encrypted-tfvars }}'
PLAINTEXT_FILE: '${{ inputs.sops-plaintext-tfvars }}'
run: |
sops decrypt "$ENCRYPTED_FILE" --output "$PLAINTEXT_FILE"
- name: Terraform Init
id: init
run: terraform init -input=false
- name: Terraform Plan
id: plan
run: |
terraform plan -no-color -input=false | tee plan.txt
{
echo "plan<<EOF"
cat plan.txt
echo "EOF"
} >> "$GITHUB_OUTPUT"
continue-on-error: true
- uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
if: github.event_name == 'pull_request'
env:
PLAN: '${{ steps.plan.outputs.plan }}'
ACTOR: '${{ github.actor }}'
EVENT: '${{ github.event_name }}'
WORKDIR: '${{ inputs.working-directory }}'
OUTCOME: '${{ steps.plan.outcome }}'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { PLAN, ACTOR, EVENT, WORKDIR, OUTCOME } = process.env;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
})
const botComment = comments.find(comment => {
return comment.user.type === 'Bot' && comment.body.includes('Terraform Plan 📖')
})
const output = `#### Terraform Plan 📖\`${OUTCOME}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>
*Pusher: @${ACTOR}, Action: \`${EVENT}\`, Working Directory: \`${WORKDIR}\`*`;
if (botComment) {
github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: output
})
} else {
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
}