Skip to content

Commit 387287a

Browse files
feat: add allow-alpha input for Node.js alpha releases (#1674)
1 parent 66f83c8 commit 387287a

9 files changed

Lines changed: 330 additions & 16 deletions

File tree

‎.github/workflows/versions.yml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,32 @@ jobs:
151151
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
152152
shell: bash
153153

154+
allow-alpha:
155+
runs-on: ${{ matrix.os }}
156+
strategy:
157+
fail-fast: false
158+
matrix:
159+
os: [ubuntu-latest, windows-latest, macos-latest]
160+
node-version: [27, 27.0.0]
161+
steps:
162+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
163+
- name: Setup Node with allow-alpha
164+
uses: ./
165+
with:
166+
node-version: ${{ matrix.node-version }}
167+
allow-alpha: true
168+
- name: Verify an alpha is installed
169+
run: |
170+
nodeVersion=$(node --version)
171+
if [[ "$nodeVersion" != *-alpha* ]]; then
172+
echo "Expected a Node.js alpha but found $nodeVersion. Move this job to the next alpha release line."
173+
exit 1
174+
fi
175+
shell: bash
176+
- name: Verify node and npm
177+
run: __tests__/verify-node.sh "${{ matrix.node-version }}"
178+
shell: bash
179+
154180
version-file:
155181
runs-on: ${{ matrix.os }}
156182
strategy:

‎README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ See [action.yml](action.yml)
5959
# Default: false
6060
check-latest: false
6161

62+
# Allow alpha releases when no stable release matches the version spec.
63+
# Default: false
64+
allow-alpha: false
65+
6266
# Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.
6367
# Default: ''. The action use system architecture by default
6468
architecture: ''
@@ -255,6 +259,7 @@ If the runner is not able to access github.com, any Nodejs versions requested du
255259
## Advanced usage
256260

257261
- [Check latest version](docs/advanced-usage.md#check-latest-version)
262+
- [Allow alpha releases](docs/advanced-usage.md#allow-alpha-releases)
258263
- [Using a node version file](docs/advanced-usage.md#node-version-file)
259264
- [Using different architectures](docs/advanced-usage.md#architecture)
260265
- [Using v8 canary versions](docs/advanced-usage.md#v8-canary-versions)

‎__tests__/official-installer.test.ts‎

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,160 @@ describe('setup-node', () => {
792792
}, 10000);
793793
});
794794

795+
describe('allow-alpha', () => {
796+
const olderAlpha = '27.0.0-alpha.0.0.9';
797+
const newestAlpha = '27.0.0-alpha.0.0.10';
798+
const files = ['linux-x64'];
799+
let releases: INodeVersion[];
800+
801+
function expectInstalled(version: string) {
802+
const toolPath = path.normalize(`/cache/node/${version}/${os.arch}`);
803+
cacheSpy.mockImplementation(async () => toolPath);
804+
getExecOutputSpy.mockImplementation(async () => ({
805+
stdout: `v${version}`,
806+
stderr: '',
807+
exitCode: 0
808+
}));
809+
return toolPath;
810+
}
811+
812+
beforeEach(() => {
813+
os.platform = 'linux';
814+
os.arch = 'x64';
815+
inputs['allow-alpha'] = 'true';
816+
inputs['node-version'] = '27';
817+
releases = [
818+
{version: `v${olderAlpha}`, files},
819+
{version: `v${newestAlpha}`, files},
820+
{version: 'v28.0.0-alpha.0.0.0', files},
821+
{version: 'v26.1.0', files}
822+
];
823+
getJsonSpy.mockImplementation(async () => ({result: releases}));
824+
findSpy.mockReturnValue('');
825+
dlSpy.mockImplementation(async () => '/some/temp/path');
826+
exSpy.mockImplementation(async () => '/some/extracted/path');
827+
});
828+
829+
it.each([
830+
'27',
831+
'27.x',
832+
'27.0',
833+
'27.0.0',
834+
'^27.0.0',
835+
'~27.0.0',
836+
'>=27.0.0 <28.0.0'
837+
])('selects the newest matching alpha for %s', async versionSpec => {
838+
inputs['node-version'] = versionSpec;
839+
expectInstalled(newestAlpha);
840+
841+
await main.run();
842+
843+
expect(setFailedSpy).not.toHaveBeenCalled();
844+
expect(dlSpy).toHaveBeenCalledWith(
845+
`https://nodejs.org/dist/v${newestAlpha}/node-v${newestAlpha}-linux-x64.tar.gz`,
846+
undefined,
847+
undefined
848+
);
849+
});
850+
851+
it.each(['27', '27.0.0'])(
852+
'prefers a stable release for %s over a newer alpha',
853+
async versionSpec => {
854+
inputs['node-version'] = versionSpec;
855+
releases.push({version: 'v27.0.0', files});
856+
expectInstalled('27.0.0');
857+
858+
await main.run();
859+
860+
expect(setFailedSpy).not.toHaveBeenCalled();
861+
expect(cacheSpy).toHaveBeenCalledWith(
862+
'/some/extracted/path',
863+
'node',
864+
'27.0.0',
865+
'x64'
866+
);
867+
}
868+
);
869+
870+
it.each([undefined, 'false'])(
871+
'does not select an alpha and suggests the input when allow-alpha is %s',
872+
async allowAlpha => {
873+
inputs['allow-alpha'] = allowAlpha;
874+
await main.run();
875+
expect(setFailedSpy).toHaveBeenCalledWith(
876+
"Unable to find Node version '27' for platform linux and architecture x64. A matching alpha release is available; set 'allow-alpha: true' to use it."
877+
);
878+
expect(dlSpy).not.toHaveBeenCalled();
879+
}
880+
);
881+
882+
it.each(['latest', 'current', 'node'])(
883+
'installs the newest stable release for %s',
884+
async versionSpec => {
885+
inputs['node-version'] = versionSpec;
886+
expectInstalled('26.1.0');
887+
888+
await main.run();
889+
890+
expect(setFailedSpy).not.toHaveBeenCalled();
891+
expect(cacheSpy).toHaveBeenCalledWith(
892+
'/some/extracted/path',
893+
'node',
894+
'26.1.0',
895+
'x64'
896+
);
897+
}
898+
);
899+
900+
it('does not suggest allow-alpha when no alpha matches', async () => {
901+
inputs['allow-alpha'] = 'false';
902+
inputs['node-version'] = '29';
903+
await main.run();
904+
expect(setFailedSpy).toHaveBeenCalledWith(
905+
"Unable to find Node version '29' for platform linux and architecture x64."
906+
);
907+
});
908+
909+
it('respects an explicit prerelease lower bound', async () => {
910+
inputs['node-version'] = '>=27.0.0-alpha.0.0.99';
911+
await main.run();
912+
expect(setFailedSpy).toHaveBeenCalledWith(
913+
"Unable to find Node version '>=27.0.0-alpha.0.0.99' for platform linux and architecture x64."
914+
);
915+
expect(dlSpy).not.toHaveBeenCalled();
916+
});
917+
918+
it('reuses a cached alpha instead of downloading it again', async () => {
919+
const toolPath = expectInstalled(newestAlpha);
920+
findSpy.mockImplementation((_tool, version) =>
921+
version === `v${newestAlpha}` ? toolPath : ''
922+
);
923+
924+
await main.run();
925+
926+
expect(setFailedSpy).not.toHaveBeenCalled();
927+
expect(dlSpy).not.toHaveBeenCalled();
928+
expect(addPathSpy).toHaveBeenCalledWith(path.join(toolPath, 'bin'));
929+
});
930+
931+
it('downloads a newer alpha when only an older alpha is cached', async () => {
932+
findSpy.mockImplementation((_tool, version) =>
933+
version === `v${olderAlpha}` ? `/cache/node/${olderAlpha}/x64` : ''
934+
);
935+
expectInstalled(newestAlpha);
936+
937+
await main.run();
938+
939+
expect(setFailedSpy).not.toHaveBeenCalled();
940+
expect(cacheSpy).toHaveBeenCalledWith(
941+
'/some/extracted/path',
942+
'node',
943+
newestAlpha,
944+
'x64'
945+
);
946+
});
947+
});
948+
795949
describe('LTS version', () => {
796950
useRetryTimers();
797951

‎action.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ inputs:
1111
check-latest:
1212
description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec.'
1313
default: false
14+
allow-alpha:
15+
description: 'Allow alpha releases when no stable release matches the version spec.'
16+
default: false
1417
registry-url:
1518
description: 'Optional registry to set up for auth. Will set the registry in a project level .npmrc and .yarnrc file, and set up auth to read in from env.NODE_AUTH_TOKEN.'
1619
scope:

‎dist/setup/index.js‎

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99075,7 +99075,7 @@ class OfficialBuilds extends BaseDistribution {
9907599075
core_info('Falling back to download directly from Node');
9907699076
}
9907799077
if (!toolPath) {
99078-
toolPath = await this.downloadDirectlyFromNode();
99078+
toolPath = await this.downloadDirectlyFromNode(nodeJsVersions);
9907999079
}
9908099080
const installedDir = toolPath;
9908199081
if (this.osPlat != 'win32') {
@@ -99090,12 +99090,24 @@ class OfficialBuilds extends BaseDistribution {
9909099090
}
9909199091
addPath(toolPath);
9909299092
}
99093-
async downloadDirectlyFromNode() {
99094-
const nodeJsVersions = await this.getNodeJsVersions();
99093+
async downloadDirectlyFromNode(nodeJsVersions) {
99094+
nodeJsVersions ??= await this.getNodeJsVersions();
9909599095
const versions = this.filterVersions(nodeJsVersions);
9909699096
const evaluatedVersion = this.evaluateVersions(versions);
9909799097
if (!evaluatedVersion) {
99098-
throw new Error(`Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`);
99098+
let message = `Unable to find Node version '${this.nodeInfo.versionSpec}' for platform ${this.osPlat} and architecture ${this.nodeInfo.arch}.`;
99099+
if (!this.nodeInfo.allowAlpha && this.findAlphaVersion(versions)) {
99100+
message += ` A matching alpha release is available; set 'allow-alpha: true' to use it.`;
99101+
}
99102+
throw new Error(message);
99103+
}
99104+
// The tool cache cannot match prereleases against a range such as `27`, so reuse a cached alpha here.
99105+
if (semver_default().prerelease(evaluatedVersion)) {
99106+
const cachedPath = find('node', evaluatedVersion, this.translateArchToDistUrl(this.nodeInfo.arch));
99107+
if (cachedPath) {
99108+
core_info(`Found in cache @ ${cachedPath}`);
99109+
return cachedPath;
99110+
}
9909999111
}
9910099112
const toolName = this.getNodejsDistInfo(evaluatedVersion);
9910199113
try {
@@ -99112,7 +99124,6 @@ class OfficialBuilds extends BaseDistribution {
9911299124
}
9911399125
}
9911499126
evaluateVersions(versions) {
99115-
let version = '';
9911699127
if (this.isLatestSyntax(this.nodeInfo.versionSpec)) {
9911799128
core_info(`getting latest node version...`);
9911899129
const stableVersion = versions.find(potential => semver_default().prerelease(potential) === null);
@@ -99121,8 +99132,37 @@ class OfficialBuilds extends BaseDistribution {
9912199132
}
9912299133
return stableVersion;
9912399134
}
99124-
version = super.evaluateVersions(versions);
99125-
return version;
99135+
const version = super.evaluateVersions(versions);
99136+
if (version || !this.nodeInfo.allowAlpha) {
99137+
return version;
99138+
}
99139+
// Only fall back to an alpha when no stable release matches the spec.
99140+
const alphaVersion = this.findAlphaVersion(versions);
99141+
if (alphaVersion) {
99142+
core_info(`No stable release matches '${this.nodeInfo.versionSpec}', using ${alphaVersion}`);
99143+
}
99144+
return alphaVersion;
99145+
}
99146+
// Alphas are matched by their release version, so `27.0.0` or `^27.0.0` select `27.0.0-alpha.*`.
99147+
// Ranges with explicit prerelease bounds are already handled by standard semver.
99148+
findAlphaVersion(versions) {
99149+
const { range } = this.validRange(this.nodeInfo.versionSpec);
99150+
if (!semver_default().validRange(range) || this.hasPrereleaseBound(range)) {
99151+
return '';
99152+
}
99153+
return (versions.find(potential => {
99154+
const parsed = semver_default().parse(potential);
99155+
return (parsed?.prerelease[0] === 'alpha' &&
99156+
semver_default().satisfies(`${parsed.major}.${parsed.minor}.${parsed.patch}`, range));
99157+
}) ?? '');
99158+
}
99159+
hasPrereleaseBound(range) {
99160+
// semver desugars `27`, `^27.0.0`, etc. into an upper bound such as `<28.0.0-0`; ignore that synthetic `-0`.
99161+
return new (semver_default()).Range(range).set.some(comparators => comparators.some(({ operator, semver: bound }) => {
99162+
const prerelease = bound.prerelease ?? [];
99163+
const isSyntheticUpperBound = operator === '<' && prerelease.length === 1 && prerelease[0] === 0;
99164+
return prerelease.length > 0 && !isSyntheticUpperBound;
99165+
}));
9912699166
}
9912799167
getDistributionUrl(mirror) {
9912899168
const url = mirror || 'https://nodejs.org';
@@ -99329,9 +99369,11 @@ async function run() {
9932999369
const mirrorToken = getInput('mirror-token');
9933099370
const stable = (getInput('stable') || 'true').toUpperCase() === 'TRUE';
9933199371
const checkLatest = (getInput('check-latest') || 'false').toUpperCase() === 'TRUE';
99372+
const allowAlpha = (getInput('allow-alpha') || 'false').toUpperCase() === 'TRUE';
9933299373
const nodejsInfo = {
9933399374
versionSpec: version,
9933499375
checkLatest,
99376+
allowAlpha,
9933599377
auth,
9933699378
stable,
9933799379
arch,

‎docs/advanced-usage.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@ steps:
7474
- run: npm test
7575
```
7676

77+
## Allow alpha releases
78+
79+
The `allow-alpha` input defaults to `false`. Set it to `true` to use an alpha release when no stable release matches the requested version.
80+
81+
Alphas are matched by their release version, so `27`, `27.x`, `27.0.0`, `^27.0.0`, and `~27.0.0` all select the newest Node.js 27 alpha. Ranges with an explicit prerelease bound, such as `>=27.0.0-alpha.3`, use standard semver matching.
82+
83+
Alpha builds are not published for every platform. For example, Node.js 27 alphas are not available for macOS x64, so setup fails on those runners.
84+
85+
```yaml
86+
steps:
87+
- uses: actions/checkout@v7
88+
- uses: actions/setup-node@v7
89+
with:
90+
node-version: '27'
91+
allow-alpha: true
92+
package-manager-cache: false # Disable automatic npm caching if not required
93+
- run: npm ci
94+
- run: npm test
95+
```
96+
97+
Exact alpha versions such as `27.0.0-alpha.0.0.0` work without `allow-alpha`.
98+
7799
## Node version file
78100

79101
The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc`, `.node-version`, `.tool-versions`, `mise.toml`, or `package.json`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used.

‎src/distributions/base-models.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface NodeInputs {
33
arch: string;
44
auth?: string;
55
checkLatest: boolean;
6+
allowAlpha: boolean;
67
stable: boolean;
78
mirror: string;
89
mirrorToken: string;

0 commit comments

Comments
 (0)