Skip to content

Commit 23fcbc3

Browse files
authored
feat: support parallel tests (#12)
1 parent 6014352 commit 23fcbc3

File tree

5 files changed

+176
-3
lines changed

5 files changed

+176
-3
lines changed

.github/workflows/node-test-mysql.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
- 6379:6379
104104

105105
concurrency:
106-
group: ${{ github.workflow }}-#${{ github.event.pull_request.number || github.ref }}-(${{ matrix.os }}, ${{ matrix.version }}, ${{ matrix.mysql_version }})
106+
group: ${{ github.workflow }}-#${{ github.event.pull_request.number || github.ref }}-(${{ matrix.os }}, ${{ matrix.version }}, mysql@${{ matrix.mysql_version }})
107107
cancel-in-progress: true
108108

109109
steps:
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
name: Node.js Unit Test in Parallel
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
CODECOV_TOKEN:
7+
description: 'codecov token'
8+
required: false
9+
10+
inputs:
11+
os:
12+
type: string
13+
description: 'Operator System, such as: ubuntu-latest, macos-latest'
14+
default: 'ubuntu-latest, macos-latest, windows-latest'
15+
16+
version:
17+
type: string
18+
description: 'Node.js Version, such as 18, 20, 22'
19+
default: '18, 20, 22'
20+
21+
install:
22+
type: string
23+
description: 'Install dependencies script'
24+
default: 'npm i --no-package-lock --no-fund'
25+
26+
test:
27+
type: string
28+
description: 'test script, such as: npm test, npm run ci'
29+
default: 'npm run ci'
30+
31+
action_ref:
32+
type: string
33+
description: 'Branch name for node-modules/github-actions, for test purpose'
34+
default: master
35+
36+
parallel:
37+
type: number
38+
description: 'Number of parallel test jobs'
39+
default: 3
40+
41+
jobs:
42+
Setup:
43+
runs-on: ubuntu-latest
44+
outputs:
45+
os: ${{ steps.handler.outputs.os }}
46+
version: ${{ steps.handler.outputs.version }}
47+
node_index: ${{ steps.handler.outputs.node_index }}
48+
total_nodes: ${{ steps.handler.outputs.total_nodes }}
49+
50+
steps:
51+
# Checkout action repository
52+
- name: Checkout
53+
uses: actions/checkout@v4
54+
with:
55+
repository: node-modules/github-actions
56+
path: action_repo
57+
ref: ${{ inputs.action_ref }}
58+
59+
# Setup Node.js environment
60+
- name: Setup Node.js
61+
uses: actions/setup-node@v4
62+
63+
# Install dependencies
64+
- name: Install dependencies
65+
run: npm i --no-package-lock --no-fund
66+
working-directory: action_repo/scripts/test
67+
68+
# Normalize inputs style
69+
- name: Convert Inputs to Matrix
70+
id: handler
71+
run: node action_repo/scripts/test/index.js
72+
env:
73+
INPUT_OS: ${{ inputs.os }}
74+
INPUT_VERSION: ${{ inputs.version }}
75+
INPUT_PARALLEL: ${{ inputs.parallel }}
76+
77+
Test:
78+
needs: Setup
79+
strategy:
80+
fail-fast: false
81+
matrix:
82+
os: ${{ fromJSON(needs.setup.outputs.os) }}
83+
version: ${{ fromJSON(needs.setup.outputs.version) }}
84+
node_index: ${{ fromJSON(needs.setup.outputs.node_index) }}
85+
total_nodes: ${{ fromJSON(needs.setup.outputs.total_nodes) }}
86+
87+
name: Test (${{ matrix.os }}, ${{ matrix.version }}, ${{ matrix.node_index }}:${{ matrix.total_nodes }})
88+
runs-on: ${{ matrix.os }}
89+
90+
concurrency:
91+
group: ${{ github.workflow }}-#${{ github.event.pull_request.number || github.ref }}-(${{ matrix.os }}, ${{ matrix.version }}, ${{ matrix.node_index }} / ${{ matrix.total_nodes }})
92+
cancel-in-progress: true
93+
94+
steps:
95+
- name: Checkout Git Source
96+
uses: actions/checkout@v4
97+
98+
- name: Calculate Architecture
99+
uses: actions/github-script@v7
100+
id: calculate_architecture
101+
with:
102+
result-encoding: string
103+
script: |
104+
const osVersion = '${{ matrix.os }}';
105+
const isMacOS = osVersion === 'macos-latest' || osVersion.startsWith('macos');
106+
const nodeVersion = parseInt('${{ matrix.version }}'.split('.')[0]);
107+
if (isMacOS && nodeVersion <= 14) {
108+
return 'x64';
109+
} else {
110+
return '';
111+
}
112+
113+
- name: Use Node.js ${{ matrix.version }}
114+
uses: actions/setup-node@v4
115+
with:
116+
node-version: ${{ matrix.version }}
117+
architecture: ${{ steps.calculate_architecture.outputs.result }}
118+
check-latest: true
119+
120+
- name: Install Dependencies
121+
run: ${{ inputs.install }}
122+
env:
123+
CI_NODE_INDEX: ${{ matrix.node_index }}
124+
CI_NODE_TOTAL: ${{ matrix.total_nodes }}
125+
126+
- name: Run Lint
127+
run: npm run lint --if-present
128+
env:
129+
CI_NODE_INDEX: ${{ matrix.node_index }}
130+
CI_NODE_TOTAL: ${{ matrix.total_nodes }}
131+
132+
- name: Run Test
133+
run: ${{ inputs.test }}
134+
env:
135+
CI_NODE_INDEX: ${{ matrix.node_index }}
136+
CI_NODE_TOTAL: ${{ matrix.total_nodes }}
137+
138+
- name: Code Coverage
139+
uses: codecov/codecov-action@v3
140+
with:
141+
token: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/node-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
runs-on: ${{ matrix.os }}
7979

8080
concurrency:
81-
group: ${{ github.workflow }}-#${{ github.event.pull_request.number || github.ref }}-(${{ matrix.os }}, mysql@${{ matrix.version }})
81+
group: ${{ github.workflow }}-#${{ github.event.pull_request.number || github.ref }}-(${{ matrix.os }}, ${{ matrix.version }})
8282
cancel-in-progress: true
8383

8484
steps:

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,29 @@ jobs:
7373
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
7474
```
7575

76+
### 开启并行测试
77+
78+
```yaml
79+
name: CI
80+
81+
on:
82+
push:
83+
branches: [ master ]
84+
pull_request:
85+
branches: [ master ]
86+
87+
jobs:
88+
Job:
89+
name: Node.js
90+
uses: node-modules/github-actions/.github/workflows/node-test-parallel.yml@master
91+
# with:
92+
# os: 'ubuntu-latest'
93+
# version: '18, 20, 22'
94+
# parallel: 3
95+
# secrets:
96+
# CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
97+
```
98+
7699
## 发布 NPM 包
77100

78101
使用 [semantic-release](https://semantic-release.gitbook.io/) 自动发布 NPM 包。

scripts/test/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ const version = core.getInput('version').split(',').map(x => x.trim());
55
const mysqlVersionInput = core.getInput('mysql_version') || '5';
66
const mysqlVersion = mysqlVersionInput.split(',').map(x => x.trim());
77

8+
const parallel = parseInt(core.getInput('parallel')) || 3;
9+
const node_index = [];
10+
const total_nodes = [ parallel ];
11+
for (let i = 0; i < parallel; i++) {
12+
node_index.push(i);
13+
}
14+
815
core.setOutput('os', JSON.stringify(os));
916
core.setOutput('version', JSON.stringify(version));
1017
core.setOutput('mysql_version', JSON.stringify(mysqlVersion === '' ? [] : mysqlVersion));
18+
core.setOutput('node_index', JSON.stringify(node_index));
19+
core.setOutput('total_nodes', JSON.stringify(total_nodes));
1120

12-
core.info(`os: ${os}, version: ${version}, mysql_version: ${mysqlVersion}`);
21+
core.info(`[debug] os: ${JSON.stringify(os)}, version: ${JSON.stringify(version)}, mysql_version: ${JSON.stringify(mysqlVersion)}, parallel: ${parallel}, node_index: ${JSON.stringify(node_index)}, total_nodes: ${JSON.stringify(total_nodes)}`);

0 commit comments

Comments
 (0)