Files
publish-immutable-action/src/main.ts
T

126 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-11-17 20:04:42 +00:00
import * as core from '@actions/core'
import * as github from '@actions/github'
import * as fsHelper from './fs-helper'
import * as ociContainer from './oci-container'
import * as ghcr from './ghcr-client'
2024-01-29 20:31:00 +00:00
import * as api from './api-client'
2023-11-17 20:04:42 +00:00
import semver from 'semver'
/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
2024-01-25 08:34:02 -08:00
export async function run(pathInput: string): Promise<void> {
2023-11-17 21:31:12 +00:00
const tmpDirs: string[] = []
2023-11-17 20:04:42 +00:00
try {
const repository: string = process.env.GITHUB_REPOSITORY || ''
if (repository === '') {
core.setFailed(`Could not find Repository.`)
return
}
2024-01-26 13:03:16 -05:00
2024-01-29 20:31:00 +00:00
const token: string = process.env.TOKEN || ''
const sourceCommit: string = process.env.GITHUB_SHA || ''
if (token === '') {
core.setFailed(`Could not find GITHUB_TOKEN.`)
return
}
if (sourceCommit === '') {
core.setFailed(`Could not find source commit.`)
2023-11-17 20:04:42 +00:00
return
}
2024-01-26 13:03:16 -05:00
2024-01-29 20:31:00 +00:00
const semanticVersion = parseSourceSemanticVersion()
2023-11-17 20:04:42 +00:00
2024-01-29 20:31:00 +00:00
// Create a temporary directory to stage files for packaging in archives
const stagedActionFilesDir = fsHelper.createTempDir()
tmpDirs.push(stagedActionFilesDir)
fsHelper.stageActionFiles('.', stagedActionFilesDir)
2024-01-26 13:03:16 -05:00
2023-11-17 20:04:42 +00:00
// Create a temporary directory to store the archives
2023-11-17 21:31:12 +00:00
const archiveDir = fsHelper.createTempDir()
tmpDirs.push(archiveDir)
2024-01-29 20:31:00 +00:00
const archives = await fsHelper.createArchives(
stagedActionFilesDir,
archiveDir
)
2023-11-17 20:04:42 +00:00
2024-01-29 20:31:00 +00:00
const { repoId, ownerId } = await api.getRepositoryMetadata(
repository,
token
)
2023-11-17 20:04:42 +00:00
const manifest = ociContainer.createActionPackageManifest(
archives.tarFile,
archives.zipFile,
repository,
2024-01-29 20:31:00 +00:00
repoId,
ownerId,
sourceCommit,
semanticVersion.raw,
2023-11-17 20:04:42 +00:00
new Date()
)
2024-01-29 20:31:00 +00:00
const containerRegistryURL = await api.getContainerRegistryURL()
console.log(`Container registry URL: ${containerRegistryURL}`)
2024-01-25 08:34:02 -08:00
2024-01-29 20:31:00 +00:00
const { packageURL, manifestDigest } = await ghcr.publishOCIArtifact(
2023-11-17 20:04:42 +00:00
token,
2024-01-29 20:31:00 +00:00
containerRegistryURL,
2023-11-17 20:04:42 +00:00
repository,
2024-01-29 20:31:00 +00:00
semanticVersion.raw,
2023-11-17 20:04:42 +00:00
archives.zipFile,
archives.tarFile,
manifest,
true
)
core.setOutput('package-url', packageURL.toString())
core.setOutput('package-manifest', JSON.stringify(manifest))
2024-01-29 16:44:15 -05:00
core.setOutput('package-manifest-sha', manifestDigest)
2023-11-17 20:04:42 +00:00
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)
2023-11-17 20:04:42 +00:00
} finally {
2023-11-17 21:31:12 +00:00
// Clean up any temporary directories that exist
for (const tmpDir of tmpDirs) {
if (tmpDir !== '') {
fsHelper.removeDir(tmpDir)
}
2023-11-17 20:04:42 +00:00
}
}
}
2024-01-29 20:31:00 +00:00
// This action can be triggered by release events or tag push events.
// In each case, the source event should produce a Semantic Version compliant tag representing the code to be packaged.
function parseSourceSemanticVersion(): semver.SemVer {
const event = github.context.eventName
var semverTag = ''
// Grab the raw tag
if (event === 'release') semverTag = github.context.payload.release.tag_name
else if (event === 'push' && github.context.ref.startsWith('refs/tags/')) {
semverTag = github.context.ref.replace(/^refs\/tags\//, '')
} else {
throw new Error(
`This action can only be triggered by release events or tag push events.`
)
}
if (semverTag === '') {
throw new Error(
`Could not find a Semantic Version tag in the event payload.`
)
}
const semanticVersion = semver.parse(semverTag.replace(/^v/, ''))
if (!semanticVersion) {
throw new Error(
`${semverTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.`
)
}
return semanticVersion
}