import * as fs from 'fs'; import * as path from 'path'; import * as semver from 'semver'; import * as util from 'util'; import * as context from './context'; import * as exec from './exec'; import * as github from './github'; import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; export async function getVersion(): Promise { return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => { if (res.stderr != '' && !res.success) { throw new Error(res.stderr); } return parseVersion(res.stdout); }); } export async function parseVersion(stdout: string): Promise { const matches = /\sv?([0-9.]+)/.exec(stdout); if (!matches) { throw new Error(`Cannot parse Buildx version`); } return semver.clean(matches[1]); } export async function isAvailable(): Promise { return await exec.exec(`docker`, ['buildx'], true).then(res => { if (res.stderr != '' && !res.success) { return false; } return res.success; }); } export async function platforms(): Promise { return await exec.exec(`docker`, ['buildx', 'inspect'], true).then(res => { if (res.stderr != '' && !res.success) { throw new Error(res.stderr); } for (const line of res.stdout.trim().split(`\n`)) { if (line.startsWith('Platforms')) { return line.replace('Platforms: ', '').replace(/\s/g, '').trim(); } } }); } export async function install(inputVersion: string, dockerConfigHome: string): Promise { const release: github.GitHubRelease | null = await github.getRelease(inputVersion); if (!release) { throw new Error(`Cannot find buildx ${inputVersion} release`); } core.debug(`Release ${release.tag_name} found`); const version = release.tag_name.replace(/^v+|v+$/g, ''); let toolPath: string; toolPath = tc.find('buildx', version); if (!toolPath) { const c = semver.clean(version) || ''; if (!semver.valid(c)) { throw new Error(`Invalid Buildx version "${version}".`); } toolPath = await download(version); } const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins'); core.debug(`Plugins dir is ${pluginsDir}`); if (!fs.existsSync(pluginsDir)) { fs.mkdirSync(pluginsDir, {recursive: true}); } const filename: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const pluginPath: string = path.join(pluginsDir, filename); core.debug(`Plugin path is ${pluginPath}`); fs.copyFileSync(path.join(toolPath, filename), pluginPath); core.info('Fixing perms'); fs.chmodSync(pluginPath, '0755'); return pluginPath; } async function download(version: string): Promise { const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; const downloadUrl = util.format( 'https://github.com/docker/buildx/releases/download/v%s/%s', version, await filename(version) ); let downloadPath: string; try { core.info(`Downloading ${downloadUrl}`); downloadPath = await tc.downloadTool(downloadUrl); core.debug(`Downloaded to ${downloadPath}`); } catch (error) { throw error; } return await tc.cacheFile(downloadPath, targetFile, 'buildx', version); } async function filename(version: string): Promise { let arch: string; switch (context.osArch) { case 'x64': { arch = 'amd64'; break; } case 'ppc64': { arch = 'ppc64le'; break; } case 'arm': { const arm_version = (process.config.variables as any).arm_version; arch = arm_version ? 'arm-v' + arm_version : 'arm'; break; } default: { arch = context.osArch; break; } } const platform: string = context.osPlat == 'win32' ? 'windows' : context.osPlat; const ext: string = context.osPlat == 'win32' ? '.exe' : ''; return util.format('buildx-v%s.%s-%s%s', version, platform, arch, ext); }