Skip to content

Commit

Permalink
Merge pull request #46 from crazy-max/buildx-split-install
Browse files Browse the repository at this point in the history
buildx: split install from download and build methods
  • Loading branch information
crazy-max authored Feb 19, 2023
2 parents 67957d8 + 89ecd37 commit 580aee9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 47 deletions.
19 changes: 15 additions & 4 deletions __tests__/buildx/install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);
});
Expand Down
88 changes: 45 additions & 43 deletions src/buildx/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class Install {
this._standalone = opts?.standalone;
}

public async download(version: string, dest?: string): Promise<string> {
public async download(version: string): Promise<string> {
const release: GitHubRelease = await Install.getRelease(version);
const fversion = release.tag_name.replace(/^v+|v+$/g, '');
core.debug(`Install.download version: ${fversion}`);
Expand All @@ -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<string> {
public async build(gitContext: string): Promise<string> {
// eslint-disable-next-line prefer-const
let [repo, ref] = gitContext.split('#');
if (ref.length == 0) {
Expand Down Expand Up @@ -103,12 +98,48 @@ export class Install {
});
}

return toolPath;
}

public async installStandalone(toolPath: string, dest?: string): Promise<string> {
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');
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);

core.info('Fixing perms');
fs.chmodSync(buildxPath, '0755');

core.addPath(binDir);
core.info('Added Buildx to PATH');

core.info(`Binary path: ${buildxPath}`);
return buildxPath;
}

public async installPlugin(toolPath: string, dest?: string): Promise<string> {
core.info('Docker plugin mode');
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);

core.info('Fixing perms');
fs.chmodSync(pluginPath, '0755');

core.info(`Plugin path: ${pluginPath}`);
return pluginPath;
}

private async buildCommand(gitContext: string, outputDir: string): Promise<{args: Array<string>; command: string}> {
Expand Down Expand Up @@ -148,41 +179,12 @@ export class Install {
return standalone;
}

private async setStandalone(toolPath: string, dest: string): Promise<string> {
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<string> {
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<string> {
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);
}

Expand Down

0 comments on commit 580aee9

Please sign in to comment.