Skip to content

Commit 0a41a3e

Browse files
ci: optimize ci speed (#461)
1 parent 22d1a8b commit 0a41a3e

File tree

2 files changed

+85
-30
lines changed

2 files changed

+85
-30
lines changed

.github/workflows/build-and-test.yml

Lines changed: 84 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,34 @@ on:
55
branches:
66
- main
77
jobs:
8-
build:
8+
npm-install-if-needed:
99
runs-on: ubuntu-20.04
1010
steps:
1111
- name: Check out code
1212
uses: actions/checkout@v2
1313

14-
- name: Cache node modules
14+
- id: cache
15+
name: Cache node modules
1516
uses: actions/cache@v2
1617
with:
17-
path: ~/.npm
18-
key: ${{ runner.os }}-node-${{ hashFiles('package.json') }}-${{ hashFiles('package-lock.json') }}
19-
restore-keys: |
20-
${{ runner.os }}-node-${{ hashFiles('package.json') }}-
21-
${{ runner.os }}-node-
18+
path: node_modules
19+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
2220

2321
- name: NPM Install
22+
if: steps.cache.outputs.cache-hit != 'true'
2423
run: npm ci
24+
build:
25+
runs-on: ubuntu-20.04
26+
needs: [npm-install-if-needed]
27+
steps:
28+
- name: Check out code
29+
uses: actions/checkout@v2
30+
31+
- name: Cache node modules
32+
uses: actions/cache@v2
33+
with:
34+
path: node_modules
35+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
2536

2637
- name: Build
2738
run: npm run build:ci
@@ -33,28 +44,57 @@ jobs:
3344
path: dist
3445
lint:
3546
runs-on: ubuntu-20.04
47+
needs: [npm-install-if-needed]
3648
steps:
3749
- name: Check out code
3850
uses: actions/checkout@v2
3951

40-
- name: Cache node modules
52+
- name: Restore node modules
4153
uses: actions/cache@v2
4254
with:
43-
path: ~/.npm
44-
key: ${{ runner.os }}-node-${{ hashFiles('package.json') }}-${{ hashFiles('package-lock.json') }}
45-
restore-keys: |
46-
${{ runner.os }}-node-${{ hashFiles('package.json') }}-
47-
${{ runner.os }}-node-
48-
- name: NPM Install
49-
run: npm ci
55+
path: node_modules
56+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
5057

5158
- name: Lint
5259
run: npm run lint
5360

5461
- name: Prettier Check
5562
run: npm run prettier:check
63+
setup-test-batches:
64+
runs-on: ubuntu-20.04
65+
needs: [npm-install-if-needed]
66+
env:
67+
PARALLELISM: 4
68+
outputs:
69+
test-chunks: ${{ steps['set-test-chunks'].outputs['test-chunks'] }}
70+
test-chunk-ids: ${{ steps['set-test-chunk-ids'].outputs['test-chunk-ids'] }}
71+
steps:
72+
- name: Check out code
73+
uses: actions/checkout@v2
74+
75+
- name: Cache node modules
76+
uses: actions/cache@v2
77+
with:
78+
path: node_modules
79+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
80+
81+
- id: set-test-chunks
82+
name: Set Chunks
83+
run: echo "::set-output name=test-chunks::$(npx jest --listTests --json | jq -cM "[_nwise(length / $PARALLELISM | ceil)]")"
84+
85+
- id: set-test-chunk-ids
86+
name: Set Chunk IDs
87+
run: echo "::set-output name=test-chunk-ids::$(echo $CHUNKS | jq -cM 'to_entries | map(.key)')"
88+
env:
89+
CHUNKS: ${{ steps['set-test-chunks'].outputs['test-chunks'] }}
5690
test:
91+
name: test (chunk ${{ matrix.chunk }})
5792
runs-on: ubuntu-20.04
93+
needs: [npm-install-if-needed, setup-test-batches]
94+
strategy:
95+
fail-fast: false
96+
matrix:
97+
chunk: ${{ fromJson(needs['setup-test-batches'].outputs['test-chunk-ids']) }}
5898
steps:
5999
- name: Check out code
60100
uses: actions/checkout@v2
@@ -65,44 +105,59 @@ jobs:
65105
- name: Cache node modules
66106
uses: actions/cache@v2
67107
with:
68-
path: ~/.npm
69-
key: ${{ runner.os }}-node-${{ hashFiles('package.json') }}-${{ hashFiles('package-lock.json') }}
70-
restore-keys: |
71-
${{ runner.os }}-node-${{ hashFiles('package.json') }}-
72-
${{ runner.os }}-node-
73-
74-
- name: NPM Install
75-
run: npm ci
108+
path: node_modules
109+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
76110

77111
- name: Test
78-
run: npm run test:ci
112+
run: npm run test:ci -- $(echo $CHUNKS | jq -r '.[${{ matrix.chunk }}] | .[] | "--testPathPattern "+.')
113+
env:
114+
CHUNKS: ${{ needs['setup-test-batches'].outputs['test-chunks'] }}
79115

80116
- name: Upload coverage to Codecov
81117
uses: codecov/codecov-action@v1
82118
with:
83119
fail_ci_if_error: true
84120

85-
- name: Publish Unit Test Results
86-
uses: docker://ghcr.io/enricomi/publish-unit-test-result-action:v1.6
121+
- name: Upload Unit Test Results
87122
if: always()
123+
uses: actions/upload-artifact@v2
88124
with:
89-
github_token: ${{ secrets.GITHUB_TOKEN }}
90-
files: test-results/**/*.xml
125+
name: Unit Test Results (chunk ${{ matrix.chunk }})
126+
path: test-results/**/*.xml
91127
validate-helm-charts:
92128
runs-on: ubuntu-20.04
93129
steps:
94130
- name: Check out code
95131
uses: actions/checkout@v2
132+
96133
- name: Set up Helm
97134
uses: azure/setup-helm@v1
98135
with:
99136
version: v3.3.0
137+
100138
- name: validate charts
101139
uses: hypertrace/actions/validate-charts@main
140+
publish-test-results:
141+
name: "Publish Unit Tests Results"
142+
needs: test
143+
runs-on: ubuntu-20.04
144+
# Only run if prereq jobs completed - successfully or not
145+
if: success() || failure()
146+
steps:
147+
- name: Download Artifacts
148+
uses: actions/download-artifact@v2
149+
with:
150+
path: artifacts
151+
152+
- name: Publish Unit Test Results
153+
uses: docker://ghcr.io/enricomi/publish-unit-test-result-action:v1.6
154+
with:
155+
github_token: ${{ secrets.GITHUB_TOKEN }}
156+
files: artifacts/**/*.xml
102157
merge-publish:
103158
if: github.event_name == 'push'
104159
runs-on: ubuntu-20.04
105-
needs: [build, lint, test, validate-helm-charts]
160+
needs: [npm-install-if-needed, build, lint, test, validate-helm-charts]
106161
steps:
107162
- name: Check out code
108163
uses: actions/checkout@v2

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"lint": "ng lint hypertrace-ui",
2020
"lint:fix": "ng lint --fix hypertrace-ui",
2121
"prettier:check": "prettier --check '**'",
22-
"test:ci": "ng test hypertrace-ui -i --ci --coverage",
22+
"test:ci": "ng test hypertrace-ui --maxWorkers=2 --ci --coverage",
2323
"docs": "compodoc -p tsconfig.json"
2424
},
2525
"private": true,

0 commit comments

Comments
 (0)