generated from twilio-professional-services/flex-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
290 lines (233 loc) · 12.9 KB
/
terraform_keep_alive.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
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
279
280
281
282
283
284
285
286
287
288
289
290
name: 'Terraform Keep Alive'
on:
workflow_dispatch:
schedule:
# runs 3:15AM on the 1st of each month
- cron: '15 3 1 * *'
# this is an example script for maintaining the terraform
# state as a github actions artifact past the limited
# retention period.
#
# it does this by downloading the last state file
# then uploading it again.
#
# this creates a new artifact instance which has
# an extended retention period over the artifact
# that was downloaded
jobs:
touch-state-file-dev:
env:
ENVIRONMENT: dev
runs-on: ubuntu-latest
steps:
- id: download_state
run: |
echo "running download_state for ${{ env.ENVIRONMENT }}"
echo "### Job summary" >> $env:GITHUB_STEP_SUMMARY
echo " - Repo: ${{ github.repository }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Environment: ${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Artifact Name: terraformstatefile-${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " --- " >> $env:GITHUB_STEP_SUMMARY
echo "### Job Results " >> $env:GITHUB_STEP_SUMMARY
# Perform the request
$Repo = "${{ github.repository }}"
$BaseUri = "https://api.github.com"
$ArtifactUri = "$BaseUri/repos/$Repo/actions/artifacts"
$Token = "${{ github.token }}" | ConvertTo-SecureString -AsPlainText
$RestResponse = Invoke-RestMethod -Authentication Bearer -Uri $ArtifactUri -Token $Token -DisableKeepAlive | Select-Object -ExpandProperty artifacts
# If there was a response
if ($RestResponse){
# get the most result artifact from the artifact list
$MostRecentArtifact = $RestResponse | Sort-Object -Property created_at -Descending | where name -eq "terraformstatefile-${{ env.ENVIRONMENT }}" | Select-Object -First 1
if ($MostRecentArtifact){
echo "${{ env.ENVIRONMENT }} Terraform state file found:"
echo $MostRecentArtifact
$MostRecentArtifactURI = $MostRecentArtifact | Select-Object -ExpandProperty archive_download_url
echo " - refreshing ${{ env.ENVIRONMENT }} state file " >> $env:GITHUB_STEP_SUMMARY
$id = $($MostRecentArtifact).id
$name = $($MostRecentArtifact).name
$created_at = $($MostRecentArtifact).created_at
$expires_at = $($MostRecentArtifact).expires_at
echo " - id: $id" >> $env:GITHUB_STEP_SUMMARY
echo " - name: $name" >> $env:GITHUB_STEP_SUMMARY
echo " - created_at: $created_at" >> $env:GITHUB_STEP_SUMMARY
echo " - expires_at: $expires_at" >> $env:GITHUB_STEP_SUMMARY
# download the artifact
Invoke-RestMethod -uri $MostRecentArtifactURI -Token $Token -Authentication bearer -outfile ./state.zip
# expand the zip file
# this is expected to produce a file ./state/terraform.tfstate.enc
Expand-Archive ./state.zip
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
shell: pwsh
- uses: actions/upload-artifact@v3
with:
name: terraformstatefile-${{ env.ENVIRONMENT }}
path: ./state/terraform.tfstate.enc
if-no-files-found: warn
touch-state-file-qa:
env:
ENVIRONMENT: qa
runs-on: ubuntu-latest
steps:
- id: download_state
run: |
echo "running download_state for ${{ env.ENVIRONMENT }}"
echo "### Job summary" >> $env:GITHUB_STEP_SUMMARY
echo " - Repo: ${{ github.repository }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Environment: ${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Artifact Name: terraformstatefile-${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " --- " >> $env:GITHUB_STEP_SUMMARY
echo "### Job Results " >> $env:GITHUB_STEP_SUMMARY
# Perform the request
$Repo = "${{ github.repository }}"
$BaseUri = "https://api.github.com"
$ArtifactUri = "$BaseUri/repos/$Repo/actions/artifacts"
$Token = "${{ github.token }}" | ConvertTo-SecureString -AsPlainText
$RestResponse = Invoke-RestMethod -Authentication Bearer -Uri $ArtifactUri -Token $Token -DisableKeepAlive | Select-Object -ExpandProperty artifacts
# If there was a response
if ($RestResponse){
# get the most result artifact from the artifact list
$MostRecentArtifact = $RestResponse | Sort-Object -Property created_at -Descending | where name -eq "terraformstatefile-${{ env.ENVIRONMENT }}" | Select-Object -First 1
if ($MostRecentArtifact){
echo "${{ env.ENVIRONMENT }} Terraform state file found:"
echo $MostRecentArtifact
$MostRecentArtifactURI = $MostRecentArtifact | Select-Object -ExpandProperty archive_download_url
echo " - refreshing ${{ env.ENVIRONMENT }} state file " >> $env:GITHUB_STEP_SUMMARY
$id = $($MostRecentArtifact).id
$name = $($MostRecentArtifact).name
$created_at = $($MostRecentArtifact).created_at
$expires_at = $($MostRecentArtifact).expires_at
echo " - id: $id" >> $env:GITHUB_STEP_SUMMARY
echo " - name: $name" >> $env:GITHUB_STEP_SUMMARY
echo " - created_at: $created_at" >> $env:GITHUB_STEP_SUMMARY
echo " - expires_at: $expires_at" >> $env:GITHUB_STEP_SUMMARY
# download the artifact
Invoke-RestMethod -uri $MostRecentArtifactURI -Token $Token -Authentication bearer -outfile ./state.zip
# expand the zip file
# this is expected to produce a file ./state/terraform.tfstate.enc
Expand-Archive ./state.zip
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
shell: pwsh
- uses: actions/upload-artifact@v3
with:
name: terraformstatefile-${{ env.ENVIRONMENT }}
path: ./state/terraform.tfstate.enc
if-no-files-found: warn
touch-state-file-test:
env:
ENVIRONMENT: test
runs-on: ubuntu-latest
steps:
- id: download_state
run: |
echo "running download_state for ${{ env.ENVIRONMENT }}"
echo "### Job summary" >> $env:GITHUB_STEP_SUMMARY
echo " - Repo: ${{ github.repository }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Environment: ${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Artifact Name: terraformstatefile-${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " --- " >> $env:GITHUB_STEP_SUMMARY
echo "### Job Results " >> $env:GITHUB_STEP_SUMMARY
# Perform the request
$Repo = "${{ github.repository }}"
$BaseUri = "https://api.github.com"
$ArtifactUri = "$BaseUri/repos/$Repo/actions/artifacts"
$Token = "${{ github.token }}" | ConvertTo-SecureString -AsPlainText
$RestResponse = Invoke-RestMethod -Authentication Bearer -Uri $ArtifactUri -Token $Token -DisableKeepAlive | Select-Object -ExpandProperty artifacts
# If there was a response
if ($RestResponse){
# get the most result artifact from the artifact list
$MostRecentArtifact = $RestResponse | Sort-Object -Property created_at -Descending | where name -eq "terraformstatefile-${{ env.ENVIRONMENT }}" | Select-Object -First 1
if ($MostRecentArtifact){
echo "${{ env.ENVIRONMENT }} Terraform state file found:"
echo $MostRecentArtifact
$MostRecentArtifactURI = $MostRecentArtifact | Select-Object -ExpandProperty archive_download_url
echo " - refreshing ${{ env.ENVIRONMENT }} state file " >> $env:GITHUB_STEP_SUMMARY
$id = $($MostRecentArtifact).id
$name = $($MostRecentArtifact).name
$created_at = $($MostRecentArtifact).created_at
$expires_at = $($MostRecentArtifact).expires_at
echo " - id: $id" >> $env:GITHUB_STEP_SUMMARY
echo " - name: $name" >> $env:GITHUB_STEP_SUMMARY
echo " - created_at: $created_at" >> $env:GITHUB_STEP_SUMMARY
echo " - expires_at: $expires_at" >> $env:GITHUB_STEP_SUMMARY
# download the artifact
Invoke-RestMethod -uri $MostRecentArtifactURI -Token $Token -Authentication bearer -outfile ./state.zip
# expand the zip file
# this is expected to produce a file ./state/terraform.tfstate.enc
Expand-Archive ./state.zip
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
shell: pwsh
- uses: actions/upload-artifact@v3
with:
name: terraformstatefile-${{ env.ENVIRONMENT }}
path: ./state/terraform.tfstate.enc
if-no-files-found: warn
touch-state-file-prod:
env:
ENVIRONMENT: prod
runs-on: ubuntu-latest
steps:
- id: download_state
run: |
echo "running download_state for ${{ env.ENVIRONMENT }}"
echo "### Job summary" >> $env:GITHUB_STEP_SUMMARY
echo " - Repo: ${{ github.repository }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Environment: ${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " - Artifact Name: terraformstatefile-${{ env.ENVIRONMENT }}" >> $env:GITHUB_STEP_SUMMARY
echo " --- " >> $env:GITHUB_STEP_SUMMARY
echo "### Job Results " >> $env:GITHUB_STEP_SUMMARY
# Perform the request
$Repo = "${{ github.repository }}"
$BaseUri = "https://api.github.com"
$ArtifactUri = "$BaseUri/repos/$Repo/actions/artifacts"
$Token = "${{ github.token }}" | ConvertTo-SecureString -AsPlainText
$RestResponse = Invoke-RestMethod -Authentication Bearer -Uri $ArtifactUri -Token $Token -DisableKeepAlive | Select-Object -ExpandProperty artifacts
# If there was a response
if ($RestResponse){
# get the most result artifact from the artifact list
$MostRecentArtifact = $RestResponse | Sort-Object -Property created_at -Descending | where name -eq "terraformstatefile-${{ env.ENVIRONMENT }}" | Select-Object -First 1
if ($MostRecentArtifact){
echo "${{ env.ENVIRONMENT }} Terraform state file found:"
echo $MostRecentArtifact
$MostRecentArtifactURI = $MostRecentArtifact | Select-Object -ExpandProperty archive_download_url
echo " - refreshing ${{ env.ENVIRONMENT }} state file " >> $env:GITHUB_STEP_SUMMARY
$id = $($MostRecentArtifact).id
$name = $($MostRecentArtifact).name
$created_at = $($MostRecentArtifact).created_at
$expires_at = $($MostRecentArtifact).expires_at
echo " - id: $id" >> $env:GITHUB_STEP_SUMMARY
echo " - name: $name" >> $env:GITHUB_STEP_SUMMARY
echo " - created_at: $created_at" >> $env:GITHUB_STEP_SUMMARY
echo " - expires_at: $expires_at" >> $env:GITHUB_STEP_SUMMARY
# download the artifact
Invoke-RestMethod -uri $MostRecentArtifactURI -Token $Token -Authentication bearer -outfile ./state.zip
# expand the zip file
# this is expected to produce a file ./state/terraform.tfstate.enc
Expand-Archive ./state.zip
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
} else {
echo " - :warning: No Terraform state files found for ${{ env.ENVIRONMENT }}, nothing to preserve" >> $env:GITHUB_STEP_SUMMARY
}
shell: pwsh
- uses: actions/upload-artifact@v3
with:
name: terraformstatefile-${{ env.ENVIRONMENT }}
path: ./state/terraform.tfstate.enc
if-no-files-found: warn