-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_terraformEnvironmentTemplate.yml
More file actions
243 lines (215 loc) · 7.23 KB
/
_terraformEnvironmentTemplate.yml
File metadata and controls
243 lines (215 loc) · 7.23 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
239
240
241
242
243
name: Terraform Template
on:
workflow_call:
inputs:
environment:
required: true
type: string
description: "Specifies the environment of the deployment."
config:
required: true
type: string
description: "Specifies the configuration folder for the deployment."
terraform_version:
required: true
type: string
description: "Specifies the terraform version."
node_version:
required: true
type: number
description: "Specifies the node version."
working_directory:
required: true
type: string
description: "Specifies the working directory."
tenant_id:
required: true
type: string
description: "Specifies the tenant id of the deployment."
subscription_id:
required: true
type: string
description: "Specifies the subscription id of the deployment."
secrets:
CLIENT_ID:
required: true
description: "Specifies the client id."
MY_SAMPLE_SECRET:
required: true
description: "Specifies a sample secret."
permissions:
id-token: write
contents: read
pull-requests: write
jobs:
lint:
name: Terraform Lint
runs-on: [ubuntu-latest]
continue-on-error: false
steps:
# Setup Terraform
- name: Setup Terraform
id: terraform_setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}
terraform_wrapper: true
# Check Out Repository
- name: Check Out Repository
id: checkout_repository
uses: actions/checkout@v4
# Terraform Format
- name: Terraform Format
id: terraform_format
working-directory: ${{ inputs.working_directory }}
run: |
terraform fmt -check -recursive
# Add Pull Request Comment
- name: Add Pull Request Comment
uses: actions/github-script@v7
id: pr_comment
if: github.event_name == 'pull_request'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Lint Results
* Terraform Version 📎\`${{ inputs.terraform_version }}\`
* Working Directory 📂\`${{ inputs.working_directory }}\`
* Terraform Format and Style 🖌\`${{ steps.terraform_format.outcome }}\``;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
plan:
name: Terraform Plan
runs-on: [self-hosted]
continue-on-error: false
environment: ${{ inputs.environment }}
needs: [lint]
concurrency:
group: terraform-${{ inputs.config }}-${{ inputs.environment }}
cancel-in-progress: false
env:
ARM_TENANT_ID: ${{ inputs.tenant_id }}
ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }}
ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
ARM_USE_OIDC: true
steps:
# Setup Node
- name: Setup Node
id: node_setup
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node_version }}
# Setup Terraform
- name: Setup Terraform
id: terraform_setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}
terraform_wrapper: true
# Check Out Repository
- name: Check Out Repository
id: checkout_repository
uses: actions/checkout@v4
# Terraform Init
- name: Terraform Init
id: terraform_init
working-directory: ${{ inputs.working_directory }}
run: |
terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend
env:
CONFIG: ${{ inputs.config }}
# Terraform Validate
- name: Terraform Validate
id: terraform_validate
working-directory: ${{ inputs.working_directory }}
run: |
terraform validate
# Terraform Plan
- name: Terraform Plan
id: terraform_plan
working-directory: ${{ inputs.working_directory }}
run: |
terraform plan -var-file="../../config/${CONFIG}/vars.tfvars" -input=false
env:
CONFIG: ${{ inputs.config }}
TF_VAR_my_secret: ${{ secrets.MY_SAMPLE_SECRET }}
# Add Pull Request Comment
- name: Add Pull Request Comment
id: pr_comment
uses: actions/github-script@v7
if: github.event_name == 'pull_request'
continue-on-error: true
env:
PLAN: "terraform\n${{ steps.terraform_plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const output = `#### Terraform Validation & Plan Results
* Terraform Version 📎\`${{ inputs.terraform_version }}\`
* Working Directory 📂\`${{ inputs.working_directory }}\`
* Terraform Initialization ⚙️\`${{ steps.terraform_init.outcome }}\`
* Terraform Validation 🤖\`${{ steps.terraform_validate.outcome }}\`
* Terraform Plan 📖\`${{ steps.terraform_plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`\n
${process.env.PLAN}
\`\`\`
</details>`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
})
apply:
name: Terraform Apply
runs-on: [self-hosted]
continue-on-error: false
environment: ${{ inputs.environment }}
# if: github.event_name == 'push' || github.event_name == 'release'
needs: [plan]
concurrency:
group: terraform-${{ inputs.config }}-${{ inputs.environment }}
cancel-in-progress: false
env:
ARM_TENANT_ID: ${{ inputs.tenant_id }}
ARM_SUBSCRIPTION_ID: ${{ inputs.subscription_id }}
ARM_CLIENT_ID: ${{ secrets.CLIENT_ID }}
ARM_USE_OIDC: true
steps:
# Setup Node
- name: Setup Node
id: node_setup
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node_version }}
# Setup Terraform
- name: Setup Terraform
id: terraform_setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform_version }}
terraform_wrapper: true
# Check Out Repository
- name: Check Out Repository
id: checkout_repository
uses: actions/checkout@v4
# Terraform Init
- name: Terraform Init
working-directory: ${{ inputs.working_directory }}
run: |
terraform init -backend-config=../../config/${CONFIG}/azurerm.tfbackend
env:
CONFIG: ${{ inputs.config }}
# Terraform Apply
- name: Terraform Apply
working-directory: ${{ inputs.working_directory }}
run: |
terraform apply -var-file="../../config/${CONFIG}/vars.tfvars" -auto-approve -input=false
env:
CONFIG: ${{ inputs.config }}
TF_VAR_my_secret: ${{ secrets.MY_SAMPLE_SECRET }}