diff --git a/README.md b/README.md index e4158422..c3e63858 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ See [action.yml](action.yml) # Examples: 12.x, 10.15.1, >=10.15.0, lts/Hydrogen, 16-nightly, latest, node node-version: '' - # File containing the version Spec of the version to use. Examples: package.json, .nvmrc, .node-version, .tool-versions. + # File containing the version Spec of the version to use. Examples: package.json, .nvmrc, .node-version, .tool-versions, action.yml. # If node-version and node-version-file are both provided the action will use version from node-version. node-version-file: '' diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 382d8b19..c1f52b72 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -197,6 +197,7 @@ describe('main tests', () => { ${'{"engines": {"node": "17.0.0"}}'} | ${'17.0.0'} ${'{"devEngines": {"runtime": {"name": "node", "version": "22.0.0"}}}'} | ${'22.0.0'} ${'{"devEngines": {"runtime": [{"name": "bun"}, {"name": "node", "version": "22.0.0"}]}}'} | ${'22.0.0'} + ${"name: 'My Action'\nruns:\n using: 'node24'\n main: 'dist/index.js'\n"} | ${'24'} `.it('parses "$contents"', ({contents, expected}: any) => { const existsSpy = jest.spyOn(fs, 'existsSync'); existsSpy.mockImplementation(() => true); diff --git a/action.yml b/action.yml index dc1cefd5..ae8b3b45 100644 --- a/action.yml +++ b/action.yml @@ -5,7 +5,7 @@ inputs: node-version: description: 'Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0.' node-version-file: - description: 'File containing the version Spec of the version to use. Examples: package.json, .nvmrc, .node-version, .tool-versions.' + description: 'File containing the version Spec of the version to use. Examples: package.json, .nvmrc, .node-version, .tool-versions, action.yml.' architecture: description: 'Target architecture for Node to use. Examples: x86, x64. Will use system architecture by default.' check-latest: diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 2010c595..185fa744 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -76,7 +76,7 @@ steps: ## Node version file -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`, or `package.json`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used. +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`, `package.json`, or an action metadata file such as `action.yml`. For action metadata, the action reads the Node.js version from `runs.using` (for example, `node24`). If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used. See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax). > The action will search for the node version file relative to the repository root. diff --git a/src/util.ts b/src/util.ts index 1663a406..1f5234e8 100644 --- a/src/util.ts +++ b/src/util.ts @@ -68,6 +68,16 @@ export function getNodeVersionFromFile(versionFilePath: string): string | null { core.info('Node version file is not JSON file'); } + const runsSection = contents.match( + /^runs\s*:\s*$(?[\s\S]*?)(?=^[^\s].*?:|\s*$)/m + )?.groups?.contents; + const actionRuntime = runsSection?.match( + /^\s+using:\s*['"]?node(?\d+)['"]?\s*(?:#.*)?$/m + ); + if (actionRuntime?.groups?.version) { + return actionRuntime.groups.version; + } + const found = contents.match(/^(?:node(js)?\s+)?v?(?[^\s]+)$/m); return found?.groups?.version ?? contents.trim(); }