Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Enable jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Green committed Sep 23, 2023
1 parent 5803578 commit 61f4595
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# deploy-cloudrun

## NOTE

> This GitHub Action is being used as a workaround until the following PR is merged: [feat: enable using Cloud Run Job](https://github.com/google-github-actions/deploy-cloudrun/pull/422). It is recommended to use the [official Google deploy-cloudrun GitHub Action](https://github.com/google-github-actions/deploy-cloudrun).
The `deploy-cloudrun` GitHub Action deploys to Google [Cloud Run][cloud-run]. It
can deploy a container image or from source, and the resulting service URL is
available as a GitHub Actions output for use in future steps.
Expand Down Expand Up @@ -52,9 +56,15 @@ 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.

- `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.

27 changes: 26 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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 @@ -127,6 +128,8 @@ export async function run(): Promise<void> {
throw new Error(`invalid input received for gcloud_component: ${gcloudComponent}`);
}

const useJob = job && !service;

// Find base command
if (revTraffic || tagTraffic) {
// Set response type for output parsing
Expand Down Expand Up @@ -176,6 +179,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 @@ -211,7 +234,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 @@ -247,6 +248,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

0 comments on commit 61f4595

Please sign in to comment.