Skip to content

Commit 4e055c8

Browse files
committed
feat: add smart get-node-version
1 parent 5922921 commit 4e055c8

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

.github/actions/build-node-python/action.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,18 @@ runs:
9595
using: "composite"
9696
steps:
9797
# Node
98+
- uses: ./tmp/github-workflows/.github/actions/get-node-version
99+
if: inputs.enable_node == 'true'
100+
id: get-node-version
101+
98102
- name: Set up node version
99-
uses: actions/setup-node@v4
103+
uses: actions/setup-node@v5
100104
if: inputs.enable_node == 'true'
101105
with:
102-
node-version: ${{ inputs.node_version }}
106+
# TODO: Revert the two
107+
node-version: ${{ fromJson(steps.get-node-version.outputs.node_version) || inputs.node_version }}
103108
registry-url: ${{ inputs.npm_registry }}
109+
package-manager-cache: false
104110
- name: Show node and npm version
105111
if: inputs.enable_node == 'true'
106112
run: |
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: get-node-version
2+
description: extracts the node version from the package.json engines
3+
author: datavisyn
4+
5+
outputs:
6+
node_version:
7+
description: "extracted node version"
8+
value: ${{ steps.get-node-version.outputs.result }}
9+
10+
runs:
11+
using: "composite"
12+
steps:
13+
- name: Setup sandboxed dependencies
14+
shell: bash
15+
run: |
16+
npm init -y --prefix ${{ github.action_path }} >/dev/null 2>&1
17+
npm install semver --prefix ${{ github.action_path }} >/dev/null 2>&1
18+
19+
- name: Find version script
20+
id: get-node-version
21+
uses: actions/github-script@v7
22+
with:
23+
script: |
24+
const path = require('path');
25+
const fs = require('fs');
26+
// Load the sandboxed semver dependency from the action's own node_modules
27+
const semver = require(path.join(process.env.GITHUB_ACTION_PATH, 'node_modules/semver'));
28+
29+
// Define the versions to check against the package.json engines field, in order of preference
30+
// See https://github.com/actions/node-versions/releases for available versions
31+
const versionsToCheck = ['24.9', '20.9'].map((v) => semver.minVersion(v).version);
32+
33+
const packageJsonPath = path.join(process.env.GITHUB_WORKSPACE, 'package.json');
34+
if (!fs.existsSync(packageJsonPath)) {
35+
core.setFailed(`Error: package.json not found at ${packageJsonPath}`);
36+
return;
37+
}
38+
39+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
40+
const requiredRange = packageJson.engines?.node;
41+
42+
if (!requiredRange) {
43+
core.info('No node engine requirement in package.json. Returning empty.');
44+
return '';
45+
}
46+
47+
core.info(`Required Node.js range: ${requiredRange}`);
48+
49+
for (const version of versionsToCheck) {
50+
if (semver.satisfies(version, requiredRange)) {
51+
core.info(`\nMatch found! Returning version ${version}.`);
52+
// Simply return the matching version string to set the 'result' output
53+
return version;
54+
}
55+
}
56+
57+
core.setFailed(`\nNo compatible Node.js version found in the list of versions "${versionsToCheck.join(', ')}" for range "${requiredRange}".`);

.github/workflows/build-node-python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ env:
119119
PYPI_REGISTRY: "https://upload.pypi.org/legacy/"
120120
PYPI_USERNAME: "datavisyn"
121121
PYTHON_VERSION: ${{ vars.PYTHON_VERSION || '3.10' }}
122-
WORKFLOW_BRANCH: "main"
122+
WORKFLOW_BRANCH: "mp/node_version"
123123
POSTGRES_HOSTNAME: postgres_${{ github.job }}_${{ inputs.deduplication_id }}_${{ github.run_id }}_${{ github.run_attempt }}
124124

125125
permissions:

0 commit comments

Comments
 (0)