Skip to content

Commit

Permalink
Parse image reference before push
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
CrazyMax committed Aug 15, 2020
1 parent b15ad02 commit 320acb8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
48 changes: 47 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface Image {
registry?: string;
namespace?: string;
repository: string;
tag?: string;
}

export const parseImage = async (image: string): Promise<Image | undefined> => {
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;
};
9 changes: 7 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -84,10 +85,14 @@ async function run(): Promise<void> {
if (!buildxEnabled && inputs.push) {
let pushRepos: Array<string> = [];
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]);
}
Expand Down

0 comments on commit 320acb8

Please sign in to comment.