-
Notifications
You must be signed in to change notification settings - Fork 7
347 lines (328 loc) · 14.3 KB
/
release_and_publish.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
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
name: Release and Publish
on:
pull_request:
branches:
- main
types:
- closed
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
check_pr_size_label:
if: github.event.pull_request.merged == true
name: 'Check PR Size Label'
runs-on: ubuntu-latest
steps:
- name: Step 1 - Check pr size label
id: check_pr_size_label_step
env:
ENV_LABELS: ${{ toJSON(github.event.pull_request.labels) }}
SIZE_LABELS: >-
"Size - XS","Size - S","Size - M","Size - L","Size - XL","Size - XXL"
run: |
IFS=',' read -ra SIZE_LABELS_ARR <<< "$SIZE_LABELS"
# Remove the quotes from SIZE_LABELS_ARR
SIZE_LABELS_ARR=("${SIZE_LABELS_ARR[@]//\"}")
# Get PR Labels
FOUND_LABELS_JSON=$(echo "$ENV_LABELS" | jq -r '.[] | .name')
readarray -t FOUND_LABELS <<<"$FOUND_LABELS_JSON"
echo "FOUND LABELS:"
printf '%s\n' "${FOUND_LABELS[@]}"
# Check if any of the labels in FOUND_LABELS is in SIZE_LABELS_ARR
SIZE_LABEL_FOUND=''
for found_label in "${FOUND_LABELS[@]}"; do
for size_label in "${SIZE_LABELS_ARR[@]}"; do
if [[ "$found_label" == "$size_label" ]]; then
SIZE_LABEL_FOUND=$found_label
echo "Label 'SIZE_LABEL_FOUND' found in SIZE_LABELS"
fi
done
done
SIZE_LABEL_FOUND_EMOJI="❓"
if [ "$SIZE_LABEL_FOUND" == "Size - XS" ]; then
SIZE_LABEL_FOUND_EMOJI="🟢"
elif [ "$SIZE_LABEL_FOUND" == "Size - S" ]; then
SIZE_LABEL_FOUND_EMOJI="🟢"
elif [ "$SIZE_LABEL_FOUND" == "Size - M" ]; then
SIZE_LABEL_FOUND_EMOJI="🟡"
elif [ "$SIZE_LABEL_FOUND" == "Size - L" ]; then
SIZE_LABEL_FOUND_EMOJI="🟠"
elif [ "$SIZE_LABEL_FOUND" == "Size - XL" ]; then
SIZE_LABEL_FOUND_EMOJI="🔴"
elif [ "$SIZE_LABEL_FOUND" == "Size - XXL" ]; then
SIZE_LABEL_FOUND_EMOJI="🔴"
fi
SIZE_LABEL_FOUND_RESULT="$SIZE_LABEL_FOUND_EMOJI $SIZE_LABEL_FOUND"
echo "size_label_found=$SIZE_LABEL_FOUND_RESULT" >> $GITHUB_OUTPUT
outputs:
size_label_found: ${{ steps.check_pr_size_label_step.outputs.size_label_found }}
notify_on_channel_start:
needs: check_pr_size_label
if: github.event.pull_request.merged == true
name: 'Notify on channel - Start'
runs-on: ubuntu-latest
steps:
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Step 1 - 🔔 Send notification on channel
env:
SIZE_LABEL_FOUND: ${{ needs.check_pr_size_label.outputs.size_label_found }}
run: |
NOTIFICATION_MESSAGE="> *WorkFlow: [$GITHUB_WORKFLOW](https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)*"$'\n'
NOTIFICATION_MESSAGE+="> *Repository:* [$GITHUB_REPOSITORY](https://github.com/$GITHUB_REPOSITORY)"$'\n'
NOTIFICATION_MESSAGE+="> *User Actor:* $GITHUB_ACTOR"$'\n\n'
NOTIFICATION_MESSAGE+="> *Pull request:* [#${{github.event.pull_request.number}}](https://github.com/$GITHUB_REPOSITORY/pull/${{ github.event.pull_request.number }})"$'\n'
PR_TITLE='${{ github.event.pull_request.title }}'
PR_BODY='${{ github.event.pull_request.body }}'
PR_BODY=$(echo $PR_BODY | sed 's/\r/\n/g')
NOTIFICATION_MESSAGE+="> *Pull request size label:* $SIZE_LABEL_FOUND"$'\n'
NOTIFICATION_MESSAGE+="> *Pull request title:*"$'\n'
NOTIFICATION_MESSAGE+="> \`\`\`🚩 $PR_TITLE\`\`\`"$'\n'
NOTIFICATION_MESSAGE+="> *Pull request body content:*"$'\n'
NOTIFICATION_MESSAGE+="> \`\`\`$(echo $PR_BODY | sed 's/⬆️/\n⬆️/g')\`\`\`"$'\n\n'
NOTIFICATION_MESSAGE="*👉 #$GITHUB_RUN_NUMBER ($GITHUB_REF_NAME) - 🏁 DEPLOYMENT STARTED*"$'\n\n'"$NOTIFICATION_MESSAGE"
NOTIFICATION_MESSAGE=$(echo $NOTIFICATION_MESSAGE | sed 's/[>()#_\.|-]/\\\\&/g')
echo $NOTIFICATION_MESSAGE
JSON_DATA=$(jq -n \
--arg text "$NOTIFICATION_MESSAGE" \
--arg chat_id "${{ secrets.PERTECHTALKS_DEPLOY_NOTIFICATION_CHANNEL_ID }}" \
--arg message_thread_id "${{ secrets.PERTECHTALKS_DEPLOY_NOTIFICATION_THREAD_ID }}" \
'{
chat_id: $chat_id | tonumber,
text: "🏁 RELEASE STARTED",
message_thread_id: $message_thread_id | tonumber,
parse_mode: "MarkdownV2",
disable_web_page_preview: true
}')
echo $JSON_DATA
curl --location "https://api.telegram.org/bot${{secrets.TELEGRAM_BOT_TOKEN}}/sendMessage" \
--header 'Content-Type: application/json' \
--data "$JSON_DATA"
build_and_deploy:
needs: notify_on_channel_start
name: 'Build application bundle'
runs-on: ubuntu-latest
environment: live
env:
# Sentry
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
steps:
- name: Step 1 - ⚙️ Setup Actions
uses: actions/checkout@v4
with:
fetch-depth: '30'
token: ${{ secrets.AUTOMATION_GITHUB_TOKEN }}
- name: Step 2 - ⚙️ Setup GitHub Config
run: |
git config user.name "🤖 Pereira Tech Talks"
git config user.email "[email protected]"
- uses: actions/setup-node@v4
with:
node-version: '20.16.0'
- name: Step 3 - Verify the existence of 'ghpages' branch
run: |
git fetch origin
git checkout -b ghpages
git pull origin ghpages --rebase || true
- name: Step 4 - Remove previous build
run: |
rm -rf docs
- name: Step 5 - 📁 Cache node modules
id: cache-npm
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
path: |
~/.npm
node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
- if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}
name: Step 6 - ⚙️ Install Dependencies
run: npm install
- name: Step 7 - Build static files
run: |
npm run build:ghpages
if [ ! -d "docs" ]; then
echo "⚠️ Error: docs folder does not exist."
exit 1
fi
- name: Step 8 - Commit build changes
run: |
git add .
git commit -m "Build: Update static files for GitHub Pages" || echo "No changes to commit"
- name: Step 9 - Push changes to ghpages
run: |
git push origin ghpages
env:
GITHUB_TOKEN: ${{ secrets.AUTOMATION_GITHUB_TOKEN }}
- name: Step 10 - Checkout 'main' branch
run: |
git checkout main
release_and_publish:
needs: build_and_deploy
name: 'Release and Publish'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: '30'
token: ${{ secrets.AUTOMATION_GITHUB_TOKEN }}
- uses: actions/setup-node@v4
with:
node-version: '20.16.0'
registry-url: https://registry.npmjs.org/
- name: Step 0 - 📁 Cache node modules
uses: actions/cache@v4
id: cache-npm
env:
cache-name: cache-node-modules
with:
path: |
~/.npm
node_modules
dist
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
- name: Step 1 - 📁 Cache build dist
uses: actions/cache@v4
env:
cache-name: cache-build-dist
with:
path: dist
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
- name: Step 2 - ⚙️ Setup GitHub Config
run: |
git config user.name "🤖 Pereira Tech Talks"
git config user.email "[email protected]"
- name: Step 3 - 📄 Set GitHub release content "BODY" env var
run: |
bash .github/scripts/get_github_release_log.sh
if [[ ! -f git_logs_output.txt ]]; then
echo "⚠️ No description found for release body content."
exit 1
fi
- name: Step 4 -🔄 Prepare release
run: |
npm run release
git push --follow-tags origin main
- name: Step 5 - 🏷️ Set GitHub release "TAG" env vars
run: |
GITHUB_RELEASE_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
if [[ -z $GITHUB_RELEASE_TAG ]]; then
echo "⚠️ No release tag found."
exit 1
fi
echo "::set-env name=GITHUB_RELEASE_TAG::$GITHUB_RELEASE_TAG"
env:
ACTIONS_ALLOW_UNSECURE_COMMANDS: true
- name: Step 6 - 🚀 Publish GitHub release
uses: ncipollo/release-action@v1
with:
name: Release ${{ env.GITHUB_RELEASE_TAG }}
tag: ${{ env.GITHUB_RELEASE_TAG }}
bodyFile: git_logs_output.txt
token: ${{ secrets.AUTOMATION_GITHUB_TOKEN }}
# - name: Step 7 - 🗑️ Deleting source branch
# run: |
# PR_MERGED=$(jq --raw-output .pull_request.merged "$GITHUB_EVENT_PATH")
# if [ "$PR_MERGED" = "true" ]; then
# git push origin --delete "${{ github.event.pull_request.head.ref }}"
# fi
outputs:
package_version: ${{ steps.publish_npm_package.outputs.package_version }}
cleanup_caches:
needs: release_and_publish
name: 'Cleanup caches'
runs-on: ubuntu-latest
steps:
- name: Step 1 - 🗑️ Trigger cleanup caches workflow
run: |
curl --location "https://api.github.com/repos/$GITHUB_REPOSITORY/dispatches" \
--header "Authorization: token ${{ secrets.AUTOMATION_GITHUB_TOKEN }}" \
--header "Content-Type: application/json" \
--data '{
"event_type": "cleanup_caches",
"client_payload": {}
}'
notify_on_channel_end:
needs: [check_pr_size_label, build_and_deploy, release_and_publish, cleanup_caches]
name: 'Notify on channel - End'
if: always() && github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- name: Install jq
run: sudo apt-get update && sudo apt-get install -y jq
- name: Step 1 - 🔔 Send notification on channel
env:
NEEDS_JSON: ${{ toJSON(needs) }}
SIZE_LABEL_FOUND: ${{ needs.check_pr_size_label.outputs.size_label_found }}
run: |
NOTIFICATION_MESSAGE="> *WorkFlow: [$GITHUB_WORKFLOW](https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID)*"$'\n'
NOTIFICATION_MESSAGE+="> *Repository:* [$GITHUB_REPOSITORY]https://github.com/$GITHUB_REPOSITORY)"$'\n'
NOTIFICATION_MESSAGE+="> *User Actor:* $GITHUB_ACTOR"$'\n'
PACKAGE_VERSION=$(echo "${NEEDS_JSON}" | jq -r ".release_and_publish.outputs.package_version")
if [ ! -z "$PACKAGE_VERSION" ]; then
NOTIFICATION_MESSAGE+="> *New Version:* $PACKAGE_VERSION"$'\n'
fi
NOTIFICATION_MESSAGE+=""$'\n'
NOTIFICATION_MESSAGE+="> *Pull request:* [#${{github.event.pull_request.number}}](https://github.com/$GITHUB_REPOSITORY/pull/${{ github.event.pull_request.number }})"$'\n'
PR_TITLE='${{ github.event.pull_request.title }}'
PR_BODY='${{ github.event.pull_request.body }}'
PR_BODY=$(echo $PR_BODY | sed 's/\r/\n/g')
NOTIFICATION_MESSAGE+="> *Pull request size label:* $SIZE_LABEL_FOUND"$'\n'
NOTIFICATION_MESSAGE+="> *Pull request title:*"$'\n'
NOTIFICATION_MESSAGE+="> \`\`\`🚩 $PR_TITLE\`\`\`"$'\n'
NOTIFICATION_MESSAGE+="> *Pull request body content:*"$'\n'
NOTIFICATION_MESSAGE+="> \`\`\`$(echo $PR_BODY | sed 's/⬆️/\n⬆️/g')\`\`\`"$'\n\n'
NOTIFICATION_MESSAGE+="> *Jobs:*"$'\n'
INDEX=1
NEEDS_ORDER=("check_pr_size_label""build_and_deploy" "release_and_publish" "cleanup_caches")
DEPLOYMENT_SUCCESS="true"
for key in "${NEEDS_ORDER[@]}"; do
RESULT=$(echo "${NEEDS_JSON}" | jq -r ".${key}.result")
if [ "$RESULT" == "success" ]; then
EMOJI="✅"
elif [ "$RESULT" == "failure" ]; then
DEPLOYMENT_SUCCESS="false"
EMOJI="❌"
elif [ "$RESULT" == "skipped" ]; then
DEPLOYMENT_SUCCESS="false"
EMOJI="⏩"
else
DEPLOYMENT_SUCCESS="false"
EMOJI="❓"
fi
NOTIFICATION_MESSAGE+="> • *Job $INDEX:* $key $EMOJI"$'\n'
INDEX=$((INDEX+1))
done
if [ "$DEPLOYMENT_SUCCESS" == "true" ]; then
NOTIFICATION_MESSAGE="*👉 #$GITHUB_RUN_NUMBER ($GITHUB_REF_NAME) - 🚀 DEPLOYMENT SUCCESS*"$'\n\n'"$NOTIFICATION_MESSAGE"
else
NOTIFICATION_MESSAGE="*👉 #$GITHUB_RUN_NUMBER ($GITHUB_REF_NAME) - ${{ vars.USERS_TO_NOTIFY }} ⚠️ DEPLOYMENT FAILED*"$'\n\n'"$NOTIFICATION_MESSAGE"
fi
NOTIFICATION_MESSAGE=$(echo $NOTIFICATION_MESSAGE | sed 's/[>()#_\.|-]/\\\\&/g')
echo $NOTIFICATION_MESSAGE
JSON_DATA=$(jq -n \
--arg text "$NOTIFICATION_MESSAGE" \
--arg chat_id "${{ secrets.PERTECHTALKS_DEPLOY_NOTIFICATION_CHANNEL_ID }}" \
--arg message_thread_id "${{ secrets.PERTECHTALKS_DEPLOY_NOTIFICATION_THREAD_ID }}" \
'{
chat_id: $chat_id | tonumber,
text: "RELEASE COMPLETED 🚀",
message_thread_id: $message_thread_id | tonumber,
parse_mode: "MarkdownV2",
disable_web_page_preview: true
}')
echo $JSON_DATA
curl --location "https://api.telegram.org/bot${{secrets.TELEGRAM_BOT_TOKEN}}/sendMessage" \
--header 'Content-Type: application/json' \
--data "$JSON_DATA"