-
Notifications
You must be signed in to change notification settings - Fork 0
348 lines (325 loc) · 11 KB
/
initial-deploy.yml
File metadata and controls
348 lines (325 loc) · 11 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
name: Initial create workflow
on:
push:
permissions: write-all
jobs:
deploy:
env:
TF_CLOUD_ORGANIZATION: ${{ github.repository_owner }}
TF_API_TOKEN: ${{ secrets.TFE_TOKEN }}
name: "Build and Deploy"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Create Release branch
uses: JamesIves/github-pages-deploy-action@v4
with:
branch: release # The branch the action should deploy to.
folder: . # The folder the action should deploy.
- name: Checkout Release branch
uses: actions/checkout@v4
with:
ref: release
path: release
- name: Get next version
uses: reecetech/version-increment@2023.9.3
id: version
with:
scheme: calver
increment: patch
- name: Build
run: |
cd release
npm install
npm run build
- name: Create workspace
id: create_workspace_dev
run: |
cat > payload.json <<EOF
{
"data": {
"attributes": {
"name": "${{ github.event.repository.name }}-dev",
"auto-apply": "true"
},
"type": "workspaces"
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
--output output.txt \
https://app.terraform.io/api/v2/organizations/${{ github.repository_owner }}/workspaces
echo "workspace_id=$(cat output.txt | jq -r '.[].id')" >> $GITHUB_OUTPUT
- name: Create workspace
id: create_workspace_prod
run: |
cat > payload.json <<EOF
{
"data": {
"attributes": {
"name": "${{ github.event.repository.name }}-prod",
"auto-apply": "true"
},
"type": "workspaces"
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
--output output.txt \
https://app.terraform.io/api/v2/organizations/${{ github.repository_owner }}/workspaces
echo "workspace_id=$(cat output.txt | jq -r '.[].id')" >> $GITHUB_OUTPUT
- name: Create workspace variables dev
run: |
cat > payload.json <<EOF
{
"data": {
"type":"vars",
"attributes": {
"key":"project_name",
"value":"${{ github.event.repository.name }}",
"description":"project name",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"${{ steps.create_workspace_dev.outputs.workspace_id }}",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/vars
cat > payload.json <<EOF
{
"data": {
"type":"vars",
"attributes": {
"key":"environment",
"value":"dev",
"description":"organization name",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"${{ steps.create_workspace_dev.outputs.workspace_id }}",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/vars
cat > payload.json <<EOF
{
"data": {
"type":"vars",
"attributes": {
"key":"VERSION",
"value":"${{ steps.version.outputs.version }}",
"description":"code version",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"${{ steps.create_workspace_dev.outputs.workspace_id }}",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/vars
- name: Create workspace variables prod
run: |
cat > payload.json <<EOF
{
"data": {
"type":"vars",
"attributes": {
"key":"project_name",
"value":"${{ github.event.repository.name }}",
"description":"project name",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"${{ steps.create_workspace_prod.outputs.workspace_id }}",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/vars
cat > payload.json <<EOF
{
"data": {
"type":"vars",
"attributes": {
"key":"environment",
"value":"prod",
"description":"organization name",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"${{ steps.create_workspace_prod.outputs.workspace_id }}",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/vars
cat > payload.json <<EOF
{
"data": {
"type":"vars",
"attributes": {
"key":"VERSION",
"value":"${{ steps.version.outputs.version }}",
"description":"code version",
"category":"terraform",
"hcl":false,
"sensitive":false
},
"relationships": {
"workspace": {
"data": {
"id":"${{ steps.create_workspace_prod.outputs.workspace_id }}",
"type":"workspaces"
}
}
}
}
}
EOF
curl \
--header "Authorization: Bearer ${{ secrets.TFE_TOKEN }}" \
--header "Content-Type: application/vnd.api+json" \
--request POST \
--data @payload.json \
https://app.terraform.io/api/v2/vars
- name: Create release
uses: "actions/github-script@v6"
env:
VERSION: ${{ steps.version.outputs.version }}
with:
github-token: "${{ github.token }}"
script: |
try {
const response = await github.rest.repos.createRelease({
draft: false,
generate_release_notes: true,
name: process.env.VERSION,
owner: context.repo.owner,
prerelease: false,
repo: context.repo.repo,
tag_name: process.env.VERSION + '-initial',
target_commitish: 'release'
});
core.exportVariable('RELEASE_ID', response.data.id);
core.exportVariable('RELEASE_UPLOAD_URL', response.data.upload_url);
} catch (error) {
core.setFailed(error.message);
}
- uses: hashicorp/tfc-workflows-github/actions/upload-configuration@v1.1.1
id: upload_dev
with:
workspace: ${{ github.event.repository.name }}-dev
directory: release
- uses: hashicorp/tfc-workflows-github/actions/upload-configuration@v1.1.1
id: upload_prod
with:
workspace: ${{ github.event.repository.name }}-prod
directory: release
- name: Create Apply Run dev
uses: hashicorp/tfc-workflows-github/actions/create-run@v1.1.1
id: apply-run-dev
if: success() || failure()
with:
workspace: ${{ github.event.repository.name }}-dev
configuration_version: ${{ steps.upload_dev.outputs.configuration_version_id }}
plan_only: false
- name: Create Apply Run prod
uses: hashicorp/tfc-workflows-github/actions/create-run@v1.1.1
id: apply-run-prod
if: success() || failure()
with:
workspace: ${{ github.event.repository.name }}-prod
configuration_version: ${{ steps.upload_prod.outputs.configuration_version_id }}
plan_only: false
- name: Disable initial workflow
run: |
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/actions/workflows/initial-deploy.yml/disable
env:
GH_TOKEN: ${{ github.token }}
- name: Delete release branch
continue-on-error: true
run: |
git config --global user.name "${{ github.actor }}"
git config --global user.email "${{ github.actor }}@users.noreply.github.com"
git config pull.rebase true
resp=`git ls-remote --heads origin release | wc -m | sed 's/ //g'`
if [ $resp -gt 0 ]; then
git push origin --delete release
fi