Skip to content
Closed
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@ jobs:

## Inputs

- `service`: (Required, unless providing `metadata`) ID of the service or
- `service`: (Required, unless providing `metadata` or `job`) ID of the service or
fully-qualified identifier of the service.

- `job`: (Required, unless providing `metadata` or `service`) ID of the job or
fully-qualified identifier of the job. If `job` and `service` are specified
then the `service` will be updated and the `job` will be ignored. Note that
the `job` must be created first. This will only update an existing `job`, it
will not deploy/create a new job. Note that as of Dec 31, 2022, Cloud Run for

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of May 1, 2023 - job in Cloud Run has exited beta.

`jobs` is still in Beta and thus requires that you set `gcloud_component` to
`beta` or `alpha`.

- `image`: (Required, unless providing `metadata` or `source`) Fully-qualified
name of the container image to deploy. For example:

Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ inputs:
Required if not using a service YAML.
required: false

job:
description: |-
ID of the job or fully qualified identifier for the job.
Required if not using a service YAML.
required: false

region:
description: |-
Region in which the resource can be found.
Expand Down
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function run(): Promise<void> {
// Get action inputs
const image = getInput('image'); // Image ie gcr.io/...
const service = getInput('service'); // Service name
const job = getInput('job'); // Job name
const metadata = getInput('metadata'); // YAML file
const projectId = getInput('project_id');
const gcloudVersion = await computeGcloudVersion(getInput('gcloud_version'));
Expand Down Expand Up @@ -124,6 +125,7 @@ export async function run(): Promise<void> {
if (gcloudComponent && gcloudComponent !== 'alpha' && gcloudComponent !== 'beta') {
throw new Error(`invalid input received for gcloud_component: ${gcloudComponent}`);
}
const useJob = job && !service;

// Find base command
if (revTraffic || tagTraffic) {
Expand Down Expand Up @@ -174,6 +176,26 @@ export async function run(): Promise<void> {
logWarning(`Using metadata YAML, ignoring "${key}" input`);
}
}
} else if (useJob) {
cmd = ['run', 'jobs', 'update', job, '--quiet'];

if (image) {
// Deploy job with image specified
cmd.push('--image', image);
}

// Set optional flags from inputs
const compiledEnvVars = parseKVStringAndFile(envVars, envVarsFile);
if (compiledEnvVars && Object.keys(compiledEnvVars).length > 0) {
cmd.push('--update-env-vars', kvToString(compiledEnvVars));
}
if (secrets && Object.keys(secrets).length > 0) {
cmd.push('--update-secrets', kvToString(secrets));
}

// Compile the labels
const compiledLabels = Object.assign({}, defaultLabels(), labels);
cmd.push('--update-labels', kvToString(compiledLabels));
} else {
cmd = ['run', 'deploy', service, '--quiet'];

Expand Down Expand Up @@ -206,7 +228,9 @@ export async function run(): Promise<void> {
}

// Push common flags
cmd.push('--platform', 'managed');
if (!useJob) {
cmd.push('--platform', 'managed');
}
cmd.push('--format', 'json');
if (region) cmd.push('--region', region);
if (projectId) cmd.push('--project', projectId);
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { run, kvToString } from '../../src/main';
const fakeInputs: { [key: string]: string } = {
image: 'gcr.io/cloudrun/hello',
service: 'test',
job: '',
metadata: '',
project_id: 'my-test-project',
env_vars: '',
Expand Down Expand Up @@ -230,6 +231,50 @@ describe('#run', function () {
await run();
expect(this.stubs.installComponent.withArgs('beta').callCount).to.eq(1);
});

it('updates a job if job is specified and service is not', async function () {
this.stubs.getInput.withArgs('service').returns(undefined);
this.stubs.getInput.withArgs('job').returns('job-name');
await run();
const call = this.stubs.getExecOutput.getCall(0);
expect(call).to.be;
const args = call.args[1];
expect(args).to.include.members([
'run',
'jobs',
'update',
'job-name',
'--image',
'gcr.io/cloudrun/hello',
]);
});

it('updates a job if job is specified and service is an empty string', async function () {
this.stubs.getInput.withArgs('service').returns('');
this.stubs.getInput.withArgs('job').returns('job-name');
await run();
const call = this.stubs.getExecOutput.getCall(0);
expect(call).to.be;
const args = call.args[1];
expect(args).to.include.members([
'run',
'jobs',
'update',
'job-name',
'--image',
'gcr.io/cloudrun/hello',
]);
});

it('ignore job if job and service are both specified', async function () {
this.stubs.getInput.withArgs('service').returns('service-name');
this.stubs.getInput.withArgs('job').returns('job-name');
await run();
const call = this.stubs.getExecOutput.getCall(0);
expect(call).to.be;
const args = call.args[1];
expect(args).to.not.include.members(['jobs', 'job-name', '--platform']);
});
});

describe('#kvToString', () => {
Expand Down