From f0a3f907d98de920dcd15506c5b3579a5a2d2daa Mon Sep 17 00:00:00 2001 From: Edwin Sirko Date: Fri, 26 Jan 2024 13:03:16 -0500 Subject: [PATCH 1/6] generate-new-version fixes --- generate-new-version.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/generate-new-version.sh b/generate-new-version.sh index 703e075..fbe0e37 100755 --- a/generate-new-version.sh +++ b/generate-new-version.sh @@ -17,9 +17,11 @@ fi echo "Generating new version $VERSION with message $MESSAGE" -sed -i '' -E 's/ddivad195\/publish-action-package\/package-and-publish.*$/ddivad195\/publish-action-package\/package-and-publish@'$VERSION'/g' action.yml +#sed -i '' -E 's/ddivad195\/publish-action-package\/package-and-publish.*$/ddivad195\/publish-action-package\/package-and-publish@'$VERSION'/g' action.yml npm run bundle git add . git commit -m "$VERSION: $MESSAGE" git push +git tag $VERSION +git push origin $VERSION gh release create --repo ddivad195/publish-action-package --title $VERSION --notes $VERSION $VERSION \ No newline at end of file From d264ea08991f0e5111aa1998679cb29c0a0614dd Mon Sep 17 00:00:00 2001 From: Edwin Sirko Date: Fri, 26 Jan 2024 13:05:35 -0500 Subject: [PATCH 2/6] now properly getting the CR URL --- src/main.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main.ts b/src/main.ts index fa0322c..8a6a1d2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -39,12 +39,9 @@ export async function run(pathInput: string): Promise { } const token: string = process.env.TOKEN! - // TODO: once https://github.com/github/github/pull/309384 goes in, we can switch to the actual endpoint - //const response = await fetch( - // process.env.GITHUB_API_URL + '/packages/container-registry-url' - //) + const response = await fetch( - 'http://echo.jsontest.com/url/https:ghcr.io' // for testing locally. Remove the slashes, they will be reintroduced when forming the URL object below + process.env.GITHUB_API_URL + '/packages/container-registry-url' ) if (!response.ok) { throw new Error(`Failed to fetch status page: ${response.statusText}`) From 55e582b23e1f50a9d5178e4fdde26558311b31a7 Mon Sep 17 00:00:00 2001 From: Edwin Sirko Date: Fri, 26 Jan 2024 13:07:11 -0500 Subject: [PATCH 3/6] resolved a TODO item about semver --- src/main.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 8a6a1d2..7a661ec 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,7 +31,6 @@ export async function run(pathInput: string): Promise { // https://docs.github.com/en/actions/creating-actions/releasing-and-maintaining-actions const targetVersion = semver.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.` ) From 4ed2e10e92a8fac37e25a1da7e7d7dc1ed1ae28a Mon Sep 17 00:00:00 2001 From: Edwin Sirko Date: Fri, 26 Jan 2024 13:07:57 -0500 Subject: [PATCH 4/6] isActionRepo() --- __tests__/fs-helper.test.ts | 28 ++++++++++++++++++++++++++++ src/fs-helper.ts | 9 +++++++++ src/main.ts | 7 +++++++ 3 files changed, 44 insertions(+) diff --git a/__tests__/fs-helper.test.ts b/__tests__/fs-helper.test.ts index 641d9f5..b86174a 100644 --- a/__tests__/fs-helper.test.ts +++ b/__tests__/fs-helper.test.ts @@ -1,5 +1,6 @@ import * as fsHelper from '../src/fs-helper' import * as fs from 'fs' +import * as path from 'path' import * as os from 'os' import { execSync } from 'child_process' @@ -131,6 +132,33 @@ describe('isDirectory', () => { }) }) +describe('isActionRepo', () => { + let stagingDir: string + + beforeEach(() => { + stagingDir = fsHelper.createTempDir() + }) + + afterEach(() => { + fs.rmSync(stagingDir, { recursive: true }) + }) + + it('returns true if action.yml exists at the root', () => { + fs.writeFileSync(path.join(stagingDir, `action.yml`), fileContent) + expect(fsHelper.isActionRepo(stagingDir)).toEqual(true) + }) + + it('returns true if action.yaml exists at the root', () => { + fs.writeFileSync(path.join(stagingDir, `action.yaml`), fileContent) + expect(fsHelper.isActionRepo(stagingDir)).toEqual(true) + }) + + it('returns false if action.y(a)ml doesn\'t exist at the root', () => { + fs.writeFileSync(path.join(stagingDir, `action.yaaml`), fileContent) + expect(fsHelper.isActionRepo(stagingDir)).toEqual(false) + }) +}) + describe('readFileContents', () => { let dir: string diff --git a/src/fs-helper.ts b/src/fs-helper.ts index 2245132..a191aba 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -91,6 +91,14 @@ export function isDirectory(dirPath: string): boolean { return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory() } +export function isActionRepo(stagingDir: string): boolean { + return ( + fs.existsSync(path.join(stagingDir, 'action.yml')) || + fs.existsSync(path.join(stagingDir, 'action.yaml')) + ) +} + + export function readFileContents(filePath: string): Buffer { return fs.readFileSync(filePath) } @@ -116,6 +124,7 @@ export function bundleFilesintoDirectory( return targetDir } + // Converts a file path to a filemetadata object by querying the fs for relevant metadata. async function fileMetadata(filePath: string): Promise { const stats = fs.statSync(filePath) diff --git a/src/main.ts b/src/main.ts index 7a661ec..2c5c6f0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -65,6 +65,13 @@ export async function run(pathInput: string): Promise { path = fsHelper.bundleFilesintoDirectory(paths, bundleDir) } + if (!fsHelper.isActionRepo(path)) { + core.setFailed( + 'action.y(a)ml not found. Action packages can be created only for action repositories.' + ) + return + } + // Create a temporary directory to store the archives const archiveDir = fsHelper.createTempDir() tmpDirs.push(archiveDir) From 7120405e17829843c4ceef926a11b65b8dcdf0dc Mon Sep 17 00:00:00 2001 From: Edwin Sirko Date: Fri, 26 Jan 2024 13:09:08 -0500 Subject: [PATCH 5/6] auto changes --- __tests__/fs-helper.test.ts | 2 +- dist/index.js | 19 +++++++++++-------- src/fs-helper.ts | 2 -- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/__tests__/fs-helper.test.ts b/__tests__/fs-helper.test.ts index b86174a..68931ef 100644 --- a/__tests__/fs-helper.test.ts +++ b/__tests__/fs-helper.test.ts @@ -153,7 +153,7 @@ describe('isActionRepo', () => { expect(fsHelper.isActionRepo(stagingDir)).toEqual(true) }) - it('returns false if action.y(a)ml doesn\'t exist at the root', () => { + it("returns false if action.y(a)ml doesn't exist at the root", () => { fs.writeFileSync(path.join(stagingDir, `action.yaaml`), fileContent) expect(fsHelper.isActionRepo(stagingDir)).toEqual(false) }) diff --git a/dist/index.js b/dist/index.js index bed62a1..fb91e07 100644 --- a/dist/index.js +++ b/dist/index.js @@ -74693,7 +74693,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.bundleFilesintoDirectory = exports.readFileContents = exports.isDirectory = exports.createArchives = exports.removeDir = exports.createTempDir = void 0; +exports.bundleFilesintoDirectory = exports.readFileContents = exports.isActionRepo = exports.isDirectory = exports.createArchives = exports.removeDir = exports.createTempDir = void 0; const fs = __importStar(__nccwpck_require__(57147)); const fs_extra_1 = __importDefault(__nccwpck_require__(5630)); const path = __importStar(__nccwpck_require__(71017)); @@ -74764,6 +74764,11 @@ function isDirectory(dirPath) { return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory(); } exports.isDirectory = isDirectory; +function isActionRepo(stagingDir) { + return (fs.existsSync(path.join(stagingDir, 'action.yml')) || + fs.existsSync(path.join(stagingDir, 'action.yaml'))); +} +exports.isActionRepo = isActionRepo; function readFileContents(filePath) { return fs.readFileSync(filePath); } @@ -75052,17 +75057,11 @@ async function run(pathInput) { // 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.`); return; } const token = process.env.TOKEN; - // TODO: once https://github.com/github/github/pull/309384 goes in, we can switch to the actual endpoint - //const response = await fetch( - // process.env.GITHUB_API_URL + '/packages/container-registry-url' - //) - const response = await fetch('http://echo.jsontest.com/url/https:ghcr.io' // for testing locally. Remove the slashes, they will be reintroduced when forming the URL object below - ); + const response = await fetch(process.env.GITHUB_API_URL + '/packages/container-registry-url'); if (!response.ok) { throw new Error(`Failed to fetch status page: ${response.statusText}`); } @@ -75084,6 +75083,10 @@ async function run(pathInput) { tmpDirs.push(bundleDir); path = fsHelper.bundleFilesintoDirectory(paths, bundleDir); } + if (!fsHelper.isActionRepo(path)) { + core.setFailed('action.y(a)ml not found. Action packages can be created only for action repositories.'); + return; + } // Create a temporary directory to store the archives const archiveDir = fsHelper.createTempDir(); tmpDirs.push(archiveDir); diff --git a/src/fs-helper.ts b/src/fs-helper.ts index a191aba..5f81281 100644 --- a/src/fs-helper.ts +++ b/src/fs-helper.ts @@ -98,7 +98,6 @@ export function isActionRepo(stagingDir: string): boolean { ) } - export function readFileContents(filePath: string): Buffer { return fs.readFileSync(filePath) } @@ -124,7 +123,6 @@ export function bundleFilesintoDirectory( return targetDir } - // Converts a file path to a filemetadata object by querying the fs for relevant metadata. async function fileMetadata(filePath: string): Promise { const stats = fs.statSync(filePath) From 206ff2d4e261f655b7ebaa470009a838de8d7e39 Mon Sep 17 00:00:00 2001 From: Edwin Sirko Date: Fri, 26 Jan 2024 13:15:36 -0500 Subject: [PATCH 6/6] order logic to fail fast appropriately --- src/main.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/main.ts b/src/main.ts index 2c5c6f0..8e936ae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,10 +21,12 @@ export async function run(pathInput: string): Promise { core.setFailed(`Could not find Repository.`) return } + if (github.context.eventName !== 'release') { core.setFailed('Please ensure you have the workflow trigger as release.') return } + 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 @@ -39,16 +41,6 @@ export async function run(pathInput: string): Promise { const token: string = process.env.TOKEN! - const response = await fetch( - process.env.GITHUB_API_URL + '/packages/container-registry-url' - ) - 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}`) - // Gather & validate user input // Paths to be included in the OCI image // const paths: string[] = core.getInput('path').split(' ') @@ -92,6 +84,16 @@ export async function run(pathInput: string): Promise { .update(JSON.stringify(manifest)) .digest('hex') + const response = await fetch( + process.env.GITHUB_API_URL + '/packages/container-registry-url' + ) + 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}`) + const packageURL = await ghcr.publishOCIArtifact( token, registryURL,