-
Notifications
You must be signed in to change notification settings - Fork 2
325 lines (272 loc) · 9.67 KB
/
ci.yml
File metadata and controls
325 lines (272 loc) · 9.67 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
name: React CI/CD
env:
CI: "true"
IS_ACT: "false"
on:
push:
branches:
- main
- feature/**
pull_request:
branches:
- main
permissions:
contents: write
issues: write
pull-requests: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
name: 🧪 Run Tests and Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js 18
uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
cache-dependency-path: package-lock.json
- name: Install deps (CI)
run: npm ci
- name: Prettier check
run: npm run format:check
- name: ESLint
run: npm run lint
- name: Run tests (no watch) with coverage + summary
run: |
npm test --silent -- --coverage | tee test-output.txt
echo "### ✅ Test Results" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
cat test-output.txt >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
- name: Upload coverage artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: jest-coverage
path: coverage/
if-no-files-found: ignore
release:
name: 🚀 Version & Release
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
outputs:
released: ${{ steps.get_tag.outputs.released }}
tag: ${{ steps.get_tag.outputs.tag }}
steps:
- name: Checkout (full history & tags)
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Setup Node.js 18
uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
cache-dependency-path: package-lock.json
- name: Install deps (CI)
run: npm ci
- name: Semantic Release
id: semantic_release
if: ${{ env.IS_ACT != 'true' }}
uses: cycjimmy/semantic-release-action@v4
with:
extra_plugins: |
@semantic-release/changelog
@semantic-release/git
@semantic-release/github
env:
GITHUB_TOKEN: ${{ secrets.GH_PAT }}
- name: Get created tag
id: get_tag
if: ${{ always() }}
run: |
if [ "${IS_ACT}" = "true" ]; then
echo "tag=local-test" >> $GITHUB_OUTPUT
echo "released=true" >> $GITHUB_OUTPUT
else
git fetch --tags || true
TAGS=$(git tag --points-at HEAD || true)
TAG=$(echo "$TAGS" | head -n1 | tr -d '\r\n')
if [ -n "$TAG" ]; then
echo "tag=$TAG" >> $GITHUB_OUTPUT
echo "released=true" >> $GITHUB_OUTPUT
else
echo "tag=" >> $GITHUB_OUTPUT
echo "released=false" >> $GITHUB_OUTPUT
fi
fi
build:
name: 🏗️ Build (CRA)
runs-on: ubuntu-latest
needs: release
if: needs.release.outputs.released == 'true'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js 18
uses: actions/setup-node@v4
with:
node-version: 18
cache: npm
cache-dependency-path: package-lock.json
- name: Install deps (CI)
run: npm ci
- name: Build
run: npm run build
- name: Add SPA 404 fallback
run: cp build/index.html build/404.html
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: react-build
path: build/
if-no-files-found: error
deploy:
name: 🚀 Deploy to GitHub Pages
runs-on: ubuntu-latest
needs: build
if: needs.release.outputs.released == 'true'
steps:
- name: Download build artifact
if: ${{ env.IS_ACT != 'true' }}
uses: actions/download-artifact@v4
with:
name: react-build
path: build
- name: Deploy using gh-pages action
if: ${{ env.IS_ACT != 'true' }}
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: build
publish_branch: gh-pages
user_name: "github-actions[bot]"
user_email: "github-actions[bot]@users.noreply.github.com"
notify-release:
name: 📢 Telegram Success Notification
runs-on: ubuntu-latest
needs: [release, deploy]
if: needs.release.outputs.released == 'true'
steps:
- name: Checkout (full history & tags)
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Send Telegram notification
env:
IS_ACT: ${{ env.IS_ACT }}
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
ACTION_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
RELEASE_TAG: ${{ needs.release.outputs.tag }}
GITHUB_ACTOR: ${{ github.actor }}
run: |
escape_html() {
printf '%s' "$1" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g' -e 's/"/\"/g'
}
if [ "${IS_ACT}" = "true" ]; then
TAG="local-test"
else
TAG="${RELEASE_TAG}"
fi
LAST_AUTHOR="${GITHUB_ACTOR}"
AUTHOR_LINK="https://github.com/${LAST_AUTHOR}"
DATE=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
eTAG=$(escape_html "$TAG")
eAuthor=$(escape_html "$LAST_AUTHOR")
eAuthorLink=$(escape_html "$AUTHOR_LINK")
eActionRun=$(escape_html "$ACTION_RUN_URL")
eDate=$(escape_html "$DATE")
printf -v MESSAGE '%s\n%s\n%s\n%s\n%s\n%s' \
"🚀 <b>New release published</b>" \
"🛠️ <b>Service:</b> CS Queue Calendar" \
"🏷️ <b>Version:</b> ${eTAG}" \
"👤 <b>Author:</b> <a href=\"${eAuthorLink}\">${eAuthor}</a>" \
"🕓 <b>Date:</b> ${eDate}" \
"🔗 <a href=\"${eActionRun}\">View GitHub Action Run</a>"
RESPONSE="$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" \
--data-urlencode "text=${MESSAGE}" \
-d "parse_mode=HTML" \
-d "disable_web_page_preview=true" \
-w "\n%{http_code}")"
HTTP_CODE="$(echo "$RESPONSE" | tail -n1)"
BODY="$(echo "$RESPONSE" | sed '$d')"
if [ "$HTTP_CODE" != "200" ] || ! echo "$BODY" | grep -q '"ok":true'; then
echo "❌ Telegram API error. HTTP=$HTTP_CODE BODY=$BODY"
exit 1
fi
echo "✅ Telegram notification sent."
notify-error:
name: 📢 Telegram Error Notification
runs-on: ubuntu-latest
needs: [test, release, build, deploy]
if: ${{ failure() }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Send Telegram error notification
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
LAST_AUTHOR="${GITHUB_ACTOR}"
AUTHOR_LINK="https://github.com/${LAST_AUTHOR}"
FAILED_STEPS=()
if [ "${{ needs.test.result }}" = "failure" ]; then
FAILED_STEPS+=("Tests")
fi
if [ "${{ needs.release.result }}" = "failure" ]; then
FAILED_STEPS+=("Release")
fi
if [ "${{ needs.build.result }}" = "failure" ]; then
FAILED_STEPS+=("Build")
fi
if [ "${{ needs.deploy.result }}" = "failure" ]; then
FAILED_STEPS+=("Deploy")
fi
if [ ${#FAILED_STEPS[@]} -eq 0 ]; then
echo "No failed steps detected. Exiting."
exit 0
fi
FAILED_LIST=$(IFS=', '; echo "${FAILED_STEPS[*]}")
escape_html() {
printf '%s' "$1" | sed -e 's/&/\&/g' -e 's/</\</g' -e 's/>/\>/g' -e 's/"/\"/g'
}
eFailedList=$(escape_html "$FAILED_LIST")
eActor=$(escape_html "$LAST_AUTHOR")
eAuthorLink=$(escape_html "$AUTHOR_LINK")
eRunURL=$(escape_html "$GITHUB_RUN_URL")
eDate=$(escape_html "$(date -u +"%Y-%m-%d %H:%M:%S UTC")")
printf -v MESSAGE '%s\n%s\n%s\n%s\n%s\n%s' \
"❌ <b>Pipeline Error Detected</b>" \
"🛠️ <b>Service:</b> CS Queue Calendar" \
"⚠️ <b>Failed Steps:</b> ${eFailedList}" \
"👤 <b>Triggered by:</b> <a href=\"${eAuthorLink}\">${eActor}</a>" \
"🕓 <b>Date:</b> ${eDate}" \
"🔗 <a href=\"${eRunURL}\">View GitHub Action Run</a>"
RESPONSE="$(curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${TELEGRAM_CHAT_ID}" \
--data-urlencode "text=${MESSAGE}" \
-d "parse_mode=HTML" \
-d "disable_web_page_preview=true" \
-w "\n%{http_code}")"
HTTP_CODE="$(echo "$RESPONSE" | tail -n1)"
BODY="$(echo "$RESPONSE" | sed '$d')"
if [ "$HTTP_CODE" != "200" ] || ! echo "$BODY" | grep -q '"ok":true'; then
echo "❌ Telegram API error. HTTP=$HTTP_CODE BODY=$BODY"
exit 1
fi
echo "✅ Telegram error notification sent."