From 139fb39ab0a3ec2cb819403e367a9d1fe68da5c4 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 19 Feb 2023 03:25:07 +0100 Subject: [PATCH 1/2] buildx: split install from download and build methods Signed-off-by: CrazyMax --- __tests__/buildx/install.test.ts | 19 ++++++-- src/buildx/install.ts | 74 ++++++++++++++------------------ 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/__tests__/buildx/install.test.ts b/__tests__/buildx/install.test.ts index 24c2657a..76cb41e6 100644 --- a/__tests__/buildx/install.test.ts +++ b/__tests__/buildx/install.test.ts @@ -43,7 +43,14 @@ describe('download', () => { ])( 'acquires %p of buildx (standalone: %p)', async (version, standalone) => { const install = new Install({standalone: standalone}); - const buildxBin = await install.download(version, tmpDir); + const toolPath = await install.download(version); + expect(fs.existsSync(toolPath)).toBe(true); + let buildxBin: string; + if (standalone) { + buildxBin = await install.installStandalone(toolPath, tmpDir); + } else { + buildxBin = await install.installPlugin(toolPath, tmpDir); + } expect(fs.existsSync(buildxBin)).toBe(true); }, 100000 @@ -65,7 +72,7 @@ describe('download', () => { jest.spyOn(osm, 'platform').mockImplementation(() => os); jest.spyOn(osm, 'arch').mockImplementation(() => arch); const install = new Install(); - const buildxBin = await install.download('latest', tmpDir); + const buildxBin = await install.download('latest'); expect(fs.existsSync(buildxBin)).toBe(true); }, 100000 @@ -82,14 +89,18 @@ describe('build', () => { // eslint-disable-next-line jest/no-disabled-tests it.skip('builds refs/pull/648/head', async () => { const install = new Install(); - const buildxBin = await install.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir); + const toolPath = await install.build('https://github.com/docker/buildx.git#refs/pull/648/head'); + expect(fs.existsSync(toolPath)).toBe(true); + const buildxBin = await install.installStandalone(toolPath, tmpDir); expect(fs.existsSync(buildxBin)).toBe(true); }, 100000); // eslint-disable-next-line jest/no-disabled-tests it.skip('builds 67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', async () => { const install = new Install(); - const buildxBin = await install.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', tmpDir); + const toolPath = await install.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14'); + expect(fs.existsSync(toolPath)).toBe(true); + const buildxBin = await install.installPlugin(toolPath, tmpDir); expect(fs.existsSync(buildxBin)).toBe(true); }, 100000); }); diff --git a/src/buildx/install.ts b/src/buildx/install.ts index 2e790dbf..04c3f9bf 100644 --- a/src/buildx/install.ts +++ b/src/buildx/install.ts @@ -46,7 +46,7 @@ export class Install { this._standalone = opts?.standalone; } - public async download(version: string, dest?: string): Promise { + public async download(version: string): Promise { const release: GitHubRelease = await Install.getRelease(version); const fversion = release.tag_name.replace(/^v+|v+$/g, ''); core.debug(`Install.download version: ${fversion}`); @@ -62,15 +62,10 @@ export class Install { } core.debug(`Install.download toolPath: ${toolPath}`); - dest = dest || ((await this.isStandalone()) ? this.context.tmpDir() : Docker.configDir); - core.debug(`Install.download dest: ${dest}`); - if (await this.isStandalone()) { - return this.setStandalone(toolPath, dest); - } - return this.setPlugin(toolPath, dest); + return toolPath; } - public async build(gitContext: string, dest?: string): Promise { + public async build(gitContext: string): Promise { // eslint-disable-next-line prefer-const let [repo, ref] = gitContext.split('#'); if (ref.length == 0) { @@ -103,12 +98,38 @@ export class Install { }); } + return toolPath; + } + + public async installStandalone(toolPath: string, dest?: string): Promise { + dest = dest || this.context.tmpDir(); + const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); + const binDir = path.join(dest, 'bin'); + if (!fs.existsSync(binDir)) { + fs.mkdirSync(binDir, {recursive: true}); + } + const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx'; + const buildxPath: string = path.join(binDir, filename); + fs.copyFileSync(toolBinPath, buildxPath); + fs.chmodSync(buildxPath, '0755'); + core.addPath(binDir); + core.debug(`Install.installStandalone buildxPath: ${buildxPath}`); + return buildxPath; + } + + public async installPlugin(toolPath: string, dest?: string): Promise { dest = dest || Docker.configDir; - core.debug(`Install.build dest: ${dest}`); - if (await this.isStandalone()) { - return this.setStandalone(toolPath, dest); + const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); + const pluginsDir: string = path.join(dest, 'cli-plugins'); + if (!fs.existsSync(pluginsDir)) { + fs.mkdirSync(pluginsDir, {recursive: true}); } - return this.setPlugin(toolPath, dest); + const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; + const pluginPath: string = path.join(pluginsDir, filename); + fs.copyFileSync(toolBinPath, pluginPath); + fs.chmodSync(pluginPath, '0755'); + core.debug(`Install.installPlugin pluginPath: ${pluginPath}`); + return pluginPath; } private async buildCommand(gitContext: string, outputDir: string): Promise<{args: Array; command: string}> { @@ -148,35 +169,6 @@ export class Install { return standalone; } - private async setStandalone(toolPath: string, dest: string): Promise { - const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); - const binDir = path.join(dest, 'bin'); - if (!fs.existsSync(binDir)) { - fs.mkdirSync(binDir, {recursive: true}); - } - const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx'; - const buildxPath: string = path.join(binDir, filename); - fs.copyFileSync(toolBinPath, buildxPath); - fs.chmodSync(buildxPath, '0755'); - core.addPath(binDir); - core.debug(`Install.setStandalone buildxPath: ${buildxPath}`); - return buildxPath; - } - - private async setPlugin(toolPath: string, dest: string): Promise { - const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); - const pluginsDir: string = path.join(dest, 'cli-plugins'); - if (!fs.existsSync(pluginsDir)) { - fs.mkdirSync(pluginsDir, {recursive: true}); - } - const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; - const pluginPath: string = path.join(pluginsDir, filename); - fs.copyFileSync(toolBinPath, pluginPath); - fs.chmodSync(pluginPath, '0755'); - core.debug(`Install.setPlugin pluginPath: ${pluginPath}`); - return pluginPath; - } - private async fetchBinary(version: string): Promise { const targetFile: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, this.filename(version)); From 89ecd3768135a7115508fbe0042d812fa9fe37af Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 19 Feb 2023 03:34:30 +0100 Subject: [PATCH 2/2] buildx: info logs on install Signed-off-by: CrazyMax --- src/buildx/install.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/buildx/install.ts b/src/buildx/install.ts index 04c3f9bf..46304716 100644 --- a/src/buildx/install.ts +++ b/src/buildx/install.ts @@ -102,6 +102,7 @@ export class Install { } public async installStandalone(toolPath: string, dest?: string): Promise { + core.info('Standalone mode'); dest = dest || this.context.tmpDir(); const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); const binDir = path.join(dest, 'bin'); @@ -111,13 +112,19 @@ export class Install { const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx'; const buildxPath: string = path.join(binDir, filename); fs.copyFileSync(toolBinPath, buildxPath); + + core.info('Fixing perms'); fs.chmodSync(buildxPath, '0755'); + core.addPath(binDir); - core.debug(`Install.installStandalone buildxPath: ${buildxPath}`); + core.info('Added Buildx to PATH'); + + core.info(`Binary path: ${buildxPath}`); return buildxPath; } public async installPlugin(toolPath: string, dest?: string): Promise { + core.info('Docker plugin mode'); dest = dest || Docker.configDir; const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); const pluginsDir: string = path.join(dest, 'cli-plugins'); @@ -127,8 +134,11 @@ export class Install { const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const pluginPath: string = path.join(pluginsDir, filename); fs.copyFileSync(toolBinPath, pluginPath); + + core.info('Fixing perms'); fs.chmodSync(pluginPath, '0755'); - core.debug(`Install.installPlugin pluginPath: ${pluginPath}`); + + core.info(`Plugin path: ${pluginPath}`); return pluginPath; } @@ -172,9 +182,9 @@ export class Install { private async fetchBinary(version: string): Promise { const targetFile: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, this.filename(version)); + core.info(`Downloading ${downloadURL}`); const downloadPath = await tc.downloadTool(downloadURL); - core.debug(`downloadURL: ${downloadURL}`); - core.debug(`downloadPath: ${downloadPath}`); + core.debug(`Install.fetchBinary downloadPath: ${downloadPath}`); return await tc.cacheFile(downloadPath, targetFile, 'buildx', version); }