-
Notifications
You must be signed in to change notification settings - Fork 4
297 lines (262 loc) · 9.43 KB
/
deploy-command.yml
File metadata and controls
297 lines (262 loc) · 9.43 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
name: Deploying to staging
run-name: Deploying to staging triggered by ${{ inputs.author }}
concurrency:
group: deploy-staging-pr-${{ inputs.prId }}
cancel-in-progress: true
on:
workflow_dispatch:
inputs:
prId:
description: "PR to deploy"
required: true
repository:
description: "The repository from which the slash command was dispatched"
required: true
comment-id:
description: "The comment-id of the slash command"
required: true
author:
description: "The author that triggered this actions"
required: true
permissions:
contents: write
issues: write
pull-requests: write
statuses: write
checks: write
jobs:
getPRHead:
name: Get PR head SHA
runs-on: ubuntu-latest
outputs:
sha: ${{ steps.pr-head.outputs.result }}
steps:
- name: Get PR head SHA
id: pr-head
uses: actions/github-script@v8
with:
result-encoding: string
script: |
const prId = ${{ inputs.prId }};
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: parseInt(prId, 10)
});
return pr.head.sha;
sendMessage:
name: Send deploying to staging message
runs-on: ubuntu-latest
needs: getPRHead
steps:
- name: Comment on PR
uses: actions/github-script@v8
with:
script: |
const prId = ${{ inputs.prId }};
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const commitHash = '${{ needs.getPRHead.outputs.sha }}';
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: commitHash,
state: 'pending',
target_url: runUrl,
context: 'Deploy to staging',
description: 'Deployment to staging in progress...',
});
checkExistingImage:
name: Check if image already exists
runs-on: ubuntu-latest
needs: [getPRHead, sendMessage]
outputs:
image-exists: ${{ steps.check-image.outputs.exists }}
git-sha: ${{ steps.set-sha.outputs.sha }}
steps:
- name: Set SHA
id: set-sha
run: echo "sha=$(echo "${{ needs.getPRHead.outputs.sha }}" | cut -c1-7)" >> $GITHUB_OUTPUT
- name: Check if Docker image exists
id: check-image
run: |
SHA7=$(echo "${{ needs.getPRHead.outputs.sha }}" | cut -c1-7)
if docker manifest inspect tahminator/codebloom:staging-$SHA7 > /dev/null 2>&1; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "Image staging-$SHA7 already exists"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "Image staging-$SHA7 does not exist"
fi
promoteExisting:
name: Promote existing image to staging-latest
runs-on: ubuntu-latest
needs: [getPRHead, checkExistingImage]
if: needs.checkExistingImage.outputs.image-exists == 'true'
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
ref: ${{ needs.getPRHead.outputs.sha }}
- name: Setup CI
uses: ./.github/composite/setup-ci
with:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
- name: Load secrets
uses: ./.github/composite/load-secrets
with:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
UNLOAD_ENVIRONMENTS: ci
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: docker.io
username: tahminator
password: ${{ env.DOCKER_HUB_PAT }}
- name: Promote existing image
run: |
SHA7=${{ needs.checkExistingImage.outputs.git-sha }}
docker pull tahminator/codebloom:staging-$SHA7
docker tag tahminator/codebloom:staging-$SHA7 tahminator/codebloom:staging-latest
docker push tahminator/codebloom:staging-latest
echo "Promoted staging-$SHA7 to staging-latest"
frontendPreTest:
name: Frontend Pre Test
runs-on: ubuntu-latest
needs: [getPRHead, checkExistingImage]
if: needs.checkExistingImage.outputs.image-exists == 'false'
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
ref: ${{ needs.getPRHead.outputs.sha }}
- name: Run workflow
uses: ./.github/composite/test/frontend-pre-test
with:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
backendPreTest:
name: Backend Compile Test
runs-on: ubuntu-latest
needs: [getPRHead, checkExistingImage]
if: needs.checkExistingImage.outputs.image-exists == 'false'
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
ref: ${{ needs.getPRHead.outputs.sha }}
- name: Run workflow
uses: ./.github/composite/test/backend-pre-test
validateDBSchema:
name: Validate DB Schema on Staging DB
runs-on: ubuntu-latest
needs: [getPRHead, checkExistingImage, backendPreTest]
if: needs.checkExistingImage.outputs.image-exists == 'false'
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
ref: ${{ needs.getPRHead.outputs.sha }}
fetch-depth: 0
- name: Run workflow
uses: ./.github/composite/validate-db
with:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
ENVIRONMENT: staging
SHA: ${{ needs.getPRHead.outputs.sha }}
buildImage:
name: Build Docker Image & Upload to Registry
runs-on: ubuntu-latest
needs:
[
getPRHead,
checkExistingImage,
validateDBSchema,
backendPreTest,
frontendPreTest,
]
if: needs.checkExistingImage.outputs.image-exists == 'false'
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
ref: ${{ needs.getPRHead.outputs.sha }}
- name: Run workflow
uses: ./.github/composite/build-image
with:
ENVIRONMENT: staging
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
redeploy:
name: Redeploy on DigitalOcean
runs-on: ubuntu-latest
needs: [buildImage, promoteExisting, getPRHead]
if: always() && (needs.buildImage.result == 'success' || needs.promoteExisting.result == 'success')
steps:
- name: Checkout Repository
uses: actions/checkout@v6
with:
ref: ${{ needs.getPRHead.outputs.sha }}
fetch-depth: 0
- name: Run workflow
uses: ./.github/composite/redeploy
with:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}
ENVIRONMENT: staging
SHA: ${{ needs.getPRHead.outputs.sha }}
notifyStatus:
name: Notify on deployment status
runs-on: ubuntu-latest
needs:
[
getPRHead,
checkExistingImage,
sendMessage,
backendPreTest,
frontendPreTest,
validateDBSchema,
buildImage,
promoteExisting,
redeploy,
]
if: always()
steps:
- name: Comment on PR about deployment status
uses: actions/github-script@v8
with:
script: |
const prId = ${{ inputs.prId }};
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const commitHash = '${{ needs.getPRHead.outputs.sha }}';
const needsObj = ${{ toJSON(needs) }};
const jobNames = Object.keys(needsObj);
const failedJobs = jobNames.filter((name) => needsObj[name].result === 'failure');
const cancelledJobs = jobNames.filter((name) => needsObj[name].result === 'cancelled');
const successJobs = jobNames.filter((name) => needsObj[name].result === 'success');
let statusMessage;
let status = false;
if (failedJobs.length > 0 && cancelledJobs.length > 0) {
statusMessage = `**Staging deployment failed and was cancelled** for commit ${commitHash}`;
status = false;
} else if (failedJobs.length > 0) {
statusMessage = `**Staging deployment failed** for commit ${commitHash}`;
status = false;
} else if (cancelledJobs.length > 0) {
statusMessage = `**Staging deployment was cancelled** for commit ${commitHash}`;
status = false;
} else {
statusMessage = `**Staging deployment succeeded** for commit ${commitHash}`;
status = true;
}
await github.rest.repos.createCommitStatus({
owner: context.repo.owner,
repo: context.repo.repo,
sha: commitHash,
state: status ? 'success' : 'failure',
target_url: runUrl,
context: "Deploy to staging",
description: status ? "Staging deployment succeeded" : "Staging deployment failed",
});