-
Notifications
You must be signed in to change notification settings - Fork 10
426 lines (353 loc) · 16.6 KB
/
ci.yaml
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
name: CI
on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize
- reopened
- labeled
branches:
- main
concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
build:
name: Build And Test
runs-on: ubuntu-latest
timeout-minutes: 20
outputs:
filter: ${{steps.changed_packages.outputs.filter}}
unitTestFilter: ${{steps.changed_packages.outputs.unitTestFilter}}
unitTestMatrix: ${{steps.changed_packages.outputs.unitTestMatrix}}
componentTestFilter: ${{steps.changed_packages.outputs.componentTestFilter}}
componentTestMatrix: ${{steps.changed_packages.outputs.componentTestMatrix}}
env:
GH_TOKEN: ${{ secrets.KONGPONENTS_BOT_PAT }}
steps:
# if previous run did create comment with the reference to PR preview package
# this comment is removed in this step
- name: Remove preview consumption comment
if: ${{ env.GH_TOKEN != '' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr_preview_consumption
delete: true
GITHUB_TOKEN: ${{ env.GH_TOKEN }}
- name: Checkout Source Code
uses: actions/checkout@v4
with:
fetch-depth: 0
# When running on push, we compare the state of PR and main branches. If they are
# identical - we will skip test execution as those were already executed on same code in PR branch
# see 'run-test' step and variable bellow
- name: Check if PR Up to Date
id: 'up-to-date'
if: ${{ github.event_name == 'push' }}
uses: Kong/public-shared-actions/pr-previews/up-to-date@main
with:
github_token: ${{ env.GH_TOKEN }}
- name: Setup PNPM with Dependencies
uses: ./.github/actions/setup-pnpm-with-dependencies/
with:
force-install: true
# see the comment for `up-to-date` action
- name: Need to run tests
id: 'run-tests'
run: |
echo "run-tests=${{ (github.event_name == 'push' && steps.up-to-date.outputs.status == 'true') && 'false' || 'true' }}"
echo "run-tests=${{ (github.event_name == 'push' && steps.up-to-date.outputs.status == 'true') && 'false' || 'true' }}" >> $GITHUB_OUTPUT
# If we detect changes in any of the config files (e.g. lockfile, shared cypress configs, etc.), we need to build every package
# See 'changed_packages' below
- name: Check for config file changes
id: config-files-changed
uses: tj-actions/changed-files@v44
with:
files: |
./vite.config.shared.ts
./cypress.config.ts
./cypress/**
./pnpm-lock.yaml
./eslintrc.cjs
./.stylelintrc.js
- name: Verify no packages are in the wrong scope and have the correct publishConfig.access
run: |
# Verify no packages are in the wrong scope and have the correct publishConfig.access
# Utilize the lerna command to determine the packages that were added/updated
# we execute lerna changed to grab all the packages changed
lernaCommand="changed"
# if config-files-changed detects the change of pnpm-lock.yaml file - we will grab all the packages
if [[ "${{ steps.config-files-changed.outputs.any_modified }}" == "true" ]]; then
lernaCommand="ls"
fi
echo "lernaCommand: ${lernaCommand}"
# Verify no packages exist from the `@kong-ui` scope; if yes, then exit with error
for packageLocation in $(pnpm --silent lerna ${lernaCommand} --l --json | jq -r '.[].location')
do
packageName=$(jq -r '.name' "$packageLocation"/package.json)
publishConfigAccess=$(jq -r '.publishConfig.access' "$packageLocation"/package.json)
# Check if package.json name contains the wrong scope
if [[ "$packageName" == *"@kong-ui/"* ]]; then
echo "::error::The '""$packageName""' package.json contains an incorrect npm scope '""@kong-ui""'. This repository requires packages to have the npm scope of '""@kong-ui-public""'."
exit 1
fi
# Check if the package.json publishConfig.access is incorrect for this repo
if [[ "$publishConfigAccess" != "public" ]]; then
echo "::error::The '""$packageName""' package.json contains the publishConfig.access of '""$publishConfigAccess""'. This repository requires packages to have a publishConfig.access of '""public""'."
exit 1
fi
done
- name: Getting changed packages
id: changed_packages
run: |
# we execute lerna changed to grab all the packages changed
lernaCommand="changed"
# if config-files-changed detects the change of pnpm-lock.yaml file - we will grab all the packages
if [[ "${{ steps.config-files-changed.outputs.any_modified }}" == "true" ]]; then
lernaCommand="ls"
fi
echo "lernaCommand: ${lernaCommand}"
filter=""
unitTestFilter=""
unitTestMatrix=""
componentTestFilter=""
componentTestMatrix=""
# get the list of changed packages, grab the location
for pkgFolder in $(pnpm --silent lerna ${lernaCommand} --l --json|jq -r '.[].location')
do
# remove current folder from the location path
pkgFolder="${pkgFolder/$(pwd)\//}"
# add package path to pnpm filter
filter="${filter}${pkgFolder}|"
# if there is cypress tests in the package, add package path to cypress spec
findRes=$(find "${pkgFolder}" -name "*.cy.ts" || true)
if [[ -n "${findRes}" ]]; then
componentTestFilter="${componentTestFilter}${pkgFolder},"
componentTestMatrix="${componentTestMatrix}\"${pkgFolder}\","
fi
# we will keep the list of packages that do require unit tests based on the fact that we
# detecting UT files
findRes=$(find "${pkgFolder}" -name "*.spec.ts" || true)
if [[ -n "${findRes}" ]]; then
unitTestFilter="${unitTestFilter}${pkgFolder}|"
fi
done
# remove trailing '|' from pnpm filter
filter="{("$(echo "${filter}" | sed 's/|$//')")}"
if [[ "${filter}" == "{()}" ]]; then
filter=""
fi
echo "filter=${filter}"
unitTestFilter="{("$(echo "${unitTestFilter}" | sed 's/|$//')")}"
if [[ "${{steps.run-tests.outputs.run-tests}}" != "true" || "${unitTestFilter}" == "{()}" ]]; then
unitTestFilter=""
fi
echo "unitTestFilter=${unitTestFilter}"
unitTestMatrix=$(echo "${unitTestFilter}"| sed 's/{(/\[\"/'| sed 's/)}/\"\]'/|sed 's/|/\"\,\"/g'| sed 's/packages\///g')
echo "unitTestMatrix=${unitTestMatrix}"
# remove trailing ',' from cypress spec filter
componentTestFilter=$(echo "${componentTestFilter}" | sed 's/,$//')
if [[ "${{steps.run-tests.outputs.run-tests}}" != "true" ]]; then
componentTestFilter=""
fi
echo "componentTestFilter=${componentTestFilter}"
# remove trailing ',' from cypress spec matrix
componentTestMatrix="[$(echo "${componentTestMatrix}" | sed 's/,$//')]"
if [[ "${{steps.run-tests.outputs.run-tests}}" != "true" || "${componentTestFilter}" == "" ]]; then
componentTestMatrix=""
fi
echo "componentTestMatrix=${componentTestMatrix}"
# this is to pass to `pnpm --filter` to run pnpm command on all changed packages
echo "filter=${filter}" >> $GITHUB_OUTPUT
# this is changed packages that do have unit tests - to be passed to `pnpm test:unit`
echo "unitTestFilter=${unitTestFilter}" >> $GITHUB_OUTPUT
# this is unit tests in the format json.parse understands
echo "unitTestMatrix=${unitTestMatrix}" >> $GITHUB_OUTPUT
# this is changed packages that do have component tests - to be passed to `pnpm test:component`
echo "unitTestFilter=${unitTestFilter}" >> $GITHUB_OUTPUT
# this is component tests in the format json.parse understands
echo "componentTestMatrix=${componentTestMatrix}" >> $GITHUB_OUTPUT
- name: Stylelint Packages
if: ${{ steps.changed_packages.outputs.filter != '' }}
run: pnpm --parallel --stream --filter "${{steps.changed_packages.outputs.filter}}" run stylelint
- name: Lint Packages
if: ${{ steps.changed_packages.outputs.filter != '' }}
run: pnpm --parallel --stream --filter "${{steps.changed_packages.outputs.filter}}" run lint
- name: Build Packages
if: ${{ steps.changed_packages.outputs.filter != '' }}
# The `...` syntax tells pnpm to include dependent packages
run: pnpm --stream --filter "...${{steps.changed_packages.outputs.filter}}..." run build
- name: Check dist bundle size against max-limit
uses: Kong/github-action-dist-size-checker@main
- name: Publish Previews
if: ${{ github.event_name == 'pull_request' && steps.changed_packages.outputs.filter != '' && (github.actor != 'renovate[bot]' || contains(github.event.pull_request.labels.*.name, 'create preview package')) && env.GH_TOKEN != '' }}
run: |
git config user.email "[email protected]"
git config user.name "Kong UI Bot"
preid="pr.${{ github.event.pull_request.number }}.$(git rev-parse --short ${{ github.event.pull_request.head.sha }})"
tag="pr-${{ github.event.pull_request.number }}"
echo "preid=${preid}"
git checkout ${{ github.head_ref }}
pnpm --silent lerna version prerelease --preid ${preid} --allow-branch ${{ github.head_ref }} --conventional-prerelease --yes --amend || true
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > .npmrc
pnpmPublish=$(pnpm --stream --filter "${{steps.changed_packages.outputs.filter}}" publish --no-git-checks --access public --report-summary --tag "${tag}")
if [[ ! -z $(echo "${pnpmPublish}"|grep "There are no new packages that should be published") ]]; then
echo "There are no new packages that should be published"
exit 0
fi
npm_instructions=""
for pkg in $(echo "${pnpmPublish}" | grep "+ "| sed 's/+ //')
do
pkg="@$(echo ${pkg}|cut -d'@' -f2)@${tag}"
if [ "${npm_instructions}" != "" ]; then
npm_instructions="${npm_instructions}\n"
fi
npm_instructions="${npm_instructions}${pkg}"
done
if [[ -z "${npm_instructions}" ]]; then
echo "Error creating preview instructions"
exit -1
fi
echo "npm_instructions<<EOF" >> $GITHUB_ENV
echo -e "$npm_instructions" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_PUBLIC_PUBLISH }}
- name: Provide preview link info
if: ${{ env.npm_instructions != '' && env.GH_TOKEN != '' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
header: pr_preview_consumption
message: |
## ***Preview*** components from this PR in consuming application
In consuming application project install preview versions of shared packages generated by this PR:
```
${{env.npm_instructions}}
```
GITHUB_TOKEN: ${{ env.GH_TOKEN }}
- name: Run Unit Tests
if: ${{ steps.changed_packages.outputs.unitTestFilter != '' }}
run: pnpm --parallel --stream --filter "${{steps.changed_packages.outputs.unitTestFilter}}" run test:unit
- name: Save Build Artifacts
if: ${{ steps.changed_packages.outputs.filter != '' }}
uses: actions/upload-artifact@v4
with:
name: package-dist-artifacts
path: packages/*/*/dist
component-tests:
name: Component tests
needs: [build]
runs-on: ubuntu-latest
if: ${{ needs.build.outputs.componentTestMatrix != ''}}
timeout-minutes: 20
continue-on-error: false
strategy:
fail-fast: false
matrix:
container: ${{ fromJSON(needs.build.outputs.componentTestMatrix) }}
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
- name: Setup PNPM with Dependencies
uses: ./.github/actions/setup-pnpm-with-dependencies/
- name: Prepare Cypress
run: pnpm cypress install
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: package-dist-artifacts
path: packages/
- name: Run Component Tests
uses: cypress-io/github-action@v4
with:
install: false
command: pnpm run test:component:ci --spec ${{ matrix.container }}
- name: Normalize test results artifact name
continue-on-error: true
if: failure()
id: normalize-name
shell: bash
run: |
packageName=$(echo ${{ matrix.container }} | sed 's/\//-/g')
echo "artifactName=component-test-failures-${packageName}" >> $GITHUB_OUTPUT
- name: Upload Component Test Results
uses: actions/upload-artifact@v4
continue-on-error: true
if: failure()
with:
name: ${{ steps.normalize-name.outputs.artifactName }}
path: |
cypress/videos/
cypress/screenshots/
finish-test-and-publish:
name: Collect Test Results And Publish
needs: [build, component-tests]
runs-on: ubuntu-latest
continue-on-error: true
timeout-minutes: 20
if: ${{ always() && needs.build.outputs.filter != '' }}
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GH_TOKEN: ${{ secrets.KONGPONENTS_BOT_PAT }}
steps:
- name: Successful tests
if: ${{ !(contains(needs.*.result, 'failure')) }}
run: exit 0
- name: Failing tests
if: ${{ contains(needs.*.result, 'failure') }}
run: exit 1
- name: Checkout Source Code
if: ${{ github.event_name == 'push' }}
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.KONGPONENTS_BOT_PAT }}
- name: Setup PNPM with Dependencies
if: ${{ github.event_name == 'push' }}
uses: ./.github/actions/setup-pnpm-with-dependencies/
- name: Download build artifacts
if: ${{ github.event_name == 'push' }}
uses: actions/download-artifact@v4
with:
name: package-dist-artifacts
path: packages/
# for lerna version we use:
# --force-git-tag - if tag was already created it will be recreated instead of command failing and entire CI failing
# --yes - so CI doesn't wait for the confirmation keyboard import
# --create-release - so that release is created in github when version is changed
# --conventional-commits - so that package's version changed following semantic releases
#
# we shall ignore lerna version error as pnpm publish will fail or not publish in case of such error
- name: Update package versions
if: ${{ github.event_name == 'push' }}
run: |
git config user.email "[email protected]"
git config user.name "Kong UI Bot"
pnpm --silent lerna version --conventional-commits --create-release github --yes --force-git-tag || true
- name: Publish packages to NPM
if: ${{ github.event_name == 'push' }}
run: |
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > .npmrc
pnpm --stream --filter "${{needs.build.outputs.filter}}" publish --no-git-checks --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_PUBLIC_PUBLISH }}
notify-slack:
name: Slack Notification
if: ${{ always() && github.event_name == 'push' }}
runs-on: ubuntu-latest
timeout-minutes: 20
needs:
- finish-test-and-publish
steps:
- name: Checkout Source Code
uses: actions/checkout@v4
- name: Get aggregate Workflow status
uses: technote-space/workflow-conclusion-action@v3
- name: Post Slack notification
uses: Kong/public-shared-actions/slack-actions/workflow-notification@v2
with:
slack-webhook-url: ${{ env.WORKFLOW_CONCLUSION == 'failure' && secrets.SLACK_WEBHOOK_URL_ALERT || secrets.SLACK_WEBHOOK_URL_NOTIFY }}
status: ${{ env.WORKFLOW_CONCLUSION == 'failure' && 'failure' || 'success' }}