Skip to content

Commit

Permalink
wip: adds tag support
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Sep 4, 2024
1 parent 81cc097 commit afbd217
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
8 changes: 7 additions & 1 deletion blueprint/schema/_embed/schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ package schema
}
version: "1.0"
#Tagging: {
// Strategy contains the tagging strategy to use.
// Aliases contains the aliases to use for git tags.
// +optional
aliases?: {
[string]: string
} @go(Aliases,map[string]string)

// Strategy contains the tagging strategy to use for containers.
strategy: "commit" @go(Strategy)
}

Expand Down
6 changes: 5 additions & 1 deletion blueprint/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ type Secret struct {
}

type Tagging struct {
// Strategy contains the tagging strategy to use.
// Aliases contains the aliases to use for git tags.
// +optional
Aliases map[string]string `json:"aliases"`

// Strategy contains the tagging strategy to use for containers.
Strategy string `json:"strategy"`
}

Expand Down
6 changes: 5 additions & 1 deletion blueprint/schema/schema_go_gen.cue
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ package schema
}

#Tagging: {
// Strategy contains the tagging strategy to use.
// Aliases contains the aliases to use for git tags.
// +optional
aliases?: {[string]: string} @go(Aliases,map[string]string)

// Strategy contains the tagging strategy to use for containers.
strategy: string @go(Strategy)
}

Expand Down
58 changes: 50 additions & 8 deletions forge/actions/publish/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,49 @@ async function run() {
return;
}

const tags = [];
const gitTag = parseGitTag(process.env.GITHUB_REF);
if (gitTag !== "") {
core.info(`Detected Git tag: ${gitTag}`);

parts = gitTag.split("/");
if (parts.lenth > 1) {
const path = parts.slice(0, -1).join("/");
const tag = parts[parts.length - 1];
const projectCleaned = project.trimStart(".").trimEnd("/");

core.info(`Detected mono-repo tag path=${path} tag=${tag}`);
if (Object.keys(blueprint?.global?.ci?.tagging?.aliases) !== undefined) {
if (blueprint.global.ci.tagging.aliases[path] === projectCleaned) {
tags.push(tag);
}
} else if (path === projectCleaned) {
tags.push(tag);
} else {
core.info(`Skipping tag as it does not match the project path`);
}
} else {
core.info("Detected non mono-repo tag. Using tag as is.");
tags.push(gitTag);
}
} else {
core.info("No Git tag detected");
}

const container = blueprint.project.container;
const registries = blueprint.global.ci.registries;
const tag = getTag(blueprint.global.ci.tagging.strategy);

core.info(`Ref: ${process.env.GITHUB_REF}`);
tags.push(getTag(blueprint.global.ci.tagging.strategy));

for (const registry of registries) {
const taggedImage = `${registry}/${container}:${tag}`;
for (const tag of tags) {
const taggedImage = `${registry}/${container}:${tag}`;

core.info(`Tagging image ${image} as ${taggedImage}`);
await tagImage(image, taggedImage);
core.info(`Tagging image ${image} as ${taggedImage}`);
await tagImage(image, taggedImage);

core.info(`Pushing image ${taggedImage}`);
//await pushImage(taggedImage);
core.info(`Pushing image ${taggedImage}`);
//await pushImage(taggedImage);
}
}
} catch (error) {
core.setFailed(error.message);
Expand Down Expand Up @@ -106,6 +135,19 @@ async function imageExists(name) {
return result === 0;
}

/**
* Parse a Git tag from a ref. If the ref is not a tag, an empty string is returned.
* @param {string} ref The ref to parse
* @returns {string} The tag or an empty string
*/
function parseGitTag(ref) {
if (ref.startsWith("refs/tags/")) {
return ref.slice(10);
} else {
return "";
}
}

/***
* Push a Docker image to a registry
*/
Expand Down

0 comments on commit afbd217

Please sign in to comment.