Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract service name from metadata #538

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export async function run(): Promise<void> {
try {
// Get action inputs
const image = getInput('image'); // Image ie gcr.io/...
const service = getInput('service'); // Service name
let service = getInput('service'); // Service name
const job = getInput('job'); // Job name
const metadata = getInput('metadata'); // YAML file
const projectId = getInput('project_id');
Expand Down Expand Up @@ -156,6 +156,19 @@ export async function run(): Promise<void> {
const contents = await readFile(metadata, 'utf8');
const parsed = parseYAML(contents);

// Extract service name from metadata template
const name = parsed?.metadata?.name;
if (!name) {
throw new Error(`${metadata} is missing 'metadata.name'`);
}
if (service && service != name) {
throw new Error(
`service name in ${metadata} ("${name}") does not match GitHub ` +
`Actions service input ("${service}")`,
);
}
service = name;

const kind = parsed?.kind;
if (kind === 'Service') {
deployCmd = ['run', 'services', 'replace', metadata];
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,31 @@ test('#run', { concurrency: true }, async (suite) => {
assertMembers(args, ['services', 'replace']);
});

await suite.test('errors if metadata is given and the service names do not match', async (t) => {
defaultMocks(t.mock, {
metadata: 'tests/fixtures/service.yaml',
service: 'not-a-match',
});

await assert.rejects(
async () => {
await run();
},
{ message: /does not match/ },
);
});

await suite.test('does not error if metadata is given and the service names match', async (t) => {
defaultMocks(t.mock, {
metadata: 'tests/fixtures/service.yaml',
service: 'run-full-yaml',
});

await assert.doesNotReject(async () => {
await run();
});
});

await suite.test('sets job metadata if given', async (t) => {
const mocks = defaultMocks(t.mock, {
metadata: 'tests/fixtures/job.yaml',
Expand Down
Loading