Skip to content

Commit

Permalink
Allow prefix to be a whitespace/comma separated list of prefixes
Browse files Browse the repository at this point in the history
This project enables pushing the same image to multiple tags, and with
this feature it can enable pushing the same image to multiple
destinations with multiple tags.

A use case for this is if we want to push to both Docker Hub and Quay.io
for example, then prefix could be `"user/repo,quay.io/user/repo"`.
  • Loading branch information
consideRatio committed Nov 23, 2023
1 parent 3712f66 commit 975bd35
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The GitHub action's only output is named `tags` and is a JSON formatted list. Se

## Optional input parameters

- `prefix`: A string that each returned tag should be prefixed with, for example to tag a Docker container set this to `user/repository:`.
- `prefix`: A string that each returned tag should be prefixed with, for example to tag a Docker container set this to `user/repository:`. This is allowed to be a comma/whitespace separated list to tag multiple images, for example `user/repository:,quay.io/user/repository:`.
- `defaultTag`: If the tag output would be empty return this tag instead.
This can be useful for running a workflow in pull requests where no suitable git references are present.
`prefix` is _not_ automatically added.
Expand Down
32 changes: 31 additions & 1 deletion __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ test("No ref use default", async () => {
expect(tags).toEqual(["default-tag"]);
});

test("Prefix", async () => {
test("Single prefix", async () => {
tagInterceptor.reply(200, [
{
name: "0.0.1",
Expand All @@ -279,6 +279,36 @@ test("Prefix", async () => {
]);
});

test("Multiple prefix", async () => {
tagInterceptor.reply(200, [
{
name: "0.0.1",
},
]);
const tags = await calculateTags({
token: "TOKEN",
owner: "owner",
repo: "repo",
ref: "refs/tags/0.0.1",
// test use of different whitespace separators and a combination of both
prefix: "prefix1:\tprefix2:\n, prefix3:",
});
expect(tags).toEqual([
"prefix1:0.0.1",
"prefix2:0.0.1",
"prefix3:0.0.1",
"prefix1:0.0",
"prefix2:0.0",
"prefix3:0.0",
"prefix1:0",
"prefix2:0",
"prefix3:0",
"prefix1:latest",
"prefix2:latest",
"prefix3:latest",
]);
});

test("Branch", async () => {
const tags = await calculateTags({
token: "TOKEN",
Expand Down
33 changes: 24 additions & 9 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

39 changes: 29 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ function checkAgainstRegex(name, regexAllowed) {
return re.test(name);
}

function expandPrefix(prefix, tag) {
// Adds one or more prefixes to a tag, where prefix could be a single prefix
// or a comma/whitespace separated list of prefixes.
if (!prefix) {
return [tag];
}

let rv = [];
let prefixes = prefix.split(/\s|,/).filter(Boolean);
prefixes.forEach((p) => rv.push(`${p}${tag}`));
return rv;
}

async function calculateTags({
token,
owner,
Expand Down Expand Up @@ -59,7 +72,7 @@ async function calculateTags({
}
return [];
}
return [`${prefix}${branch}`];
return expandPrefix(prefix, branch);
}
if (!ref.startsWith("refs/tags/")) {
throw new Error(`Not a tag or branch: ${ref}`);
Expand All @@ -77,7 +90,7 @@ async function calculateTags({
});
if (!supportedPrerelease(current.prerelease)) {
core.warning(`Tag prerelease ${currentTag} is not supported`);
return [`${prefix}${currentTag}`];
return expandPrefix(prefix, currentTag);
}

const octokit = github.getOctokit(token);
Expand All @@ -103,7 +116,7 @@ async function calculateTags({

let outputTags = [];
if (current.prerelease.length) {
outputTags.push(`${prefix}${current.version}`);
outputTags.push(...expandPrefix(prefix, current.version));

// return without additional output tags if we got an outdated build number
const similarTags = tags.filter(
Expand All @@ -115,26 +128,32 @@ async function calculateTags({
}

outputTags.push(
`${prefix}${current.major}.${current.minor}.${current.patch}`,
...expandPrefix(prefix, `${current.major}.${current.minor}.${current.patch}`),
);

core.debug(semver.compare(current, tags[0]) >= 0);
if (
!tags.length ||
semver.compare(current.toString().split("-")[0], tags[0]) >= 0
) {
outputTags.push(`${prefix}${current.major}.${current.minor}`);
outputTags.push(`${prefix}${current.major}`);
outputTags.push(`${prefix}latest`);
outputTags.push(
...expandPrefix(prefix, `${current.major}.${current.minor}`),
);
outputTags.push(...expandPrefix(prefix, `${current.major}`));
outputTags.push(...expandPrefix(prefix, "latest"));
} else if (
semver.compare(current.toString().split("-")[0], majorTags[0]) >= 0
) {
outputTags.push(`${prefix}${current.major}.${current.minor}`);
outputTags.push(`${prefix}${current.major}`);
outputTags.push(
...expandPrefix(prefix, `${current.major}.${current.minor}`),
);
outputTags.push(...expandPrefix(prefix, `${current.major}`));
} else if (
semver.compare(current.toString().split("-")[0], minorTags[0]) >= 0
) {
outputTags.push(`${prefix}${current.major}.${current.minor}`);
outputTags.push(
...expandPrefix(prefix, `${current.major}.${current.minor}`),
);
}
core.debug(`outputTags: ${outputTags}`);
return outputTags;
Expand Down

0 comments on commit 975bd35

Please sign in to comment.