-
Notifications
You must be signed in to change notification settings - Fork 19
257 lines (227 loc) · 8.94 KB
/
terraform.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
---
name: "terraform"
on: # yamllint disable-line rule:truthy
workflow_dispatch:
push:
paths:
- "terraform/**.tf"
- "terraform/terraform.auto.tfvars"
- "terraform/cloud-init/*"
branches:
- "main"
permissions:
id-token: write
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
jobs:
terraform:
name: Terraform Init
runs-on: ubuntu-latest
outputs:
action: ${{ steps.terraform.outputs.action }}
steps:
- id: terraform
name: ${{ github.ref_name }} deployed is ${{ vars.DEPLOYED }}
shell: bash
run: |
env
if [[ -n "${{ vars.DEPLOYED }}" ]]
then
if [[ "${{ vars.DEPLOYED }}" == "true" ]]
then
echo 'action=apply' >> "${GITHUB_OUTPUT}"
else
echo 'action=destroy' >> "${GITHUB_OUTPUT}"
fi
else
echo 'action=skip' >> "${GITHUB_OUTPUT}"
fi
plan:
needs: [terraform]
if: needs.terraform.outputs.action == 'apply'
name: Terraform Plan
runs-on: ubuntu-latest
env:
ARM_SKIP_PROVIDER_REGISTRATION: true
outputs:
tfplanExitCode: ${{ steps.tf-plan.outputs.exitcode }}
steps:
- name: Github repository checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- name: enable-azurerm-backend
env:
ANSIBLE_LOCALHOST_WARNING: false
ANSIBLE_INVENTORY_UNPARSED_WARNING: false
run: |
ansible-playbook enable-azurerm-backend.yml
- name: Microsoft Azure Authentication
uses: azure/login@f83d0254638e35ef1165887ce2c56be3d11c3eb4
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Hashicorp Terraform
uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8
with:
terraform_version: 1.7.1
terraform_wrapper: false
- name: terraform init
id: init
env:
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
TF_IN_AUTOMATION: true
TF_CLI_ARGS_init: -backend-config="storage_account_name=${{ secrets.AZURE_STORAGE_ACCOUNT_NAME }}" -backend-config="container_name=${{ secrets.TFSTATE_CONTAINER_NAME }}" -backend-config="resource_group_name=${{ secrets.AZURE_RESOURCE_GROUP_NAME }}" -backend-config="key=${{ github.ref_name }}" -input=false
run: terraform -chdir=terraform init
- name: terraform plan
id: tf-plan
env:
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
TF_IN_AUTOMATION: true
run: |
export exitcode=0
terraform -chdir=terraform plan -detailed-exitcode -no-color -out tfplan || export exitcode=$?
echo "exitcode=$exitcode" >> "$GITHUB_OUTPUT"
if [ $exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
else
exit 0
fi
- name: Publish Terraform Plan
uses: actions/upload-artifact@0b2256b8c012f0828dc542b3febcab082c67f72b
with:
name: tfplan
path: terraform/tfplan
- name: Create String Output
id: tf-plan-string
run: |
TERRAFORM_PLAN=$(terraform -chdir=terraform show -no-color tfplan)
delimiter="$(openssl rand -hex 8)"
{
echo "summary<<${delimiter}"
echo "## Terraform Plan Output"
echo "<details><summary>Click to expand</summary>"
echo ""
echo '```terraform'
echo "$TERRAFORM_PLAN"
echo '```'
echo "</details>"
echo "${delimiter}"
} >> "$GITHUB_OUTPUT"
- name: Publish Terraform Plan to Task Summary
env:
SUMMARY: ${{ steps.tf-plan-string.outputs.summary }}
run: |
echo "$SUMMARY" >> "$GITHUB_STEP_SUMMARY"
# - name: Push Terraform Output to PR
# if: github.ref != 'refs/heads/main'
# uses: actions/github-script@v6
# env:
# SUMMARY: "${{ steps.tf-plan-string.outputs.summary }}"
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# script: |
# const body = `${process.env.SUMMARY}`;
# github.rest.issues.createComment({
# issue_number: context.issue.number,
# owner: context.repo.owner,
# repo: context.repo.repo,
# body: body
# })
apply:
name: Terraform Apply
if: needs.terraform.outputs.action == 'apply'
runs-on: ubuntu-latest
needs: [terraform, plan]
steps:
- name: Github repository checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- name: enable-azurerm-backend
run: |
ansible-playbook enable-azurerm-backend.yml
- name: Microsoft Azure Authentication
uses: azure/login@f83d0254638e35ef1165887ce2c56be3d11c3eb4
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# - name: ACR login
# uses: azure/docker-login@15c4aadf093404726ab2ff205b2cdd33fa6d054c
# with:
# login-server: "${{ secrets.PROJECTNAME }}.azurecr.io"
# username: ${{ secrets.ARM_CLIENT_ID }}
# password: ${{ secrets.ARM_CLIENT_SECRET }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db
- name: Hashicorp Terraform
uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8
with:
terraform_version: 1.7.1
terraform_wrapper: false
- name: terraform init
id: init
env:
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
TF_IN_AUTOMATION: true
TF_CLI_ARGS_init: -backend-config="storage_account_name=${{ secrets.AZURE_STORAGE_ACCOUNT_NAME }}" -backend-config="container_name=${{ secrets.TFSTATE_CONTAINER_NAME }}" -backend-config="resource_group_name=${{ secrets.AZURE_RESOURCE_GROUP_NAME }}" -backend-config="key=${{ github.ref_name }}" -input=false
run: terraform -chdir=terraform init
- name: Download Terraform Plan
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16
with:
name: tfplan
path: terraform
- name: Terraform Apply
id: apply
env:
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
TF_IN_AUTOMATION: true
run: terraform -chdir=terraform apply -auto-approve tfplan
destroy:
name: Terraform Destroy
needs: [terraform]
if: needs.terraform.outputs.action == 'destroy'
runs-on: ubuntu-latest
steps:
- name: Github repository checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
- name: enable-azurerm-backend
run: |
ansible-playbook enable-azurerm-backend.yml
- name: Microsoft Azure Authentication
uses: azure/login@f83d0254638e35ef1165887ce2c56be3d11c3eb4
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Hashicorp Terraform
uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8
with:
terraform_version: 1.7.1
terraform_wrapper: false
- name: terraform init
id: init
env:
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
TF_IN_AUTOMATION: true
TF_CLI_ARGS_init: -backend-config="storage_account_name=${{ secrets.AZURE_STORAGE_ACCOUNT_NAME }}" -backend-config="container_name=${{ secrets.TFSTATE_CONTAINER_NAME }}" -backend-config="resource_group_name=${{ secrets.AZURE_RESOURCE_GROUP_NAME }}" -backend-config="key=${{ github.ref_name }}" -input=false
run: terraform -chdir=terraform init
- name: terraform destroy
id: destroy
env:
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
TF_IN_AUTOMATION: true
run: |
terraform -chdir=terraform destroy -auto-approve