forked from brenno-andrade-novais/sum-numbers
-
Notifications
You must be signed in to change notification settings - Fork 0
285 lines (254 loc) · 10.8 KB
/
release-pipeline.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
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
name: Deploy Backend and Frontend
on:
workflow_run:
workflows: ["Build Artifacts"]
types:
- completed
jobs:
get_artifacts:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: 'Download version artifact'
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let run_number = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "run_number"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: run_number.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/run_number.zip`, Buffer.from(download.data));
- name: Unzip run_number
run: unzip run_number.zip -d .
- name: 'Download frontend artifact'
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let fs = require('fs');
fs.readFile('run_number.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
let run_number = data.trim();
let version = `1.0.${run_number}`
let frontend_build = allArtifacts.data.artifacts.find((artifact) => {
return artifact.name == `frontend-build-${version}`;
});
console.log('Version:', version);
console.log('Frontend build:', frontend_build);
if (frontend_build) {
github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: frontend_build.id,
archive_format: 'zip',
}).then(download => {
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/frontend-build-${version}.zip`, Buffer.from(download.data));
}).catch(error => {
console.error('Error downloading artifact:', error);
});
} else {
console.error('Frontend build not found for version:', version);
}
});
- name: Generate Frontend Semantic Version
id: frontend_version
run: echo "::set-output name=version::1.0.$(cat run_number.txt)"
- name: Unzip Build Folder Artifact
run: unzip frontend-build-${{ steps.frontend_version.outputs.version }}.zip -d ./frontend-build
- name: Upload files run_number as artifact
uses: actions/upload-artifact@v4
with:
name: run_number
path: run_number.txt
- name: Upload files frontend-build as artifacts
uses: actions/upload-artifact@v4
with:
name: frontend-build
path: frontend-build
deploy-backend:
needs: get_artifacts
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
environment:
name: 'production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download run_number.txt artifact
uses: actions/download-artifact@v4
with:
name: run_number
path: .
- name: Generate Backend Semantic Version
id: backend_version
run: echo "::set-output name=version::1.0.$(cat run_number.txt)"
- name: Deploy to Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v2
with:
app-name: 'sum-calculator-web-app'
slot-name: 'production'
publish-profile: ${{ secrets.AzureAppService_PublishProfile_296015baad9746858deb62e2d6f4d14c }}
images: 'inspiradevops.azurecr.io/${{ secrets.IMAGE_NAME}}:${{ steps.backend_version.outputs.version }}'
deploy-frontend:
needs: get_artifacts
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: Download frontend-build artifact
uses: actions/download-artifact@v4
with:
name: frontend-build
path: frontend-build
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_YELLOW_FOREST_0E6CD570F }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: "upload"
app_location: "frontend-build"
output_location: "frontend/build"
skip_app_build: true
rollback:
runs-on: ubuntu-latest
needs: [deploy-backend, deploy-frontend]
if: ${{ always() && (needs.deploy-backend.result == 'failure' || needs.deploy-frontend.result == 'failure') }}
steps:
- name: 'Download version artifact'
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let run_number = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "run_number"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: run_number.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/run_number.zip`, Buffer.from(download.data));
- name: Unzip run_number
run: |
unzip run_number.zip -d .
cat run_number.txt
awk '{$1=$1-1}1' run_number.txt > tmp_run_number && mv tmp_run_number run_number.txt
- name: Generate Semantic Version
id: version
run: echo "::set-output name=version::1.0.$(cat run_number.txt)"
- name: Get URL of last-but-one successful build artifacts
id: get_artifact_url
run: |
repo_full_name=$(echo "${{ github.repository }}" | tr '[:upper:]' '[:lower:]')
branch=$(echo "${{ github.ref }}" | awk -F '/' '{print $3}')
last_but_one_run_id=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "https://api.github.com/repos/$repo_full_name/actions/workflows/build-pipeline.yml/runs?event=push&status=success&branch=$branch" | jq '.workflow_runs[1].id')
echo "::set-output name=last_but_one_run_id::$last_but_one_run_id"
- name: 'Download frontend artifact'
uses: actions/github-script@v6
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ steps.get_artifact_url.outputs.last_but_one_run_id }},
});
let fs = require('fs');
fs.readFile('run_number.txt', 'utf8', (err, data) => {
if (err) {
console.error('Error reading file:', err);
return;
}
let run_number = data.trim();
let version = `1.0.${run_number}`
let frontend_build = allArtifacts.data.artifacts.find((artifact) => {
return artifact.name == `frontend-build-${version}`;
});
console.log('Version:', version);
console.log('Frontend build:', frontend_build);
if (frontend_build) {
github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: frontend_build.id,
archive_format: 'zip',
}).then(download => {
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/frontend-build-${version}.zip`, Buffer.from(download.data));
}).catch(error => {
console.error('Error downloading artifact:', error);
});
} else {
console.error('Frontend build not found for version:', version);
}
});
- name: Unzip Build Folder Artifact
run: unzip frontend-build-${{ steps.version.outputs.version }}.zip -d ./frontend-build
- name: Rollback Static Web App
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_YELLOW_FOREST_0E6CD570F }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: "upload"
app_location: "frontend-build"
output_location: "frontend/build"
skip_app_build: true
- name: Rollback Azure Web App
id: deploy-to-webapp
uses: azure/webapps-deploy@v2
with:
app-name: 'sum-calculator-web-app'
slot-name: 'production'
publish-profile: ${{ secrets.AzureAppService_PublishProfile_296015baad9746858deb62e2d6f4d14c }}
images: 'inspiradevops.azurecr.io/${{ secrets.IMAGE_NAME }}:${{ steps.version.outputs.version }}'
# notify:
# needs: [deploy-backend, deploy-frontend, rollback]
# runs-on: ubuntu-latest
# if: always()
# steps:
# - name: Send Slack notification on failure
# if: ${{ failure() }}
# uses: rtCamp/action-slack-notify@v2
# env:
# SLACK_CHANNEL: general
# SLACK_COLOR: 'red'
# SLACK_ICON: https://assets.stickpng.com/images/580b57fbd9996e24bc43bdf6.png
# SLACK_MESSAGE: 'Your application deploy have failed.'
# SLACK_TITLE: DEPLOY FAILED
# SLACK_USERNAME: Pipo
# SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
#
# - name: Send Slack notification on success
# if: ${{ success() }}
# uses: rtCamp/action-slack-notify@v2
# env:
# SLACK_CHANNEL: general
# SLACK_COLOR: 'green'
# SLACK_ICON: https://assets.stickpng.com/images/580b57fbd9996e24bc43bdf6.png
# SLACK_MESSAGE: 'Your application deploy have completed successfully.'
# SLACK_TITLE: DEPLOY SUCCEEDED
# SLACK_USERNAME: Pipo
# SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}