From 320acb84148e42c33d7314a4232987b595c58eb4 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sun, 16 Aug 2020 01:50:46 +0200 Subject: [PATCH] Parse image reference before push Signed-off-by: CrazyMax --- dist/index.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++- src/docker.ts | 30 ++++++++++++++++++++++++++++++ src/main.ts | 9 +++++++-- 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/docker.ts diff --git a/dist/index.js b/dist/index.js index e62b77d..669bedb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1004,6 +1004,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const os = __importStar(__webpack_require__(87)); const buildx = __importStar(__webpack_require__(982)); const context_helper_1 = __webpack_require__(338); +const docker_1 = __webpack_require__(231); const core = __importStar(__webpack_require__(470)); const exec = __importStar(__webpack_require__(986)); function run() { @@ -1081,7 +1082,12 @@ function run() { if (!buildxEnabled && inputs.push) { let pushRepos = []; yield asyncForEach(inputs.tags, (tag) => __awaiter(this, void 0, void 0, function* () { - const repo = tag.split(':', -1)[0]; + const img = yield docker_1.parseImage(tag); + if (!img) { + core.warning(`Cannot parse image reference ${tag}`); + return; + } + const repo = `${img.registry}${img.namespace}${img.repository}`; if (!pushRepos.includes(repo)) { pushRepos.push(repo); core.info(`⬆️ Pushing ${repo}...`); @@ -1105,6 +1111,46 @@ run(); /***/ }), +/***/ 231: +/***/ (function(__unusedmodule, exports) { + +"use strict"; + +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.parseImage = void 0; +exports.parseImage = (image) => __awaiter(void 0, void 0, void 0, function* () { + const match = image.match(/^(?:([^\/]+)\/)?(?:([^\/]+)\/)?([^@:\/]+)(?:[@:](.+))?$/); + if (!match) { + return; + } + let res = { + registry: match[1], + namespace: match[2], + repository: match[3], + tag: match[4] + }; + if (!res.namespace && res.registry && !/[:.]/.test(res.registry)) { + res.namespace = res.registry; + res.registry = undefined; + } + res.registry = res.registry ? `${res.registry}/` : ''; + res.namespace = res.namespace && res.namespace !== 'library' ? `${res.namespace}/` : ''; + res.tag = res.tag && res.tag !== 'latest' ? `:${res.tag}` : ''; + return res; +}); +//# sourceMappingURL=docker.js.map + +/***/ }), + /***/ 338: /***/ (function(__unusedmodule, exports, __webpack_require__) { diff --git a/src/docker.ts b/src/docker.ts new file mode 100644 index 0000000..64f747b --- /dev/null +++ b/src/docker.ts @@ -0,0 +1,30 @@ +export interface Image { + registry?: string; + namespace?: string; + repository: string; + tag?: string; +} + +export const parseImage = async (image: string): Promise => { + const match = image.match(/^(?:([^\/]+)\/)?(?:([^\/]+)\/)?([^@:\/]+)(?:[@:](.+))?$/); + if (!match) { + return; + } + + let res: Image = { + registry: match[1], + namespace: match[2], + repository: match[3], + tag: match[4] + }; + + if (!res.namespace && res.registry && !/[:.]/.test(res.registry)) { + res.namespace = res.registry; + res.registry = undefined; + } + + res.registry = res.registry ? `${res.registry}/` : ''; + res.namespace = res.namespace && res.namespace !== 'library' ? `${res.namespace}/` : ''; + res.tag = res.tag && res.tag !== 'latest' ? `:${res.tag}` : ''; + return res; +}; diff --git a/src/main.ts b/src/main.ts index 9c58940..d783888 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import * as os from 'os'; import * as buildx from './buildx'; import {Inputs, loadInputs, mustBuildx} from './context-helper'; +import {Image, parseImage} from './docker'; import * as core from '@actions/core'; import * as exec from '@actions/exec'; @@ -84,10 +85,14 @@ async function run(): Promise { if (!buildxEnabled && inputs.push) { let pushRepos: Array = []; await asyncForEach(inputs.tags, async tag => { - const repo = tag.split(':', -1)[0]; + const img: Image | undefined = await parseImage(tag); + if (!img) { + core.warning(`Cannot parse image reference ${tag}`); + return; + } + const repo: string = `${img.registry}${img.namespace}${img.repository}`; if (!pushRepos.includes(repo)) { pushRepos.push(repo); - core.info(`⬆️ Pushing ${repo}...`); await exec.exec('docker', ['push', repo]); }