diff --git a/__tests__/regclient/install.test.itg.ts b/__tests__/regclient/install.test.itg.ts new file mode 100644 index 0000000..9323186 --- /dev/null +++ b/__tests__/regclient/install.test.itg.ts @@ -0,0 +1,38 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, expect, test} from '@jest/globals'; +import * as fs from 'fs'; + +import {Install} from '../../src/regclient/install'; + +describe('download', () => { + // prettier-ignore + test.each(['latest'])( + 'install regclient %s', async (version) => { + await expect((async () => { + const install = new Install(); + const toolPath = await install.download(version); + if (!fs.existsSync(toolPath)) { + throw new Error('toolPath does not exist'); + } + const binPath = await install.install(toolPath); + if (!fs.existsSync(binPath)) { + throw new Error('binPath does not exist'); + } + })()).resolves.not.toThrow(); + }, 60000); +}); diff --git a/__tests__/regclient/install.test.ts b/__tests__/regclient/install.test.ts new file mode 100644 index 0000000..d260832 --- /dev/null +++ b/__tests__/regclient/install.test.ts @@ -0,0 +1,124 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import {describe, expect, it, jest, test, afterEach} from '@jest/globals'; +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import * as rimraf from 'rimraf'; +import osm = require('os'); + +import {Install} from '../../src/regclient/install'; + +const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'regclient-install-')); + +afterEach(function () { + rimraf.sync(tmpDir); +}); + +describe('download', () => { + // prettier-ignore + test.each([ + ['v0.8.2'], + ['latest'] + ])( + 'acquires %p of regclient', async (version) => { + const install = new Install(); + const toolPath = await install.download(version); + expect(fs.existsSync(toolPath)).toBe(true); + const regclientBin = await install.install(toolPath, tmpDir); + expect(fs.existsSync(regclientBin)).toBe(true); + }, + 100000 + ); + + // prettier-ignore + test.each([ + // following versions are already cached to htc from previous test cases + ['v0.8.2'], + ])( + 'acquires %p of regclient with cache', async (version) => { + const install = new Install(); + const toolPath = await install.download(version); + expect(fs.existsSync(toolPath)).toBe(true); + }); + + // prettier-ignore + test.each([ + ['v0.8.1'], + ])( + 'acquires %p of regclient without cache', async (version) => { + const install = new Install(); + const toolPath = await install.download(version, true); + expect(fs.existsSync(toolPath)).toBe(true); + }); + + // prettier-ignore + test.each([ + ['win32', 'x64'], + ['darwin', 'x64'], + ['darwin', 'arm64'], + ['linux', 'x64'], + ['linux', 'arm64'], + ['linux', 'ppc64'], + ['linux', 's390x'], + ])( + 'acquires regclient for %s/%s', async (os, arch) => { + jest.spyOn(osm, 'platform').mockImplementation(() => os as NodeJS.Platform); + jest.spyOn(osm, 'arch').mockImplementation(() => arch); + const install = new Install(); + const regclientBin = await install.download('latest'); + expect(fs.existsSync(regclientBin)).toBe(true); + }, + 100000 + ); +}); + +describe('getDownloadVersion', () => { + it('returns latest download version', async () => { + const version = await Install.getDownloadVersion('latest'); + expect(version.version).toEqual('latest'); + expect(version.downloadURL).toEqual('https://github.com/regclient/regclient/releases/download/v%s/%s'); + expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json'); + }); + it('returns v0.8.1 download version', async () => { + const version = await Install.getDownloadVersion('v0.8.1'); + expect(version.version).toEqual('v0.8.1'); + expect(version.downloadURL).toEqual('https://github.com/regclient/regclient/releases/download/v%s/%s'); + expect(version.releasesURL).toEqual('https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json'); + }); +}); + +describe('getRelease', () => { + it('returns latest GitHub release', async () => { + const version = await Install.getDownloadVersion('latest'); + const release = await Install.getRelease(version); + expect(release).not.toBeNull(); + expect(release?.tag_name).not.toEqual(''); + }); + it('returns v0.8.1 GitHub release', async () => { + const version = await Install.getDownloadVersion('v0.8.1'); + const release = await Install.getRelease(version); + expect(release).not.toBeNull(); + expect(release?.id).toEqual(199719231); + expect(release?.tag_name).toEqual('v0.8.1'); + expect(release?.html_url).toEqual('https://github.com/regclient/regclient/releases/tag/v0.8.1'); + }); + it('unknown release', async () => { + const version = await Install.getDownloadVersion('foo'); + await expect(Install.getRelease(version)).rejects.toThrow(new Error('Cannot find regclient release foo in https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json')); + }); +}); diff --git a/__tests__/undock/install.test.ts b/__tests__/undock/install.test.ts index 7d0984d..3c27e1f 100644 --- a/__tests__/undock/install.test.ts +++ b/__tests__/undock/install.test.ts @@ -36,7 +36,7 @@ describe('download', () => { ['v0.7.0'], ['latest'] ])( - 'acquires %p of undock (standalone: %p)', async (version) => { + 'acquires %p of undock', async (version) => { const install = new Install(); const toolPath = await install.download(version); expect(fs.existsSync(toolPath)).toBe(true); diff --git a/src/regclient/install.ts b/src/regclient/install.ts new file mode 100644 index 0000000..92b8b10 --- /dev/null +++ b/src/regclient/install.ts @@ -0,0 +1,155 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from 'fs'; +import os from 'os'; +import path from 'path'; +import * as core from '@actions/core'; +import * as httpm from '@actions/http-client'; +import * as tc from '@actions/tool-cache'; +import * as semver from 'semver'; +import * as util from 'util'; + +import {Cache} from '../cache'; +import {Context} from '../context'; + +import {GitHubRelease} from '../types/github'; +import {DownloadVersion} from '../types/regclient/regclient'; + +export class Install { + /* + * Download regclient binary from GitHub release + * @param v: version semver version or latest + * @param ghaNoCache: disable binary caching in GitHub Actions cache backend + * @returns path to the regclient binary + */ + public async download(v: string, ghaNoCache?: boolean): Promise { + const version: DownloadVersion = await Install.getDownloadVersion(v); + core.debug(`Install.download version: ${version.version}`); + + const release: GitHubRelease = await Install.getRelease(version); + core.debug(`Install.download release tag name: ${release.tag_name}`); + + const vspec = await this.vspec(release.tag_name); + core.debug(`Install.download vspec: ${vspec}`); + + const c = semver.clean(vspec) || ''; + if (!semver.valid(c)) { + throw new Error(`Invalid regclient version "${vspec}".`); + } + + const installCache = new Cache({ + htcName: 'regctl-dl-bin', + htcVersion: vspec, + baseCacheDir: path.join(os.homedir(), '.bin'), + cacheFile: os.platform() == 'win32' ? 'regctl.exe' : 'regctl', + ghaNoCache: ghaNoCache + }); + + const cacheFoundPath = await installCache.find(); + if (cacheFoundPath) { + core.info(`regctl binary found in ${cacheFoundPath}`); + return cacheFoundPath; + } + + const downloadURL = util.format(version.downloadURL, vspec, this.filename()); + core.info(`Downloading ${downloadURL}`); + + const htcDownloadPath = await tc.downloadTool(downloadURL); + core.debug(`Install.download htcDownloadPath: ${htcDownloadPath}`); + + const cacheSavePath = await installCache.save(htcDownloadPath); + core.info(`Cached to ${cacheSavePath}`); + return cacheSavePath; + } + + public async install(binPath: string, dest?: string): Promise { + dest = dest || Context.tmpDir(); + + const binDir = path.join(dest, 'regctl-bin'); + if (!fs.existsSync(binDir)) { + fs.mkdirSync(binDir, {recursive: true}); + } + const binName: string = os.platform() == 'win32' ? 'regctl.exe' : 'regctl'; + const regctlPath: string = path.join(binDir, binName); + fs.copyFileSync(binPath, regctlPath); + + core.info('Fixing perms'); + fs.chmodSync(regctlPath, '0755'); + + core.addPath(binDir); + core.info('Added regctl to PATH'); + + core.info(`Binary path: ${regctlPath}`); + return regctlPath; + } + + private filename(): string { + let arch: string; + switch (os.arch()) { + case 'x64': { + arch = 'amd64'; + break; + } + case 'ppc64': { + arch = 'ppc64le'; + break; + } + case 'arm': { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const arm_version = (process.config.variables as any).arm_version; + arch = arm_version ? 'armv' + arm_version : 'arm'; + break; + } + default: { + arch = os.arch(); + break; + } + } + const platform: string = os.platform() == 'win32' ? 'windows' : os.platform(); + const ext: string = os.platform() == 'win32' ? '.exe' : ''; + return util.format('regctl-%s-%s%s', platform, arch, ext); + } + + private async vspec(version: string): Promise { + const v = version.replace(/^v+|v+$/g, ''); + core.info(`Use ${v} version spec cache key for ${version}`); + return v; + } + + public static async getDownloadVersion(v: string): Promise { + return { + version: v, + downloadURL: 'https://github.com/regclient/regclient/releases/download/v%s/%s', + releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/regclient-releases.json' + }; + } + + public static async getRelease(version: DownloadVersion): Promise { + const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); + const resp: httpm.HttpClientResponse = await http.get(version.releasesURL); + const body = await resp.readBody(); + const statusCode = resp.message.statusCode || 500; + if (statusCode >= 400) { + throw new Error(`Failed to get regclient releases from ${version.releasesURL} with status code ${statusCode}: ${body}`); + } + const releases = >JSON.parse(body); + if (!releases[version.version]) { + throw new Error(`Cannot find regclient release ${version.version} in ${version.releasesURL}`); + } + return releases[version.version]; + } +} diff --git a/src/types/regclient/regclient.ts b/src/types/regclient/regclient.ts new file mode 100644 index 0000000..f06edeb --- /dev/null +++ b/src/types/regclient/regclient.ts @@ -0,0 +1,21 @@ +/** + * Copyright 2025 actions-toolkit authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export interface DownloadVersion { + version: string; + downloadURL: string; + releasesURL: string; +}