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'
|
|
|
|
|
import semver from 'semver'
|
2024-01-25 08:34:02 -08:00
|
|
|
import crypto from 'crypto'
|
2023-11-17 20:04:42 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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 {
|
|
|
|
|
// Parse and validate Actions execution context, including the repository name, release name and event type
|
|
|
|
|
const repository: string = process.env.GITHUB_REPOSITORY || ''
|
2024-01-25 08:34:02 -08:00
|
|
|
|
2023-11-17 20:04:42 +00:00
|
|
|
if (repository === '') {
|
|
|
|
|
core.setFailed(`Could not find Repository.`)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-01-26 13:03:16 -05:00
|
|
|
|
2023-11-17 20:04:42 +00:00
|
|
|
if (github.context.eventName !== 'release') {
|
|
|
|
|
core.setFailed('Please ensure you have the workflow trigger as release.')
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-01-26 13:03:16 -05:00
|
|
|
|
2023-11-17 20:04:42 +00:00
|
|
|
const releaseId: string = github.context.payload.release.id
|
|
|
|
|
const releaseTag: string = github.context.payload.release.tag_name
|
|
|
|
|
// Strip any leading 'v' from the tag in case the release format is e.g. 'v1.0.0' as recommended by GitHub docs
|
|
|
|
|
// https://docs.github.com/en/actions/creating-actions/releasing-and-maintaining-actions
|
2023-11-17 21:31:12 +00:00
|
|
|
const targetVersion = semver.parse(releaseTag.replace(/^v/, ''))
|
2023-11-17 20:04:42 +00:00
|
|
|
if (!targetVersion) {
|
|
|
|
|
core.setFailed(
|
|
|
|
|
`${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.`
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 15:24:45 -05:00
|
|
|
const token: string = process.env.TOKEN!
|
|
|
|
|
|
2024-01-26 16:00:36 -05:00
|
|
|
const { consolidatedPath, needToCleanUpDir } =
|
|
|
|
|
fsHelper.getConsolidatedDirectory(pathInput)
|
|
|
|
|
if (needToCleanUpDir) {
|
|
|
|
|
tmpDirs.push(consolidatedPath)
|
2023-11-17 20:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-26 16:00:36 -05:00
|
|
|
if (!fsHelper.isActionRepo(consolidatedPath)) {
|
2024-01-26 13:03:16 -05:00
|
|
|
core.setFailed(
|
|
|
|
|
'action.y(a)ml not found. Action packages can be created only for action repositories.'
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2023-11-17 20:04:42 +00:00
|
|
|
|
2024-01-26 16:00:36 -05:00
|
|
|
const archives = await fsHelper.createArchives(consolidatedPath, archiveDir)
|
2023-11-17 20:04:42 +00:00
|
|
|
|
|
|
|
|
const manifest = ociContainer.createActionPackageManifest(
|
|
|
|
|
archives.tarFile,
|
|
|
|
|
archives.zipFile,
|
|
|
|
|
repository,
|
|
|
|
|
targetVersion.raw,
|
|
|
|
|
new Date()
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-25 08:34:02 -08:00
|
|
|
// Generate SHA-256 hash of the manifest
|
|
|
|
|
const manifestSHA = crypto.createHash('sha256')
|
|
|
|
|
const manifestHash = manifestSHA
|
|
|
|
|
.update(JSON.stringify(manifest))
|
|
|
|
|
.digest('hex')
|
|
|
|
|
|
2024-01-26 13:03:16 -05:00
|
|
|
const response = await fetch(
|
2024-01-26 14:49:48 -05:00
|
|
|
`${process.env.GITHUB_API_URL}/packages/container-registry-url`
|
2024-01-26 13:03:16 -05:00
|
|
|
)
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to fetch status page: ${response.statusText}`)
|
|
|
|
|
}
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
const registryURL: URL = new URL(data.url)
|
|
|
|
|
console.log(`Container registry URL: ${registryURL}`)
|
|
|
|
|
|
2023-11-17 21:31:12 +00:00
|
|
|
const packageURL = await ghcr.publishOCIArtifact(
|
2023-11-17 20:04:42 +00:00
|
|
|
token,
|
|
|
|
|
registryURL,
|
|
|
|
|
repository,
|
|
|
|
|
releaseId.toString(),
|
|
|
|
|
targetVersion.raw,
|
|
|
|
|
archives.zipFile,
|
|
|
|
|
archives.tarFile,
|
|
|
|
|
manifest,
|
|
|
|
|
true
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
core.setOutput('package-url', packageURL.toString())
|
|
|
|
|
core.setOutput('package-manifest', JSON.stringify(manifest))
|
2024-01-25 08:34:02 -08:00
|
|
|
core.setOutput('package-manifest-sha', `sha256:${manifestHash}`)
|
2023-11-17 20:04:42 +00:00
|
|
|
} catch (error) {
|
|
|
|
|
// Fail the workflow run if an error occurs
|
2024-01-26 14:49:48 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|