Skip to content

Commit 580aee9

Browse files
authored
Merge pull request #46 from crazy-max/buildx-split-install
buildx: split install from download and build methods
2 parents 67957d8 + 89ecd37 commit 580aee9

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-47
lines changed

__tests__/buildx/install.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ describe('download', () => {
4343
])(
4444
'acquires %p of buildx (standalone: %p)', async (version, standalone) => {
4545
const install = new Install({standalone: standalone});
46-
const buildxBin = await install.download(version, tmpDir);
46+
const toolPath = await install.download(version);
47+
expect(fs.existsSync(toolPath)).toBe(true);
48+
let buildxBin: string;
49+
if (standalone) {
50+
buildxBin = await install.installStandalone(toolPath, tmpDir);
51+
} else {
52+
buildxBin = await install.installPlugin(toolPath, tmpDir);
53+
}
4754
expect(fs.existsSync(buildxBin)).toBe(true);
4855
},
4956
100000
@@ -65,7 +72,7 @@ describe('download', () => {
6572
jest.spyOn(osm, 'platform').mockImplementation(() => os);
6673
jest.spyOn(osm, 'arch').mockImplementation(() => arch);
6774
const install = new Install();
68-
const buildxBin = await install.download('latest', tmpDir);
75+
const buildxBin = await install.download('latest');
6976
expect(fs.existsSync(buildxBin)).toBe(true);
7077
},
7178
100000
@@ -82,14 +89,18 @@ describe('build', () => {
8289
// eslint-disable-next-line jest/no-disabled-tests
8390
it.skip('builds refs/pull/648/head', async () => {
8491
const install = new Install();
85-
const buildxBin = await install.build('https://github.com/docker/buildx.git#refs/pull/648/head', tmpDir);
92+
const toolPath = await install.build('https://github.com/docker/buildx.git#refs/pull/648/head');
93+
expect(fs.existsSync(toolPath)).toBe(true);
94+
const buildxBin = await install.installStandalone(toolPath, tmpDir);
8695
expect(fs.existsSync(buildxBin)).toBe(true);
8796
}, 100000);
8897

8998
// eslint-disable-next-line jest/no-disabled-tests
9099
it.skip('builds 67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', async () => {
91100
const install = new Install();
92-
const buildxBin = await install.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14', tmpDir);
101+
const toolPath = await install.build('https://github.com/docker/buildx.git#67bd6f4dc82a9cd96f34133dab3f6f7af803bb14');
102+
expect(fs.existsSync(toolPath)).toBe(true);
103+
const buildxBin = await install.installPlugin(toolPath, tmpDir);
93104
expect(fs.existsSync(buildxBin)).toBe(true);
94105
}, 100000);
95106
});

src/buildx/install.ts

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class Install {
4646
this._standalone = opts?.standalone;
4747
}
4848

49-
public async download(version: string, dest?: string): Promise<string> {
49+
public async download(version: string): Promise<string> {
5050
const release: GitHubRelease = await Install.getRelease(version);
5151
const fversion = release.tag_name.replace(/^v+|v+$/g, '');
5252
core.debug(`Install.download version: ${fversion}`);
@@ -62,15 +62,10 @@ export class Install {
6262
}
6363
core.debug(`Install.download toolPath: ${toolPath}`);
6464

65-
dest = dest || ((await this.isStandalone()) ? this.context.tmpDir() : Docker.configDir);
66-
core.debug(`Install.download dest: ${dest}`);
67-
if (await this.isStandalone()) {
68-
return this.setStandalone(toolPath, dest);
69-
}
70-
return this.setPlugin(toolPath, dest);
65+
return toolPath;
7166
}
7267

73-
public async build(gitContext: string, dest?: string): Promise<string> {
68+
public async build(gitContext: string): Promise<string> {
7469
// eslint-disable-next-line prefer-const
7570
let [repo, ref] = gitContext.split('#');
7671
if (ref.length == 0) {
@@ -103,12 +98,48 @@ export class Install {
10398
});
10499
}
105100

101+
return toolPath;
102+
}
103+
104+
public async installStandalone(toolPath: string, dest?: string): Promise<string> {
105+
core.info('Standalone mode');
106+
dest = dest || this.context.tmpDir();
107+
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
108+
const binDir = path.join(dest, 'bin');
109+
if (!fs.existsSync(binDir)) {
110+
fs.mkdirSync(binDir, {recursive: true});
111+
}
112+
const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx';
113+
const buildxPath: string = path.join(binDir, filename);
114+
fs.copyFileSync(toolBinPath, buildxPath);
115+
116+
core.info('Fixing perms');
117+
fs.chmodSync(buildxPath, '0755');
118+
119+
core.addPath(binDir);
120+
core.info('Added Buildx to PATH');
121+
122+
core.info(`Binary path: ${buildxPath}`);
123+
return buildxPath;
124+
}
125+
126+
public async installPlugin(toolPath: string, dest?: string): Promise<string> {
127+
core.info('Docker plugin mode');
106128
dest = dest || Docker.configDir;
107-
core.debug(`Install.build dest: ${dest}`);
108-
if (await this.isStandalone()) {
109-
return this.setStandalone(toolPath, dest);
129+
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
130+
const pluginsDir: string = path.join(dest, 'cli-plugins');
131+
if (!fs.existsSync(pluginsDir)) {
132+
fs.mkdirSync(pluginsDir, {recursive: true});
110133
}
111-
return this.setPlugin(toolPath, dest);
134+
const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
135+
const pluginPath: string = path.join(pluginsDir, filename);
136+
fs.copyFileSync(toolBinPath, pluginPath);
137+
138+
core.info('Fixing perms');
139+
fs.chmodSync(pluginPath, '0755');
140+
141+
core.info(`Plugin path: ${pluginPath}`);
142+
return pluginPath;
112143
}
113144

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

151-
private async setStandalone(toolPath: string, dest: string): Promise<string> {
152-
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
153-
const binDir = path.join(dest, 'bin');
154-
if (!fs.existsSync(binDir)) {
155-
fs.mkdirSync(binDir, {recursive: true});
156-
}
157-
const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx';
158-
const buildxPath: string = path.join(binDir, filename);
159-
fs.copyFileSync(toolBinPath, buildxPath);
160-
fs.chmodSync(buildxPath, '0755');
161-
core.addPath(binDir);
162-
core.debug(`Install.setStandalone buildxPath: ${buildxPath}`);
163-
return buildxPath;
164-
}
165-
166-
private async setPlugin(toolPath: string, dest: string): Promise<string> {
167-
const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx');
168-
const pluginsDir: string = path.join(dest, 'cli-plugins');
169-
if (!fs.existsSync(pluginsDir)) {
170-
fs.mkdirSync(pluginsDir, {recursive: true});
171-
}
172-
const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
173-
const pluginPath: string = path.join(pluginsDir, filename);
174-
fs.copyFileSync(toolBinPath, pluginPath);
175-
fs.chmodSync(pluginPath, '0755');
176-
core.debug(`Install.setPlugin pluginPath: ${pluginPath}`);
177-
return pluginPath;
178-
}
179-
180182
private async fetchBinary(version: string): Promise<string> {
181183
const targetFile: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
182184
const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, this.filename(version));
185+
core.info(`Downloading ${downloadURL}`);
183186
const downloadPath = await tc.downloadTool(downloadURL);
184-
core.debug(`downloadURL: ${downloadURL}`);
185-
core.debug(`downloadPath: ${downloadPath}`);
187+
core.debug(`Install.fetchBinary downloadPath: ${downloadPath}`);
186188
return await tc.cacheFile(downloadPath, targetFile, 'buildx', version);
187189
}
188190

0 commit comments

Comments
 (0)