Skip to content

Commit a8a1f32

Browse files
committed
Extract service name from metadata
1 parent 3355306 commit a8a1f32

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/main.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export async function run(): Promise<void> {
9696
try {
9797
// Get action inputs
9898
const image = getInput('image'); // Image ie gcr.io/...
99-
const service = getInput('service'); // Service name
99+
let service = getInput('service'); // Service name
100100
const job = getInput('job'); // Job name
101101
const metadata = getInput('metadata'); // YAML file
102102
const projectId = getInput('project_id');
@@ -156,6 +156,19 @@ export async function run(): Promise<void> {
156156
const contents = await readFile(metadata, 'utf8');
157157
const parsed = parseYAML(contents);
158158

159+
// Extract service name from metadata template
160+
const name = parsed?.metadata?.name;
161+
if (!name) {
162+
throw new Error(`${metadata} is missing 'metadata.name'`);
163+
}
164+
if (service && service != name) {
165+
throw new Error(
166+
`service name in ${metadata} ("${name}") does not match GitHub ` +
167+
`Actions service input ("${service}")`,
168+
);
169+
}
170+
service = name;
171+
159172
const kind = parsed?.kind;
160173
if (kind === 'Service') {
161174
deployCmd = ['run', 'services', 'replace', metadata];

tests/unit/main.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,31 @@ test('#run', { concurrency: true }, async (suite) => {
337337
assertMembers(args, ['services', 'replace']);
338338
});
339339

340+
await suite.test('errors if metadata is given and the service names do not match', async (t) => {
341+
defaultMocks(t.mock, {
342+
metadata: 'tests/fixtures/service.yaml',
343+
service: 'not-a-match',
344+
});
345+
346+
await assert.rejects(
347+
async () => {
348+
await run();
349+
},
350+
{ message: /does not match/ },
351+
);
352+
});
353+
354+
await suite.test('does not error if metadata is given and the service names match', async (t) => {
355+
defaultMocks(t.mock, {
356+
metadata: 'tests/fixtures/service.yaml',
357+
service: 'run-full-yaml',
358+
});
359+
360+
await assert.doesNotReject(async () => {
361+
await run();
362+
});
363+
});
364+
340365
await suite.test('sets job metadata if given', async (t) => {
341366
const mocks = defaultMocks(t.mock, {
342367
metadata: 'tests/fixtures/job.yaml',

0 commit comments

Comments
 (0)