|
| 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 }} |
0 commit comments