Skip to content

Commit

Permalink
Support node-version-file input (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Apr 25, 2024
1 parent 731d54a commit 66aadfe
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ jobs:

test-default:
uses: ./.github/workflows/test.yml

test-node-version-file:
uses: ./.github/workflows/test.yml
with:
node-version-file: .node-version
57 changes: 51 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ on:
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
Expand All @@ -25,15 +30,34 @@ on:
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:
name: Test on Node.js ${{ matrix.node-version }} and ${{ matrix.os }}
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.saobby.my.eu.orgmunity/t/reusable-workflow-with-strategy-matrix/205676/2
node-version: ${{ fromJson(inputs.node-version) }}
node-version: ${{ fromJson(needs.prepare.outputs.node-version) }}
os: ${{ fromJson(inputs.os) }}
exclude: ${{ fromJson(inputs.exclude) }}

Expand All @@ -49,19 +73,40 @@ jobs:
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(matrix.node-version, '16') }}
run: npm install --global npm@9 && npm --version
if: ${{ startsWith(steps.node-version.outputs.value, 'v16') }}
run: |
npm install --global npm@9
echo "npm: $(npm --version)"
- name: Install latest npm
if: ${{ !startsWith(matrix.node-version, '16') }}
run: npm install --global npm@latest && npm --version
if: ${{ !startsWith(steps.node-version.outputs.value, 'v16') }}
run: |
npm install --global npm@latest
echo "npm: $(npm --version)"
- name: Install dependencies
run: npm ci
Expand Down
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20

0 comments on commit 66aadfe

Please sign in to comment.