From afbd217e1704ba2b51dfe0bc6d1c321b06309baa Mon Sep 17 00:00:00 2001 From: Joshua Gilman Date: Wed, 4 Sep 2024 15:08:54 -0400 Subject: [PATCH] wip: adds tag support --- blueprint/schema/_embed/schema.cue | 8 ++++- blueprint/schema/schema.go | 6 +++- blueprint/schema/schema_go_gen.cue | 6 +++- forge/actions/publish/src/main.js | 58 +++++++++++++++++++++++++----- 4 files changed, 67 insertions(+), 11 deletions(-) diff --git a/blueprint/schema/_embed/schema.cue b/blueprint/schema/_embed/schema.cue index 7195dca2..0703005f 100644 --- a/blueprint/schema/_embed/schema.cue +++ b/blueprint/schema/_embed/schema.cue @@ -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) } diff --git a/blueprint/schema/schema.go b/blueprint/schema/schema.go index 1ad1104f..e18afafd 100644 --- a/blueprint/schema/schema.go +++ b/blueprint/schema/schema.go @@ -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"` } diff --git a/blueprint/schema/schema_go_gen.cue b/blueprint/schema/schema_go_gen.cue index d5cbd536..7de5cce4 100644 --- a/blueprint/schema/schema_go_gen.cue +++ b/blueprint/schema/schema_go_gen.cue @@ -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) } diff --git a/forge/actions/publish/src/main.js b/forge/actions/publish/src/main.js index 5a51e433..3d6a1505 100644 --- a/forge/actions/publish/src/main.js +++ b/forge/actions/publish/src/main.js @@ -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); @@ -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 */