Skip to content

Commit a6ebc7d

Browse files
authored
Support jobs metadata too (#519)
Closes #508
1 parent 1b14a74 commit a6ebc7d

File tree

8 files changed

+87
-76
lines changed

8 files changed

+87
-76
lines changed

.github/workflows/integration.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ jobs:
163163
164164
- name: 'Set service name in metadata YAML'
165165
run: |-
166-
sed -i "s/run-full-yaml/${{ env.SERVICE_NAME }}/" ./tests/unit/service.yaml
166+
sed -i "s/run-full-yaml/${{ env.SERVICE_NAME }}/" ./tests/fixtures/service.yaml
167167
168168
- uses: 'actions/setup-node@v4'
169169
with:
@@ -180,7 +180,7 @@ jobs:
180180
name: 'Deploy'
181181
uses: './'
182182
with:
183-
metadata: './tests/unit/service.yaml'
183+
metadata: './tests/fixtures/service.yaml'
184184

185185
- name: 'Run initial deploy tests'
186186
run: 'npm run e2e-tests'

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ jobs:
262262
### Custom metadata YAML
263263

264264
For advanced use cases, you can define a custom Cloud Run metadata file. This is
265-
a YAML description of the Cloud Run service. This allows you to customize your
265+
a YAML description of the Cloud Run service or job. This allows you to customize your
266266
service configuration, such as [memory
267267
limits](https://cloud.google.com/run/docs/configuring/memory-limits), [CPU
268268
allocation](https://cloud.google.com/run/docs/configuring/cpu), [max
@@ -271,7 +271,7 @@ instances](https://cloud.google.com/run/docs/configuring/max-instances), and
271271

272272
**⚠️ When using a custom metadata YAML file, all other inputs are ignored!**
273273

274-
- `metadata`: (Optional) The path to a Cloud Run service metadata file.
274+
- `metadata`: (Optional) The path to a Cloud Run service or job metadata file.
275275

276276
To [deploying a new service](https://cloud.google.com/run/docs/deploying#yaml)
277277
to create a new YAML service definition:

package-lock.json

Lines changed: 14 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"@actions/exec": "^1.1.1",
3434
"@actions/tool-cache": "^2.0.1",
3535
"@google-github-actions/actions-utils": "^0.7.5",
36-
"@google-github-actions/setup-cloud-sdk": "^1.1.6"
36+
"@google-github-actions/setup-cloud-sdk": "^1.1.6",
37+
"yaml": "^2.4.2"
3738
},
3839
"devDependencies": {
3940
"@types/node": "^20.12.11",

src/main.ts

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import {
2727
} from '@actions/core';
2828
import { getExecOutput } from '@actions/exec';
2929
import * as toolCache from '@actions/tool-cache';
30+
import { readFile } from 'fs/promises';
31+
import { parse as parseYAML } from 'yaml';
32+
3033
import {
3134
errorMessage,
3235
isPinnedToHead,
@@ -146,45 +149,17 @@ export async function run(): Promise<void> {
146149
cmd = ['run', 'services', 'update-traffic', service];
147150
if (revTraffic) cmd.push('--to-revisions', revTraffic);
148151
if (tagTraffic) cmd.push('--to-tags', tagTraffic);
149-
150-
const providedButIgnored: Record<string, boolean> = {
151-
image: image !== '',
152-
metadata: metadata !== '',
153-
source: source !== '',
154-
env_vars: envVars !== '',
155-
no_traffic: noTraffic,
156-
secrets: Object.keys(secrets).length > 0,
157-
suffix: suffix !== '',
158-
tag: tag !== '',
159-
labels: Object.keys(labels).length > 0,
160-
timeout: timeout !== '',
161-
};
162-
for (const key in providedButIgnored) {
163-
if (providedButIgnored[key]) {
164-
logWarning(`Updating traffic, ignoring "${key}" input`);
165-
}
166-
}
167152
} else if (metadata) {
168-
cmd = ['run', 'services', 'replace', metadata];
169-
170-
const providedButIgnored: Record<string, boolean> = {
171-
image: image !== '',
172-
service: service !== '',
173-
source: source !== '',
174-
env_vars: envVars !== '',
175-
no_traffic: noTraffic,
176-
secrets: Object.keys(secrets).length > 0,
177-
suffix: suffix !== '',
178-
tag: tag !== '',
179-
revision_traffic: revTraffic !== '',
180-
tag_traffic: revTraffic !== '',
181-
labels: Object.keys(labels).length > 0,
182-
timeout: timeout !== '',
183-
};
184-
for (const key in providedButIgnored) {
185-
if (providedButIgnored[key]) {
186-
logWarning(`Using metadata YAML, ignoring "${key}" input`);
187-
}
153+
const contents = await readFile(metadata, 'utf8');
154+
const parsed = parseYAML(contents);
155+
156+
const kind = parsed?.kind;
157+
if (kind === 'Service') {
158+
cmd = ['run', 'services', 'replace', metadata];
159+
} else if (kind === 'Job') {
160+
cmd = ['run', 'jobs', 'replace', metadata];
161+
} else {
162+
throw new Error(`Unkown metadata type "${kind}", expected "Job" or "Service"`);
188163
}
189164
} else if (job) {
190165
logWarning(
@@ -249,17 +224,14 @@ export async function run(): Promise<void> {
249224

250225
// Push common flags
251226
cmd.push('--format', 'json');
252-
if (region) {
253-
switch (region.length) {
254-
case 0:
255-
break;
256-
case 1:
257-
cmd.push('--region', region[0]);
258-
break;
259-
default:
260-
cmd.push('--region', region.join(','));
261-
break;
262-
}
227+
if (region?.length > 0) {
228+
cmd.push(
229+
'--region',
230+
region
231+
.flat()
232+
.filter((e) => e !== undefined && e !== null && e !== '')
233+
.join(','),
234+
);
263235
}
264236
if (projectId) cmd.push('--project', projectId);
265237

tests/fixtures/job.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: 'run.googleapis.com/v1'
2+
kind: 'Job'
3+
metadata:
4+
name: 'job'
5+
labels:
6+
cloud.googleapis.com/location: 'us-east1'
7+
spec:
8+
template:
9+
metadata:
10+
annotations:
11+
run.googleapis.com/execution-environment: 'gen2'
12+
spec:
13+
parallelism: 1
14+
taskCount: 1
15+
template:
16+
spec:
17+
containers:
18+
- image: 'gcr.io/cloudrun/hello'
19+
imagePullPolicy: 'Always'
20+
resources:
21+
limits:
22+
cpu: '1000m'
23+
memory: '512Mi'
24+
maxRetries: 0
25+
timeoutSeconds: '3600'
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
apiVersion: serving.knative.dev/v1
2-
kind: Service
1+
apiVersion: 'serving.knative.dev/v1'
2+
kind: 'Service'
33
metadata:
4-
name: run-full-yaml
4+
name: 'run-full-yaml'
55
spec:
66
template:
77
metadata:
88
labels:
9-
test_label: test_value
9+
test_label: 'test_value'
1010
annotations:
1111
run.googleapis.com/cloudsql-instances: 'test-project:us-central1:my-test-instance'
1212
spec:
1313
containerConcurrency: 20
1414
containers:
15-
- image: gcr.io/cloudrun/hello
15+
- image: 'gcr.io/cloudrun/hello'
1616
ports:
1717
- containerPort: 8080
1818
resources:
1919
limits:
2020
cpu: '2'
21-
memory: 1Gi
21+
memory: '1Gi'
2222
timeoutSeconds: 300

tests/unit/main.test.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,16 +271,28 @@ test('#run', { concurrency: true }, async (suite) => {
271271
assertMembers(args, ['--source', 'example-app']);
272272
});
273273

274-
await suite.test('sets metadata if given', async (t) => {
274+
await suite.test('sets service metadata if given', async (t) => {
275275
const mocks = defaultMocks(t.mock, {
276-
metadata: 'yaml',
276+
metadata: 'tests/fixtures/service.yaml',
277277
image: '',
278278
});
279279

280280
await run();
281281

282282
const args = mocks.getExecOutput.mock.calls?.at(0).arguments?.at(1);
283-
assertMembers(args, ['services', 'replace', 'yaml']);
283+
assertMembers(args, ['services', 'replace']);
284+
});
285+
286+
await suite.test('sets job metadata if given', async (t) => {
287+
const mocks = defaultMocks(t.mock, {
288+
metadata: 'tests/fixtures/job.yaml',
289+
image: '',
290+
});
291+
292+
await run();
293+
294+
const args = mocks.getExecOutput.mock.calls?.at(0).arguments?.at(1);
295+
assertMembers(args, ['jobs', 'replace']);
284296
});
285297

286298
await suite.test('sets timeout if given', async (t) => {

0 commit comments

Comments
 (0)