From af4fd4efb5422944dd62d50a4f0bac08826c06c7 Mon Sep 17 00:00:00 2001 From: Phred Date: Fri, 6 Aug 2021 08:56:33 -0500 Subject: [PATCH 1/4] simplified argument collection Signed-off-by: Phred --- __tests__/context.test.ts | 13 +++++++++++++ src/context.ts | 41 +++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 3435701e1..46c5085d5 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -636,6 +636,19 @@ describe('asyncForEach', () => { }); }); +describe('flagMap', () => { + it('should prepend array elements with the provided flag', async () => { + const testValues = ['a', 'b', 'c']; + const results: string[][] = context.flagMap(testValues, '--catpants'); + + expect(results).toEqual([ + ['--catpants', 'a'], + ['--catpants', 'b'], + ['--catpants', 'c'], + ]); + }); +}); + describe('setOutput', () => { beforeEach(() => { process.stdout.write = jest.fn(); diff --git a/src/context.ts b/src/context.ts index b951a9be8..be5ebc1e0 100644 --- a/src/context.ts +++ b/src/context.ts @@ -88,24 +88,25 @@ export async function getInputs(defaultContext: string): Promise { } export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> { - let args: Array = ['buildx']; - args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion)); - args.push.apply(args, await getCommonArgs(inputs)); - args.push(inputs.context); - return args; + return [ + 'buildx', + ...await getBuildArgs(inputs, defaultContext, buildxVersion), + ...await getCommonArgs(inputs), + inputs.context, + ]; } async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> { - let args: Array = ['build']; - await asyncForEach(inputs.buildArgs, async buildArg => { - args.push('--build-arg', buildArg); - }); - await asyncForEach(inputs.labels, async label => { - args.push('--label', label); - }); - await asyncForEach(inputs.tags, async tag => { - args.push('--tag', tag); - }); + const args: Array = ['build'].concat( + ...flagMap(inputs.buildArgs, '--build-arg'), + ...flagMap(inputs.cacheFrom, '--cache-from'), + ...flagMap(inputs.cacheTo, '--cache-to'), + ...flagMap(inputs.labels, '--label'), + ...flagMap(inputs.outputs, '--output'), + ...flagMap(inputs.tags, '--tag'), + ...flagMap(inputs.ssh, '--ssh'), + ); + if (inputs.target) { args.push('--target', inputs.target); } @@ -115,9 +116,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio if (inputs.platforms.length > 0) { args.push('--platform', inputs.platforms.join(',')); } - await asyncForEach(inputs.outputs, async output => { - args.push('--output', output); - }); if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { args.push('--iidfile', await buildx.getImageIDFile()); } @@ -147,9 +145,6 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) { args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`)); } - await asyncForEach(inputs.ssh, async ssh => { - args.push('--ssh', ssh); - }); if (inputs.file) { args.push('--file', inputs.file); } @@ -212,6 +207,10 @@ export const asyncForEach = async (array, callback) => { } }; +export function flagMap(array: string[], flag: string): string[][] { + return array.map(value => [flag, value]); +} + // FIXME: Temp fix https://github.com/actions/toolkit/issues/777 export function setOutput(name: string, value: any): void { issueCommand('set-output', {name}, value); From 9720455de9087c19821b090890ffe06317a7d06e Mon Sep 17 00:00:00 2001 From: Phred Date: Fri, 6 Aug 2021 09:33:45 -0500 Subject: [PATCH 2/4] npm run format Signed-off-by: Phred --- __tests__/context.test.ts | 2 +- src/context.ts | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts index 46c5085d5..63b1a196e 100644 --- a/__tests__/context.test.ts +++ b/__tests__/context.test.ts @@ -644,7 +644,7 @@ describe('flagMap', () => { expect(results).toEqual([ ['--catpants', 'a'], ['--catpants', 'b'], - ['--catpants', 'c'], + ['--catpants', 'c'] ]); }); }); diff --git a/src/context.ts b/src/context.ts index be5ebc1e0..6eb4ac9e5 100644 --- a/src/context.ts +++ b/src/context.ts @@ -88,12 +88,7 @@ export async function getInputs(defaultContext: string): Promise { } export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> { - return [ - 'buildx', - ...await getBuildArgs(inputs, defaultContext, buildxVersion), - ...await getCommonArgs(inputs), - inputs.context, - ]; + return ['buildx', ...(await getBuildArgs(inputs, defaultContext, buildxVersion)), ...(await getCommonArgs(inputs)), inputs.context]; } async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise> { @@ -104,7 +99,7 @@ async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersio ...flagMap(inputs.labels, '--label'), ...flagMap(inputs.outputs, '--output'), ...flagMap(inputs.tags, '--tag'), - ...flagMap(inputs.ssh, '--ssh'), + ...flagMap(inputs.ssh, '--ssh') ); if (inputs.target) { From 33f1381f7754b6825db27bbdb2646983c5a76298 Mon Sep 17 00:00:00 2001 From: Phred Date: Fri, 6 Aug 2021 10:09:05 -0500 Subject: [PATCH 3/4] added build artifact Signed-off-by: Phred --- dist/index.js | 49 +++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/dist/index.js b/dist/index.js index 88ca69a73..d75d41698 100644 --- a/dist/index.js +++ b/dist/index.js @@ -230,7 +230,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.setOutput = exports.asyncForEach = exports.getInputList = exports.getArgs = exports.getInputs = exports.tmpNameSync = exports.tmpDir = exports.defaultContext = void 0; +exports.setOutput = exports.flagMap = exports.asyncForEach = exports.getInputList = exports.getArgs = exports.getInputs = exports.tmpNameSync = exports.tmpDir = exports.defaultContext = void 0; const sync_1 = __importDefault(__nccwpck_require__(8750)); const fs = __importStar(__nccwpck_require__(5747)); const os = __importStar(__nccwpck_require__(2087)); @@ -296,26 +296,13 @@ function getInputs(defaultContext) { exports.getInputs = getInputs; function getArgs(inputs, defaultContext, buildxVersion) { return __awaiter(this, void 0, void 0, function* () { - let args = ['buildx']; - args.push.apply(args, yield getBuildArgs(inputs, defaultContext, buildxVersion)); - args.push.apply(args, yield getCommonArgs(inputs)); - args.push(inputs.context); - return args; + return ['buildx', ...(yield getBuildArgs(inputs, defaultContext, buildxVersion)), ...(yield getCommonArgs(inputs)), inputs.context]; }); } exports.getArgs = getArgs; function getBuildArgs(inputs, defaultContext, buildxVersion) { return __awaiter(this, void 0, void 0, function* () { - let args = ['build']; - yield exports.asyncForEach(inputs.buildArgs, (buildArg) => __awaiter(this, void 0, void 0, function* () { - args.push('--build-arg', buildArg); - })); - yield exports.asyncForEach(inputs.labels, (label) => __awaiter(this, void 0, void 0, function* () { - args.push('--label', label); - })); - yield exports.asyncForEach(inputs.tags, (tag) => __awaiter(this, void 0, void 0, function* () { - args.push('--tag', tag); - })); + const args = ['build'].concat(...flagMap(inputs.buildArgs, '--build-arg'), ...flagMap(inputs.cacheFrom, '--cache-from'), ...flagMap(inputs.cacheTo, '--cache-to'), ...flagMap(inputs.labels, '--label'), ...flagMap(inputs.outputs, '--output'), ...flagMap(inputs.tags, '--tag'), ...flagMap(inputs.ssh, '--ssh')); if (inputs.target) { args.push('--target', inputs.target); } @@ -325,9 +312,6 @@ function getBuildArgs(inputs, defaultContext, buildxVersion) { if (inputs.platforms.length > 0) { args.push('--platform', inputs.platforms.join(',')); } - yield exports.asyncForEach(inputs.outputs, (output) => __awaiter(this, void 0, void 0, function* () { - args.push('--output', output); - })); if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) { args.push('--iidfile', yield buildx.getImageIDFile()); } @@ -359,9 +343,6 @@ function getBuildArgs(inputs, defaultContext, buildxVersion) { if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) { args.push('--secret', yield buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`)); } - yield exports.asyncForEach(inputs.ssh, (ssh) => __awaiter(this, void 0, void 0, function* () { - args.push('--ssh', ssh); - })); if (inputs.file) { args.push('--file', inputs.file); } @@ -425,6 +406,10 @@ const asyncForEach = (array, callback) => __awaiter(void 0, void 0, void 0, func } }); exports.asyncForEach = asyncForEach; +function flagMap(array, flag) { + return array.map(value => [flag, value]); +} +exports.flagMap = flagMap; // FIXME: Temp fix https://github.com/actions/toolkit/issues/777 function setOutput(name, value) { command_1.issueCommand('set-output', { name }, value); @@ -1091,8 +1076,8 @@ class OidcClient { const res = yield httpclient .getJson(id_token_url) .catch(error => { - throw new Error(`Failed to get ID Token. \n - Error Code : ${error.statusCode}\n + throw new Error(`Failed to get ID Token. \n + Error Code : ${error.statusCode}\n Error Message: ${error.result.message}`); }); const id_token = (_a = res.result) === null || _a === void 0 ? void 0 : _a.value; @@ -17234,7 +17219,7 @@ module.exports = require("zlib");; /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; -/******/ +/******/ /******/ // The require function /******/ function __nccwpck_require__(moduleId) { /******/ // Check if module is in cache @@ -17248,7 +17233,7 @@ module.exports = require("zlib");; /******/ // no module.loaded needed /******/ exports: {} /******/ }; -/******/ +/******/ /******/ // Execute the module function /******/ var threw = true; /******/ try { @@ -17257,21 +17242,21 @@ module.exports = require("zlib");; /******/ } finally { /******/ if(threw) delete __webpack_module_cache__[moduleId]; /******/ } -/******/ +/******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } -/******/ +/******/ /************************************************************************/ /******/ /* webpack/runtime/compat */ -/******/ +/******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/ -/******/ +/******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module is referenced by other modules so it can't be inlined /******/ var __webpack_exports__ = __nccwpck_require__(3109); /******/ module.exports = __webpack_exports__; -/******/ +/******/ /******/ })() -; \ No newline at end of file +; From d9364845e6230127c6cd360db58d6ffbd48c8bdf Mon Sep 17 00:00:00 2001 From: Phred Date: Tue, 2 Nov 2021 09:49:09 -0500 Subject: [PATCH 4/4] docker buildx bake build Signed-off-by: Phred --- dist/index.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index d75d41698..0f40b04a1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1076,8 +1076,8 @@ class OidcClient { const res = yield httpclient .getJson(id_token_url) .catch(error => { - throw new Error(`Failed to get ID Token. \n - Error Code : ${error.statusCode}\n + throw new Error(`Failed to get ID Token. \n + Error Code : ${error.statusCode}\n Error Message: ${error.result.message}`); }); const id_token = (_a = res.result) === null || _a === void 0 ? void 0 : _a.value; @@ -17219,7 +17219,7 @@ module.exports = require("zlib");; /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; -/******/ +/******/ /******/ // The require function /******/ function __nccwpck_require__(moduleId) { /******/ // Check if module is in cache @@ -17233,7 +17233,7 @@ module.exports = require("zlib");; /******/ // no module.loaded needed /******/ exports: {} /******/ }; -/******/ +/******/ /******/ // Execute the module function /******/ var threw = true; /******/ try { @@ -17242,21 +17242,21 @@ module.exports = require("zlib");; /******/ } finally { /******/ if(threw) delete __webpack_module_cache__[moduleId]; /******/ } -/******/ +/******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } -/******/ +/******/ /************************************************************************/ /******/ /* webpack/runtime/compat */ -/******/ +/******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";/************************************************************************/ -/******/ +/******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module is referenced by other modules so it can't be inlined /******/ var __webpack_exports__ = __nccwpck_require__(3109); /******/ module.exports = __webpack_exports__; -/******/ +/******/ /******/ })() -; +; \ No newline at end of file