properly getting CR URL

This commit is contained in:
Edwin Sirko
2024-02-02 12:59:49 -05:00
parent 002cf60682
commit 5f9b214e33
5 changed files with 68 additions and 23 deletions
+28
View File
@@ -1,5 +1,6 @@
import * as fsHelper from '../src/fs-helper' import * as fsHelper from '../src/fs-helper'
import * as fs from 'fs' import * as fs from 'fs'
import * as path from 'path'
import * as os from 'os' import * as os from 'os'
import { execSync } from 'child_process' 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', () => { describe('readFileContents', () => {
let dir: string let dir: string
Generated Vendored
+11 -8
View File
@@ -74693,7 +74693,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", ({ value: true })); 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 = __importStar(__nccwpck_require__(57147));
const fs_extra_1 = __importDefault(__nccwpck_require__(5630)); const fs_extra_1 = __importDefault(__nccwpck_require__(5630));
const path = __importStar(__nccwpck_require__(71017)); const path = __importStar(__nccwpck_require__(71017));
@@ -74764,6 +74764,11 @@ function isDirectory(dirPath) {
return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory(); return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
} }
exports.isDirectory = 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) { function readFileContents(filePath) {
return fs.readFileSync(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 // https://docs.github.com/en/actions/creating-actions/releasing-and-maintaining-actions
const targetVersion = semver_1.default.parse(releaseTag.replace(/^v/, '')); const targetVersion = semver_1.default.parse(releaseTag.replace(/^v/, ''));
if (!targetVersion) { 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.`); core.setFailed(`${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.`);
return; return;
} }
const token = process.env.TOKEN; 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(
// 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
);
if (!response.ok) { if (!response.ok) {
throw new Error(`Failed to fetch status page: ${response.statusText}`); throw new Error(`Failed to fetch status page: ${response.statusText}`);
} }
@@ -75084,6 +75083,10 @@ async function run(pathInput) {
tmpDirs.push(bundleDir); tmpDirs.push(bundleDir);
path = fsHelper.bundleFilesintoDirectory(paths, 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 // Create a temporary directory to store the archives
const archiveDir = fsHelper.createTempDir(); const archiveDir = fsHelper.createTempDir();
tmpDirs.push(archiveDir); tmpDirs.push(archiveDir);
+3 -1
View File
@@ -17,9 +17,11 @@ fi
echo "Generating new version $VERSION with message $MESSAGE" 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 npm run bundle
git add . git add .
git commit -m "$VERSION: $MESSAGE" git commit -m "$VERSION: $MESSAGE"
git push git push
git tag $VERSION
git push origin $VERSION
gh release create --repo ddivad195/publish-action-package --title $VERSION --notes $VERSION $VERSION gh release create --repo ddivad195/publish-action-package --title $VERSION --notes $VERSION $VERSION
+7
View File
@@ -91,6 +91,13 @@ export function isDirectory(dirPath: string): boolean {
return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory() 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 { export function readFileContents(filePath: string): Buffer {
return fs.readFileSync(filePath) return fs.readFileSync(filePath)
} }
+19 -14
View File
@@ -21,17 +21,18 @@ export async function run(pathInput: string): Promise<void> {
core.setFailed(`Could not find Repository.`) core.setFailed(`Could not find Repository.`)
return return
} }
if (github.context.eventName !== 'release') { if (github.context.eventName !== 'release') {
core.setFailed('Please ensure you have the workflow trigger as release.') core.setFailed('Please ensure you have the workflow trigger as release.')
return return
} }
const releaseId: string = github.context.payload.release.id const releaseId: string = github.context.payload.release.id
const releaseTag: string = github.context.payload.release.tag_name 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 // 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 // https://docs.github.com/en/actions/creating-actions/releasing-and-maintaining-actions
const targetVersion = semver.parse(releaseTag.replace(/^v/, '')) const targetVersion = semver.parse(releaseTag.replace(/^v/, ''))
if (!targetVersion) { 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( core.setFailed(
`${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.` `${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.`
) )
@@ -39,19 +40,6 @@ export async function run(pathInput: string): Promise<void> {
} }
const token: string = process.env.TOKEN! 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
)
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 // Gather & validate user input
// Paths to be included in the OCI image // Paths to be included in the OCI image
@@ -69,6 +57,13 @@ export async function run(pathInput: string): Promise<void> {
path = fsHelper.bundleFilesintoDirectory(paths, 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 // Create a temporary directory to store the archives
const archiveDir = fsHelper.createTempDir() const archiveDir = fsHelper.createTempDir()
tmpDirs.push(archiveDir) tmpDirs.push(archiveDir)
@@ -89,6 +84,16 @@ export async function run(pathInput: string): Promise<void> {
.update(JSON.stringify(manifest)) .update(JSON.stringify(manifest))
.digest('hex') .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( const packageURL = await ghcr.publishOCIArtifact(
token, token,
registryURL, registryURL,