import * as child_process from 'child_process'; import * as os from 'os'; import * as path from 'path'; import * as installer from './installer'; import * as stateHelper from './state-helper'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; async function run(): Promise { try { if (os.platform() !== 'linux') { core.setFailed('Only supported on linux platform'); return; } const buildxVer: string = core.getInput('buildx-version') || 'latest'; const driver: string = core.getInput('driver') || 'docker-container'; const driverOpt: string = core.getInput('driver-opt'); const install: boolean = /true/i.test(core.getInput('install')); const use: boolean = /true/i.test(core.getInput('use')); const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker'); await installer.buildx(buildxVer, dockerConfigHome); core.info('📣 Buildx info'); await exec.exec('docker', ['buildx', 'version']); core.info('🔨 Creating a new builder instance...'); let createArgs: Array = [ 'buildx', 'create', '--name', `builder-${process.env.GITHUB_SHA}`, '--driver', driver ]; if (driverOpt) { createArgs.push('--driver-opt', driverOpt); } if (use) { createArgs.push('--use'); } await exec.exec('docker', createArgs); core.info('🏃 Booting builder...'); await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']); if (install) { core.info('🤝 Setting buildx as default builder...'); await exec.exec('docker', ['buildx', 'install']); } core.info('🐳 Docker info'); await exec.exec('docker', ['info']); core.info('🛒 Extracting available platforms...'); const inspect = child_process.execSync('docker buildx inspect', { encoding: 'utf8' }); for (const line of inspect.split(os.EOL)) { if (line.startsWith('Platforms')) { core.setOutput('platforms', line.replace('Platforms: ', '').replace(/\s/g, '').trim()); break; } } } catch (error) { core.setFailed(error.message); } } async function cleanup(): Promise { try { core.info('🚿 Removing builder instance...'); await exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`]); } catch (error) { core.warning(error.message); } } // Main if (!stateHelper.IsPost) { run(); } // Post else { cleanup(); }