Tying up loose ends (#54)

* various qol updates to publish action

* review comments and run bundle
This commit is contained in:
Conor Sloan
2024-02-02 13:02:14 -05:00
committed by Edwin Sirko
parent 3c4259bfdd
commit 1f47b19ed3
23 changed files with 811 additions and 514 deletions
Generated Vendored
+43 -36
View File
@@ -32,63 +32,47 @@ const github = __importStar(require("@actions/github"));
const fsHelper = __importStar(require("./fs-helper"));
const ociContainer = __importStar(require("./oci-container"));
const ghcr = __importStar(require("./ghcr-client"));
const api = __importStar(require("./api-client"));
const semver_1 = __importDefault(require("semver"));
/**
* The main function for the action.
* @returns {Promise<void>} Resolves when the action is complete.
*/
async function run() {
async function run(pathInput) {
const tmpDirs = [];
try {
// Parse and validate Actions execution context, including the repository name, release name and event type
const repository = process.env.GITHUB_REPOSITORY || '';
if (repository === '') {
core.setFailed(`Could not find Repository.`);
return;
}
if (github.context.eventName !== 'release') {
core.setFailed('Please ensure you have the workflow trigger as release.');
const token = process.env.TOKEN || '';
const sourceCommit = process.env.GITHUB_SHA || '';
if (token === '') {
core.setFailed(`Could not find source commit.`);
return;
}
const releaseId = github.context.payload.release.id;
const releaseTag = 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
const targetVersion = semver_1.default.parse(releaseTag.replace(/^v/, ''));
if (!targetVersion) {
// TODO: We may want to limit semvers to only x.x.x, without the pre-release tags, but for now we'll allow them.
core.setFailed(`${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.`);
if (sourceCommit === '') {
core.setFailed(`Could not find source commit.`);
return;
}
// Gather & validate user inputs
const token = core.getInput('token');
const registryURL = new URL('https://ghcr.io/'); // TODO: Should this be dynamic? Maybe an API endpoint to grab the registry for GHES/proxima purposes.
console.log(core.getInput('registry'));
console.log(`registryURL: ${registryURL}`);
// Paths to be included in the OCI image
const paths = core.getInput('path').split(' ');
let path = '';
if (paths.length === 1 && fsHelper.isDirectory(paths[0])) {
// If the path is a single directory, we can skip the bundling step
path = paths[0];
}
else {
// Otherwise, we need to bundle the files & folders into a temporary directory
const bundleDir = fsHelper.createTempDir();
tmpDirs.push(bundleDir);
path = fsHelper.bundleFilesintoDirectory(paths, bundleDir);
}
const semanticVersion = parseSourceSemanticVersion();
// Create a temporary directory to stage files for packaging in archives
const stagedActionFilesDir = fsHelper.createTempDir();
tmpDirs.push(stagedActionFilesDir);
fsHelper.stageActionFiles(".", stagedActionFilesDir);
// Create a temporary directory to store the archives
const archiveDir = fsHelper.createTempDir();
tmpDirs.push(archiveDir);
const archives = await fsHelper.createArchives(path, archiveDir);
const manifest = ociContainer.createActionPackageManifest(archives.tarFile, archives.zipFile, repository, targetVersion.raw, new Date());
const packageURL = await ghcr.publishOCIArtifact(token, registryURL, repository, releaseId.toString(), targetVersion.raw, archives.zipFile, archives.tarFile, manifest, true);
const archives = await fsHelper.createArchives(stagedActionFilesDir, archiveDir);
const { repoId, ownerId } = await api.getRepositoryMetadata(repository, token);
const manifest = ociContainer.createActionPackageManifest(archives.tarFile, archives.zipFile, repository, repoId, ownerId, sourceCommit, semanticVersion.raw, new Date());
const containerRegistryURL = await api.getContainerRegistryURL();
console.log(`Container registry URL: ${containerRegistryURL}`);
const { packageURL, manifestDigest } = await ghcr.publishOCIArtifact(token, containerRegistryURL, repository, semanticVersion.raw, archives.zipFile, archives.tarFile, manifest, true);
core.setOutput('package-url', packageURL.toString());
// TODO: We might need to do some attestation stuff here, but unsure how to integrate it yet.
// We might need to return the manifest JSON from the Action and link it to another action,
// or we might be able to make an API call here. It's unclear at this point.
core.setOutput('package-manifest', JSON.stringify(manifest));
core.setOutput('package-manifest-sha', `sha256:${manifestDigest}`);
}
catch (error) {
// Fail the workflow run if an error occurs
@@ -105,4 +89,27 @@ async function run() {
}
}
exports.run = run;
// 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() {
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_1.default.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;
}
//# sourceMappingURL=main.js.map