121 lines (104 loc) · 3.58 KB
/
test.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
name: Test
on:
workflow_call:
inputs:
node-version:
description: Node.js version
required: false
default: '["18", "20", "22"]'
type: string
node-version-file:
description: Node.js version file
required: false
default: ''
type: string
os:
description: OS name
required: false
default: '["ubuntu-latest"]'
type: string
exclude:
description: Exclude from matrix
required: false
default: '[]'
type: string
test-options:
description: Options to pass the test command
required: false
default: ''
type: string
jobs:
# NOTE: Prevent an empty matrix if `node-version-file` is specified
prepare:
runs-on: ubuntu-latest
outputs:
node-version: ${{ steps.init-node-version.outputs.node-version }}
steps:
- name: Initialize "node-version" parameter
id: init-node-version
run: |
if [[ -n "${node_version_file}" ]]; then
node_version='["(skipped)"]'
fi
echo "node-version=${node_version}" >> "${GITHUB_OUTPUT}"
env:
node_version: ${{ inputs.node-version }}
node_version_file: ${{ inputs.node-version-file }}
test:
needs: prepare
name: Test on Node.js ${{ inputs.node-version-file || matrix.node-version }} and ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
# NOTE: The `strategy` property is not fully supported, so it uses `fromJson()`.
# For details, see https://github.community/t/reusable-workflow-with-strategy-matrix/205676/2
node-version: ${{ fromJson(needs.prepare.outputs.node-version) }}
os: ${{ fromJson(inputs.os) }}
exclude: ${{ fromJson(inputs.exclude) }}
runs-on: ${{ matrix.os }}
permissions:
contents: read
timeout-minutes: 30
# HACK: The `npm` command fails on Node.js v22 and Windows OS for unknown error. So, this always uses Bash.
# See https://github.com/stylelint/.github/pull/35/commits/6f7dc8bd7b597902920c9b571f06fbcce69d0c72
defaults:
run:
shell: bash
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js ${{ matrix.node-version }}
if: ${{ !inputs.node-version-file }}
id: setup-node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- name: Set up Node.js from ${{ inputs.node-version-file }} file
if: ${{ inputs.node-version-file }}
id: setup-node-file
uses: actions/setup-node@v4
with:
node-version-file: ${{ inputs.node-version-file }}
cache: npm
- name: Detect Node.js version
id: node-version
shell: bash
run: echo "value=${node_version}" >> "${GITHUB_OUTPUT}"
env:
node_version: ${{ steps.setup-node-file.outputs.node-version || steps.setup-node.outputs.node-version }}
# NOTE: npm v10 has dropped support for Node.js v16.
- name: Install npm v9
if: ${{ startsWith(steps.node-version.outputs.value, 'v16') }}
run: |
npm install --global npm@9
echo "npm: $(npm --version)"
- name: Install latest npm
if: ${{ !startsWith(steps.node-version.outputs.value, 'v16') }}
run: |
npm install --global npm@latest
echo "npm: $(npm --version)"
- name: Install dependencies
run: npm ci
- name: Test
run: npm test --ignore-scripts -- ${{ inputs.test-options }}