From ea0c8ed90e41c75f2638ac889c785a4bda6d84bf Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 16 Aug 2020 01:23:25 +0200 Subject: [PATCH] Handle push without buildx Signed-off-by: CrazyMax --- dist/index.js | 63 ++++++++++++++++++++++++++++++------------------ src/main.ts | 67 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 84 insertions(+), 46 deletions(-) diff --git a/dist/index.js b/dist/index.js index 6e63d75..e62b77d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1015,10 +1015,12 @@ function run() { } const inputs = yield context_helper_1.loadInputs(); let buildArgs = []; + const buildxEnabled = yield context_helper_1.mustBuildx(inputs); // Check buildx - if (yield context_helper_1.mustBuildx(inputs)) { + if (buildxEnabled) { if (yield !buildx.isAvailable()) { - throw new Error(`Buildx is required but not available`); + core.setFailed(`Buildx is required but not available`); + return; } core.info(`🚀 Buildx will be used to build your image`); buildArgs.push('buildx', 'build'); @@ -1026,6 +1028,7 @@ function run() { else { buildArgs.push('build'); } + // Global options if (inputs.file) { buildArgs.push('--file', inputs.file); } @@ -1047,31 +1050,45 @@ function run() { if (inputs.noCache) { buildArgs.push('--no-cache'); } - if (inputs.builder) { - core.info(`📌 Using build instance ${inputs.builder}`); - yield buildx.use(inputs.builder); + // Buildx options + if (buildxEnabled) { + if (inputs.builder) { + core.info(`📌 Using build instance ${inputs.builder}`); + yield buildx.use(inputs.builder); + } + if (inputs.platforms) { + buildArgs.push('--platform', inputs.platforms); + } + if (inputs.load) { + buildArgs.push('--load'); + } + if (inputs.push) { + buildArgs.push('--push'); + } + yield asyncForEach(inputs.outputs, (output) => __awaiter(this, void 0, void 0, function* () { + buildArgs.push('--output', output); + })); + yield asyncForEach(inputs.cacheFrom, (cacheFrom) => __awaiter(this, void 0, void 0, function* () { + buildArgs.push('--cache-from', cacheFrom); + })); + yield asyncForEach(inputs.cacheTo, (cacheTo) => __awaiter(this, void 0, void 0, function* () { + buildArgs.push('--cache-from', cacheTo); + })); } - if (inputs.platforms) { - buildArgs.push('--platform', inputs.platforms); - } - if (inputs.load) { - buildArgs.push('--load'); - } - if (inputs.push) { - buildArgs.push('--push'); - } - yield asyncForEach(inputs.outputs, (output) => __awaiter(this, void 0, void 0, function* () { - buildArgs.push('--output', output); - })); - yield asyncForEach(inputs.cacheFrom, (cacheFrom) => __awaiter(this, void 0, void 0, function* () { - buildArgs.push('--cache-from', cacheFrom); - })); - yield asyncForEach(inputs.cacheTo, (cacheTo) => __awaiter(this, void 0, void 0, function* () { - buildArgs.push('--cache-from', cacheTo); - })); buildArgs.push(inputs.context); core.info(`🏃 Starting build...`); yield exec.exec('docker', buildArgs); + if (!buildxEnabled && inputs.push) { + let pushRepos = []; + yield asyncForEach(inputs.tags, (tag) => __awaiter(this, void 0, void 0, function* () { + const repo = tag.split(':', -1)[0]; + if (!pushRepos.includes(repo)) { + pushRepos.push(repo); + core.info(`⬆️ Pushing ${repo}...`); + yield exec.exec('docker', ['push', repo]); + } + })); + } } catch (error) { core.setFailed(error.message); diff --git a/src/main.ts b/src/main.ts index 3804db4..9c58940 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,11 +13,13 @@ async function run(): Promise { const inputs: Inputs = await loadInputs(); let buildArgs: Array = []; + const buildxEnabled = await mustBuildx(inputs); // Check buildx - if (await mustBuildx(inputs)) { + if (buildxEnabled) { if (await !buildx.isAvailable()) { - throw new Error(`Buildx is required but not available`); + core.setFailed(`Buildx is required but not available`); + return; } core.info(`🚀 Buildx will be used to build your image`); buildArgs.push('buildx', 'build'); @@ -25,6 +27,7 @@ async function run(): Promise { buildArgs.push('build'); } + // Global options if (inputs.file) { buildArgs.push('--file', inputs.file); } @@ -46,32 +49,50 @@ async function run(): Promise { if (inputs.noCache) { buildArgs.push('--no-cache'); } - if (inputs.builder) { - core.info(`📌 Using build instance ${inputs.builder}`); - await buildx.use(inputs.builder); + + // Buildx options + if (buildxEnabled) { + if (inputs.builder) { + core.info(`📌 Using build instance ${inputs.builder}`); + await buildx.use(inputs.builder); + } + if (inputs.platforms) { + buildArgs.push('--platform', inputs.platforms); + } + if (inputs.load) { + buildArgs.push('--load'); + } + if (inputs.push) { + buildArgs.push('--push'); + } + await asyncForEach(inputs.outputs, async output => { + buildArgs.push('--output', output); + }); + await asyncForEach(inputs.cacheFrom, async cacheFrom => { + buildArgs.push('--cache-from', cacheFrom); + }); + await asyncForEach(inputs.cacheTo, async cacheTo => { + buildArgs.push('--cache-from', cacheTo); + }); } - if (inputs.platforms) { - buildArgs.push('--platform', inputs.platforms); - } - if (inputs.load) { - buildArgs.push('--load'); - } - if (inputs.push) { - buildArgs.push('--push'); - } - await asyncForEach(inputs.outputs, async output => { - buildArgs.push('--output', output); - }); - await asyncForEach(inputs.cacheFrom, async cacheFrom => { - buildArgs.push('--cache-from', cacheFrom); - }); - await asyncForEach(inputs.cacheTo, async cacheTo => { - buildArgs.push('--cache-from', cacheTo); - }); + buildArgs.push(inputs.context); core.info(`🏃 Starting build...`); await exec.exec('docker', buildArgs); + + if (!buildxEnabled && inputs.push) { + let pushRepos: Array = []; + await asyncForEach(inputs.tags, async tag => { + const repo = tag.split(':', -1)[0]; + if (!pushRepos.includes(repo)) { + pushRepos.push(repo); + + core.info(`⬆️ Pushing ${repo}...`); + await exec.exec('docker', ['push', repo]); + } + }); + } } catch (error) { core.setFailed(error.message); }