-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.yml
278 lines (275 loc) · 8.43 KB
/
action.yml
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
name: "Terramate All-In-One"
description: "Run common terramate commands in a single GitHub action"
inputs:
go-version:
description: "The version of Go to use"
required: false
default: "latest"
terramate-version:
description: "The version of Terramate to use"
required: false
default: "latest"
terraform-version:
description: "The version of Terraform to use"
required: false
default: "latest"
terramate-args:
description: "Arguments to pass to each Terramate command"
required: false
default: "--changed"
terramate-plan-args:
description: "Arguments to pass to Terramate in the plan step"
required: false
default: ""
terramate-apply-args:
description: "Arguments to pass to Terramate in the apply step"
required: false
default: ""
terramate-destroy-args:
description: "Arguments to pass to Terramate in the destroy step"
required: false
default: "--reverse"
terraform-common-args:
description: "Arguments to pass to each Terraform command"
required: false
default: ""
terraform-init-args:
description: "Arguments to pass to terraform init"
required: false
default: ""
terraform-validate-args:
description: "Arguments to pass to terraform validate"
required: false
default: ""
terraform-plan-args:
description: "Arguments to pass to terraform plan"
required: false
default: ""
terraform-show-args:
description: "Arguments to pass to terraform show"
required: false
default: "-no-color"
terraform-apply-args:
description: "Arguments to pass to terraform apply"
required: false
default: ""
terraform-destroy-args:
description: "Arguments to pass to terraform destroy"
required: false
default: ""
terraform-plan-file:
description: "Path to Terraform plan file"
required: false
default: "out.tfplan"
temp-file:
description: "Path to preview comment file"
required: false
default: "tmp.txt"
setup:
description: "Setup Terramate and Terraform"
required: false
default: "true"
init:
description: "Initialize Terraform"
required: false
default: "true"
lint:
description: "Run linting"
required: false
default: "true"
generate:
description: "Generate code"
required: false
default: "true"
validate:
description: "Validate Terraform"
required: false
default: "true"
plan:
description: "Plan Terraform"
required: false
default: "true"
apply:
description: "Apply Terraform"
required: false
default: "false"
destroy:
description: "Destroy Terraform"
required: false
default: "false"
comment-pull-request:
description: "Comment on pull requests"
required: false
default: "true"
github-token:
description: "GitHub oken required for commenting on pull requests"
required: false
default: ""
cleanup:
description: "Cleanup temporary files"
required: false
default: "true"
runs:
using: "composite"
steps:
- name: Setup Go
if: ${{ inputs.setup == 'true' }}
uses: actions/setup-go@v5
with:
cache: false
- name: Setup Terramate
if: ${{ inputs.setup == 'true' }}
run: |
go install github.com/terramate-io/terramate/cmd/...@${{ inputs.terramate-version }}
shell: bash
- name: Setup Terraform
if: ${{ inputs.setup == 'true' }}
uses: hashicorp/setup-terraform@v3
with:
terraform_wrapper: false
terraform_version: ${{ inputs.terraform-version }}
- name: Lint
if: ${{ inputs.lint == 'true' }}
run: |
terramate fmt --check
shell: bash
- name: List Affected Stacks
id: list
run: |
echo "stdout<<STDOUT" >>$GITHUB_OUTPUT
terramate list ${{ inputs.terramate-args }} >>$GITHUB_OUTPUT
EXIT_CODE=$?
echo "STDOUT" >>$GITHUB_OUTPUT
exit ${EXIT_CODE}
shell: bash
- name: Generate Code
if: ${{ inputs.generate == 'true' && steps.list.outputs.stdout != '' }}
run: |
terramate generate ${{ inputs.terramate-args }}
shell: bash
- name: Initialize
if: ${{ inputs.init == 'true' && steps.list.outputs.stdout != '' }}
run: |
terramate run ${{ inputs.terramate-args }} \
-- terraform init \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-init-args }}
shell: bash
- name: Validate
if: ${{ inputs.validate == 'true' && steps.list.outputs.stdout != '' }}
run: |
terramate run ${{ inputs.terramate-args }} \
-- terraform validate \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-validate-args }}
shell: bash
- name: Plan
if: >-
${{
( inputs.plan == 'true' ||
( inputs.comment-pull-request == 'true' &&
github.event_name == 'pull_request' )
) &&
steps.list.outputs.stdout != '' }}
run: |
terramate run \
${{ inputs.terramate-args }} \
${{ inputs.terramate-plan-args }} \
-- terraform plan \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-plan-args }} \
-out ${{ inputs.terraform-plan-file }}
shell: bash
- name: Generate Preview Comment
if: >-
${{
inputs.comment-pull-request == 'true' &&
github.event_name == 'pull_request'
}}
run: |
set +x
if [[ -z '${{ steps.list.outputs.stdout }}' ]]; then
echo >>${{ inputs.temp-file }} '### No changed stacks.'
cat ${{ inputs.temp-file }} >>$GITHUB_STEP_SUMMARY
else
echo >>${{ inputs.temp-file }} "### List of Changed Stacks"
echo >>${{ inputs.temp-file }}
echo >>${{ inputs.temp-file }} '```bash'
echo >>${{ inputs.temp-file }} "${{ steps.list.outputs.stdout }}"
echo >>${{ inputs.temp-file }} '```'
echo >>${{ inputs.temp-file }}
echo >>${{ inputs.temp-file }} "#### Terraform Plan"
echo >>${{ inputs.temp-file }}
echo >>${{ inputs.temp-file }} '```terraform'
terramate run ${{ inputs.terramate-args }} \
-- terraform show \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-show-args }} \
${{ inputs.terraform-plan-file }} \
2>&1 \
>>${{ inputs.temp-file }}
echo >>${{ inputs.temp-file }} '```'
cat ${{ inputs.temp-file }} >>$GITHUB_STEP_SUMMARY
fi
shell: bash
- name: Publish Plans
if: >-
${{
inputs.comment-pull-request == 'true' &&
github.event_name == 'pull_request'
}}
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ inputs.github-token }}
header: terraform-plan
path: ${{ inputs.temp-file }}
- name: Apply
if: >-
${{
inputs.apply == 'true' &&
steps.list.outputs.stdout != ''
}}
run: |
if [ -f "${{ inputs.terraform-plan-file }}" ]; then
terramate run \
${{ inputs.terramate-args }} \
${{ inputs.terramate-apply-args }} \
-- terraform apply \
-auto-approve \
-input=false \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-apply-args }} \
${{ inputs.terraform-plan-file }}
else
terramate run \
${{ inputs.terramate-args }} \
${{ inputs.terramate-apply-args }} \
-- terraform apply \
-auto-approve \
-input=false \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-apply-args }}
fi
shell: bash
- name: Destroy
if: >-
${{
inputs.destroy == 'true' &&
steps.list.outputs.stdout != ''
}}
run: |
terramate run \
${{ inputs.terramate-args }} \
${{ inputs.terramate-destroy-args }} \
-- terraform destroy \
-auto-approve \
-input=false \
${{ inputs.terraform-common-args }} \
${{ inputs.terraform-destroy-args }}
shell: bash
- name: Cleanup
if: ${{ inputs.cleanup == 'true' }}
run: |
find . -name out.tfplan -exec rm -rf {} \;
rm -f ${{ inputs.temp-file }}
shell: bash