From 1ae8f4b1fd89676f69b55d3dd6932b6df089ff7b Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Mon, 29 Jun 2020 21:56:37 +0300 Subject: [PATCH] Implement "check-latest" flag to check if pre-cached version is latest one (#165) --- .github/workflows/build-test.yml | 2 +- .github/workflows/versions.yml | 92 ++++++++++++++---- README.md | 14 +++ __tests__/installer.test.ts | 155 ++++++++++++++++++++++++++++++- action.yml | 3 + dist/index.js | 54 +++++++---- src/installer.ts | 57 +++++++++--- src/main.ts | 5 +- 8 files changed, 327 insertions(+), 55 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 5de6abfa..5d7c6c13 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -16,7 +16,7 @@ jobs: runs-on: ${{ matrix.operating-system }} strategy: matrix: - operating-system: [ubuntu-latest, windows-latest] + operating-system: [ubuntu-latest, windows-latest, macos-latest] steps: - uses: actions/checkout@v2 - name: Setup node 12 diff --git a/.github/workflows/versions.yml b/.github/workflows/versions.yml index 0042abe3..38017ef0 100644 --- a/.github/workflows/versions.yml +++ b/.github/workflows/versions.yml @@ -12,36 +12,88 @@ on: - '**.md' jobs: - versions: + local-cache: runs-on: ${{ matrix.operating-system }} strategy: fail-fast: false matrix: - operating-system: [ubuntu-latest, windows-latest] - defaults: - run: - shell: bash + operating-system: [ubuntu-latest, windows-latest, macos-latest] + node-version: [10, 12, 14] steps: - uses: actions/checkout@v2 - # test version that falls through to node dist - - name: Setup node 11 from dist + - name: Setup Node uses: ./ with: - node-version: 11 + node-version: ${{ matrix.node-version }} - name: Verify node and npm - run: __tests__/verify-node.sh 11 - # test old versions which didn't have npm and layout different + run: __tests__/verify-node.sh "${{ matrix.node-version }}" + shell: bash + + manifest: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macos-latest] + node-version: [10.15, 12.16.0, 14.2.0] + steps: + - uses: actions/checkout@v2 + - name: Setup Node + uses: ./ + with: + node-version: ${{ matrix.node-version }} + - name: Verify node and npm + run: __tests__/verify-node.sh "${{ matrix.node-version }}" + shell: bash + + check-latest: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macos-latest] + node-version: [10, 11, 12, 14] + steps: + - uses: actions/checkout@v2 + - name: Setup Node and check latest + uses: ./ + with: + node-version: ${{ matrix.node-version }} + check-latest: true + - name: Verify node and npm + run: __tests__/verify-node.sh "${{ matrix.node-version }}" + shell: bash + + node-dist: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macos-latest] + node-version: [11, 13] + steps: + - uses: actions/checkout@v2 + - name: Setup Node from dist + uses: ./ + with: + node-version: ${{ matrix.node-version }} + - name: Verify node and npm + run: __tests__/verify-node.sh "${{ matrix.node-version }}" + shell: bash + + old-versions: + runs-on: ${{ matrix.operating-system }} + strategy: + fail-fast: false + matrix: + operating-system: [ubuntu-latest, windows-latest, macos-latest] + steps: + - uses: actions/checkout@v2 + # test old versions which didn't have npm and layout different - name: Setup node 0.12.18 from dist - uses: ./ + uses: ./ with: node-version: 0.12.18 - name: Verify node - shell: bash - run: __tests__/verify-node.sh 0.12.18 SKIP_NPM - # test version from node manifest - - name: Setup node 12.16.2 from manifest - uses: ./ - with: - node-version: 12.16.2 - - name: Verify node and npm - run: __tests__/verify-node.sh 12 + run: __tests__/verify-node.sh 0.12.18 SKIP_NPM + shell: bash \ No newline at end of file diff --git a/README.md b/README.md index 0e8d6e7f..744c492f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,20 @@ steps: - run: npm test ``` +Check latest version: +> In basic example, without `check-latest` flag, the action tries to resolve version from local cache firstly and download only if it is not found. Local cache on image is updated with a couple of weeks latency. +`check-latest` flag forces the action to check if the cached version is the latest one. It reduces latency significantly but it is much more likely to incur version downloading. +```yaml +steps: +- uses: actions/checkout@v2 +- uses: actions/setup-node@v2 + with: + node-version: '12' + check-latest: true +- run: npm install +- run: npm test +``` + Matrix Testing: ```yaml jobs: diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 52b675e2..6f3a411e 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -8,6 +8,7 @@ import path from 'path'; import * as main from '../src/main'; import * as im from '../src/installer'; import * as auth from '../src/authutil'; +import {context} from '@actions/github'; let nodeTestManifest = require('./data/versions-manifest.json'); let nodeTestDist = require('./data/node-dist-index.json'); @@ -24,6 +25,7 @@ describe('setup-node', () => { let findSpy: jest.SpyInstance; let cnSpy: jest.SpyInstance; let logSpy: jest.SpyInstance; + let warningSpy: jest.SpyInstance; let getManifestSpy: jest.SpyInstance; let getDistSpy: jest.SpyInstance; let platSpy: jest.SpyInstance; @@ -77,8 +79,9 @@ describe('setup-node', () => { // writes cnSpy = jest.spyOn(process.stdout, 'write'); - logSpy = jest.spyOn(console, 'log'); + logSpy = jest.spyOn(core, 'info'); dbgSpy = jest.spyOn(core, 'debug'); + warningSpy = jest.spyOn(core, 'warning'); cnSpy.mockImplementation(line => { // uncomment to debug // process.stderr.write('write:' + line + '\n'); @@ -333,4 +336,154 @@ describe('setup-node', () => { expect(cnSpy).toHaveBeenCalledWith(`::error::${errMsg}${osm.EOL}`); }); + + describe('check-latest flag', () => { + it('use local version and dont check manifest if check-latest is not specified', async () => { + os.platform = 'linux'; + os.arch = 'x64'; + + inputs['node-version'] = '12'; + inputs['check-latest'] = 'false'; + + const toolPath = path.normalize('/cache/node/12.16.1/x64'); + findSpy.mockReturnValue(toolPath); + await main.run(); + + expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`); + expect(logSpy).not.toHaveBeenCalledWith( + 'Attempt to resolve the latest version from manifest...' + ); + }); + + it('check latest version and resolve it from local cache', async () => { + os.platform = 'linux'; + os.arch = 'x64'; + + inputs['node-version'] = '12'; + inputs['check-latest'] = 'true'; + + const toolPath = path.normalize('/cache/node/12.16.2/x64'); + findSpy.mockReturnValue(toolPath); + dlSpy.mockImplementation(async () => '/some/temp/path'); + exSpy.mockImplementation(async () => '/some/other/temp/path'); + cacheSpy.mockImplementation(async () => toolPath); + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith( + 'Attempt to resolve the latest version from manifest...' + ); + expect(logSpy).toHaveBeenCalledWith("Resolved as '12.16.2'"); + expect(logSpy).toHaveBeenCalledWith(`Found in cache @ ${toolPath}`); + }); + + it('check latest version and install it from manifest', async () => { + os.platform = 'linux'; + os.arch = 'x64'; + + inputs['node-version'] = '12'; + inputs['check-latest'] = 'true'; + + findSpy.mockImplementation(() => ''); + dlSpy.mockImplementation(async () => '/some/temp/path'); + const toolPath = path.normalize('/cache/node/12.16.2/x64'); + exSpy.mockImplementation(async () => '/some/other/temp/path'); + cacheSpy.mockImplementation(async () => toolPath); + const expectedUrl = + 'https://github.com/actions/node-versions/releases/download/12.16.2-20200423.28/node-12.16.2-linux-x64.tar.gz'; + + await main.run(); + + expect(logSpy).toHaveBeenCalledWith( + 'Attempt to resolve the latest version from manifest...' + ); + expect(logSpy).toHaveBeenCalledWith("Resolved as '12.16.2'"); + expect(logSpy).toHaveBeenCalledWith( + `Acquiring 12.16.2 from ${expectedUrl}` + ); + expect(logSpy).toHaveBeenCalledWith('Extracting ...'); + }); + + it('fallback to dist if version if not found in manifest', async () => { + os.platform = 'linux'; + os.arch = 'x64'; + + // a version which is not in the manifest but is in node dist + let versionSpec = '11'; + + inputs['node-version'] = versionSpec; + inputs['check-latest'] = 'true'; + inputs['always-auth'] = false; + inputs['token'] = 'faketoken'; + + // ... but not in the local cache + findSpy.mockImplementation(() => ''); + + dlSpy.mockImplementation(async () => '/some/temp/path'); + let toolPath = path.normalize('/cache/node/11.11.0/x64'); + exSpy.mockImplementation(async () => '/some/other/temp/path'); + cacheSpy.mockImplementation(async () => toolPath); + + await main.run(); + + let expPath = path.join(toolPath, 'bin'); + + expect(dlSpy).toHaveBeenCalled(); + expect(exSpy).toHaveBeenCalled(); + expect(logSpy).toHaveBeenCalledWith( + 'Attempt to resolve the latest version from manifest...' + ); + expect(logSpy).toHaveBeenCalledWith( + `Failed to resolve version ${versionSpec} from manifest` + ); + expect(logSpy).toHaveBeenCalledWith( + `Attempting to download ${versionSpec}...` + ); + expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); + }); + + it('fallback to dist if manifest is not available', async () => { + os.platform = 'linux'; + os.arch = 'x64'; + + // a version which is not in the manifest but is in node dist + let versionSpec = '12'; + + inputs['node-version'] = versionSpec; + inputs['check-latest'] = 'true'; + inputs['always-auth'] = false; + inputs['token'] = 'faketoken'; + + // ... but not in the local cache + findSpy.mockImplementation(() => ''); + getManifestSpy.mockImplementation(() => { + throw new Error('Unable to download manifest'); + }); + + dlSpy.mockImplementation(async () => '/some/temp/path'); + let toolPath = path.normalize('/cache/node/12.11.0/x64'); + exSpy.mockImplementation(async () => '/some/other/temp/path'); + cacheSpy.mockImplementation(async () => toolPath); + + await main.run(); + + let expPath = path.join(toolPath, 'bin'); + + expect(dlSpy).toHaveBeenCalled(); + expect(exSpy).toHaveBeenCalled(); + expect(logSpy).toHaveBeenCalledWith( + 'Attempt to resolve the latest version from manifest...' + ); + expect(logSpy).toHaveBeenCalledWith( + 'Unable to resolve version from manifest...' + ); + expect(logSpy).toHaveBeenCalledWith( + `Failed to resolve version ${versionSpec} from manifest` + ); + expect(logSpy).toHaveBeenCalledWith( + `Attempting to download ${versionSpec}...` + ); + expect(cnSpy).toHaveBeenCalledWith(`::add-path::${expPath}${osm.EOL}`); + }); + }); }); diff --git a/action.yml b/action.yml index b7c78607..e5b901fc 100644 --- a/action.yml +++ b/action.yml @@ -7,6 +7,9 @@ inputs: default: 'false' node-version: description: 'Version Spec of the version to use. Examples: 12.x, 10.15.1, >=10.15.0' + check-latest: + description: 'Set this option if you want the action to check for the latest available version that satisfies the version spec' + default: false registry-url: 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' scope: diff --git a/dist/index.js b/dist/index.js index 87ce6f0c..93c2d649 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4643,12 +4643,12 @@ function run() { if (!version) { version = core.getInput('version'); } - console.log(`version: ${version}`); if (version) { let token = core.getInput('token'); let auth = !token || isGhes() ? undefined : `token ${token}`; let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; - yield installer.getNode(version, stable, auth); + const checkLatest = (core.getInput('check-latest') || 'false').toUpperCase() === 'TRUE'; + yield installer.getNode(version, stable, checkLatest, auth); } const registryUrl = core.getInput('registry-url'); const alwaysAuth = core.getInput('always-auth'); @@ -12994,19 +12994,30 @@ const tc = __importStar(__webpack_require__(533)); const path = __importStar(__webpack_require__(622)); const semver = __importStar(__webpack_require__(280)); const fs = __webpack_require__(747); -function getNode(versionSpec, stable, auth) { +function getNode(versionSpec, stable, checkLatest, auth) { return __awaiter(this, void 0, void 0, function* () { let osPlat = os.platform(); let osArch = translateArchToDistUrl(os.arch()); + if (checkLatest) { + core.info('Attempt to resolve the latest version from manifest...'); + const resolvedVersion = yield resolveVersionFromManifest(versionSpec, stable, auth); + if (resolvedVersion) { + versionSpec = resolvedVersion; + core.info(`Resolved as '${versionSpec}'`); + } + else { + core.info(`Failed to resolve version ${versionSpec} from manifest`); + } + } // check cache let toolPath; toolPath = tc.find('node', versionSpec); // If not found in cache, download if (toolPath) { - console.log(`Found in cache @ ${toolPath}`); + core.info(`Found in cache @ ${toolPath}`); } else { - console.log(`Attempting to download ${versionSpec}...`); + core.info(`Attempting to download ${versionSpec}...`); let downloadPath = ''; let info = null; // @@ -13015,24 +13026,24 @@ function getNode(versionSpec, stable, auth) { try { info = yield getInfoFromManifest(versionSpec, stable, auth); if (info) { - console.log(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); + core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); downloadPath = yield tc.downloadTool(info.downloadUrl, undefined, auth); } else { - console.log('Not found in manifest. Falling back to download directly from Node'); + core.info('Not found in manifest. Falling back to download directly from Node'); } } catch (err) { // Rate limit? if (err instanceof tc.HTTPError && (err.httpStatusCode === 403 || err.httpStatusCode === 429)) { - console.log(`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`); + core.info(`Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded`); } else { - console.log(err.message); + core.info(err.message); } core.debug(err.stack); - console.log('Falling back to download directly from Node'); + core.info('Falling back to download directly from Node'); } // // Download from nodejs.org @@ -13042,7 +13053,7 @@ function getNode(versionSpec, stable, auth) { if (!info) { throw new Error(`Unable to find Node version '${versionSpec}' for platform ${osPlat} and architecture ${osArch}.`); } - console.log(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); + core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); try { downloadPath = yield tc.downloadTool(info.downloadUrl); } @@ -13056,7 +13067,7 @@ function getNode(versionSpec, stable, auth) { // // Extract // - console.log('Extracting ...'); + core.info('Extracting ...'); let extPath; info = info || {}; // satisfy compiler, never null when reaches here if (osPlat == 'win32') { @@ -13078,9 +13089,9 @@ function getNode(versionSpec, stable, auth) { // // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded // - console.log('Adding to the cache ...'); + core.info('Adding to the cache ...'); toolPath = yield tc.cacheDir(extPath, 'node', info.resolvedVersion); - console.log('Done'); + core.info('Done'); } // // a tool installer initimately knows details about the layout of that tool @@ -13100,7 +13111,6 @@ function getInfoFromManifest(versionSpec, stable, auth) { return __awaiter(this, void 0, void 0, function* () { let info = null; const releases = yield tc.getManifestFromRepo('actions', 'node-versions', auth); - console.log(`matching ${versionSpec}...`); const rel = yield tc.findFromManifest(versionSpec, stable, releases); if (rel && rel.files.length > 0) { info = {}; @@ -13136,6 +13146,18 @@ function getInfoFromDist(versionSpec) { }; }); } +function resolveVersionFromManifest(versionSpec, stable, auth) { + return __awaiter(this, void 0, void 0, function* () { + try { + const info = yield getInfoFromManifest(versionSpec, stable, auth); + return info === null || info === void 0 ? void 0 : info.resolvedVersion; + } + catch (err) { + core.info('Unable to resolve version from manifest...'); + core.debug(err.message); + } + }); +} // TODO - should we just export this from @actions/tool-cache? Lifted directly from there function evaluateVersions(versions, versionSpec) { let version = ''; @@ -13233,7 +13255,7 @@ function acquireNodeFromFallbackLocation(version) { try { exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`; libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`; - console.log(`Downloading only node binary from ${exeUrl}`); + core.info(`Downloading only node binary from ${exeUrl}`); const exePath = yield tc.downloadTool(exeUrl); yield io.cp(exePath, path.join(tempDir, 'node.exe')); const libPath = yield tc.downloadTool(libUrl); diff --git a/src/installer.ts b/src/installer.ts index ee96bda4..d9c3f9e6 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -26,20 +26,36 @@ interface INodeVersionInfo { export async function getNode( versionSpec: string, stable: boolean, + checkLatest: boolean, auth: string | undefined ) { let osPlat: string = os.platform(); let osArch: string = translateArchToDistUrl(os.arch()); + if (checkLatest) { + core.info('Attempt to resolve the latest version from manifest...'); + const resolvedVersion = await resolveVersionFromManifest( + versionSpec, + stable, + auth + ); + if (resolvedVersion) { + versionSpec = resolvedVersion; + core.info(`Resolved as '${versionSpec}'`); + } else { + core.info(`Failed to resolve version ${versionSpec} from manifest`); + } + } + // check cache let toolPath: string; toolPath = tc.find('node', versionSpec); // If not found in cache, download if (toolPath) { - console.log(`Found in cache @ ${toolPath}`); + core.info(`Found in cache @ ${toolPath}`); } else { - console.log(`Attempting to download ${versionSpec}...`); + core.info(`Attempting to download ${versionSpec}...`); let downloadPath = ''; let info: INodeVersionInfo | null = null; @@ -49,12 +65,10 @@ export async function getNode( try { info = await getInfoFromManifest(versionSpec, stable, auth); if (info) { - console.log( - `Acquiring ${info.resolvedVersion} from ${info.downloadUrl}` - ); + core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); downloadPath = await tc.downloadTool(info.downloadUrl, undefined, auth); } else { - console.log( + core.info( 'Not found in manifest. Falling back to download directly from Node' ); } @@ -64,14 +78,14 @@ export async function getNode( err instanceof tc.HTTPError && (err.httpStatusCode === 403 || err.httpStatusCode === 429) ) { - console.log( + core.info( `Received HTTP status code ${err.httpStatusCode}. This usually indicates the rate limit has been exceeded` ); } else { - console.log(err.message); + core.info(err.message); } core.debug(err.stack); - console.log('Falling back to download directly from Node'); + core.info('Falling back to download directly from Node'); } // @@ -85,7 +99,7 @@ export async function getNode( ); } - console.log(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); + core.info(`Acquiring ${info.resolvedVersion} from ${info.downloadUrl}`); try { downloadPath = await tc.downloadTool(info.downloadUrl); } catch (err) { @@ -100,7 +114,7 @@ export async function getNode( // // Extract // - console.log('Extracting ...'); + core.info('Extracting ...'); let extPath: string; info = info || ({} as INodeVersionInfo); // satisfy compiler, never null when reaches here if (osPlat == 'win32') { @@ -122,9 +136,9 @@ export async function getNode( // // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded // - console.log('Adding to the cache ...'); + core.info('Adding to the cache ...'); toolPath = await tc.cacheDir(extPath, 'node', info.resolvedVersion); - console.log('Done'); + core.info('Done'); } // @@ -152,7 +166,6 @@ async function getInfoFromManifest( 'node-versions', auth ); - console.log(`matching ${versionSpec}...`); const rel = await tc.findFromManifest(versionSpec, stable, releases); if (rel && rel.files.length > 0) { @@ -197,6 +210,20 @@ async function getInfoFromDist( }; } +async function resolveVersionFromManifest( + versionSpec: string, + stable: boolean, + auth: string | undefined +): Promise { + try { + const info = await getInfoFromManifest(versionSpec, stable, auth); + return info?.resolvedVersion; + } catch (err) { + core.info('Unable to resolve version from manifest...'); + core.debug(err.message); + } +} + // TODO - should we just export this from @actions/tool-cache? Lifted directly from there function evaluateVersions(versions: string[], versionSpec: string): string { let version = ''; @@ -301,7 +328,7 @@ async function acquireNodeFromFallbackLocation( exeUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.exe`; libUrl = `https://nodejs.org/dist/v${version}/win-${osArch}/node.lib`; - console.log(`Downloading only node binary from ${exeUrl}`); + core.info(`Downloading only node binary from ${exeUrl}`); const exePath = await tc.downloadTool(exeUrl); await io.cp(exePath, path.join(tempDir, 'node.exe')); diff --git a/src/main.ts b/src/main.ts index ab89421f..328db0d2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,12 +15,13 @@ export async function run() { version = core.getInput('version'); } - console.log(`version: ${version}`); if (version) { let token = core.getInput('token'); let auth = !token || isGhes() ? undefined : `token ${token}`; let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; - await installer.getNode(version, stable, auth); + const checkLatest = + (core.getInput('check-latest') || 'false').toUpperCase() === 'TRUE'; + await installer.getNode(version, stable, checkLatest, auth); } const registryUrl: string = core.getInput('registry-url');