diff --git a/.gitignore b/.gitignore index fc7c3c1..139178e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ node_modules +dist + # Rest pulled from https://github.com/github/gitignore/blob/master/Node.gitignore # Logs logs diff --git a/src/main.ts b/src/main.ts index d40e865..3614449 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,7 @@ import * as core from '@actions/core'; import { generateFeaturesDocumentation, generateTemplateDocumentation } from './generateDocs'; -import { ensureDevcontainerCliPresent, getGitHubMetadata } from './utils'; +import { addRepoTagForPublishedTag, ensureDevcontainerCliPresent, getGitHubMetadata } from './utils'; import * as exec from '@actions/exec'; async function run(): Promise { @@ -52,19 +52,43 @@ async function run(): Promise { } if (shouldPublishFeatures) { - core.info('Publishing features...'); - if (!(await publish('feature', featuresBasePath, featuresOciRegistry, featuresNamespace, cliDebugMode))) { - core.setFailed('(!) Failed to publish features.'); + core.info('Publishing Features...'); + const publishedFeatures = await publish('feature', featuresBasePath, featuresOciRegistry, featuresNamespace, cliDebugMode); + if (!publishedFeatures) { + core.setFailed('(!) Failed to publish Features.'); return; } + + // Add repo tags at the current commit. + for (const featureId in publishedFeatures) { + const versions = publishedFeatures[featureId]; + for (const version of versions) { + if (!(await addRepoTagForPublishedTag('feature', featureId, version))) { + core.setFailed('(!) Failed to add repo tag for a Feature release.'); + continue; + } + } + } } if (shouldPublishTemplates) { - core.info('Publishing templates...'); - if (!(await publish('template', templatesBasePath, templatesOciRegistry, templatesNamespace, cliDebugMode))) { - core.setFailed('(!) Failed to publish templates.'); + core.info('Publishing Templates...'); + const publishedTemplates = await publish('template', templatesBasePath, templatesOciRegistry, templatesNamespace, cliDebugMode); + if (!publishedTemplates) { + core.setFailed('(!) Failed to publish Templates.'); return; } + + // Add repo tags at the current commit. + for (const templateId in publishedTemplates) { + const versions = publishedTemplates[templateId]; + for (const version of versions) { + if (!(await addRepoTagForPublishedTag('template', templateId, version))) { + core.setFailed('(!) Failed to add repo tag for a Template release.'); + continue; + } + } + } } // -- Generate Documentation @@ -80,11 +104,17 @@ async function run(): Promise { } } -async function publish(collectionType: string, basePath: string, ociRegistry: string, namespace: string, cliDebugMode = false): Promise { +async function publish( + collectionType: string, + basePath: string, + ociRegistry: string, + namespace: string, + cliDebugMode = false +): Promise<{ [featureId: string]: [string] } | undefined> { // Ensures we have the devcontainer CLI installed. if (!(await ensureDevcontainerCliPresent(cliDebugMode))) { core.setFailed('Failed to install devcontainer CLI'); - return false; + return; } try { @@ -97,10 +127,11 @@ async function publish(collectionType: string, basePath: string, ociRegistry: st // Fails on non-zero exit code from the invoked process const res = await exec.getExecOutput(cmd, args, {}); - return res.exitCode === 0; + const result = JSON.parse(res.stdout); + return result; } catch (err: any) { core.setFailed(err?.message); - return false; + return; } } diff --git a/src/utils.ts b/src/utils.ts index 744eb2e..8e3cc85 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -51,6 +51,36 @@ export async function isDevcontainerCliAvailable(cliDebugMode = false): Promise< } } +export async function addRepoTagForPublishedTag(type: string, id: string, version: string): Promise { + const octokit = github.getOctokit(process.env.GITHUB_TOKEN || ''); + const tag = `${type}_${id}_v${version}`; + core.info(`Adding repo tag '${tag}'...`); + + try { + await octokit.rest.git.createRef({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + ref: `refs/tags/${tag}`, + sha: github.context.sha + }); + + await octokit.rest.git.createTag({ + owner: github.context.repo.owner, + repo: github.context.repo.repo, + tag, + message: `${tag}`, + object: github.context.sha, + type: 'commit' + }); + } catch (err) { + core.error(`Failed to add tag '${tag}' to repo: ${err}`); + return false; + } + + core.info(`Tag '${tag}' added.`); + return true; +} + export async function ensureDevcontainerCliPresent(cliDebugMode = false): Promise { if (await isDevcontainerCliAvailable(cliDebugMode)) { core.info('devcontainer CLI is already installed');