Skip to content

Commit 08e594e

Browse files
authored
feat: added gcloud_component flag to force alpha or beta (#294)
* feat: added gcloud_components flag to force alpha or beta * fix: addressed pr comments * fix: inline defaults * fix: addressed pr comments
1 parent 7c8e7d0 commit 08e594e

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ jobs:
8989
| `tag_traffic` | _optional_ | | Comma separated list of traffic assignments in the form TAG=PERCENTAGE. |
9090
| `flags` | _optional_ | | Space separated list of other Cloud Run flags, examples can be found: https://cloud.google.com/sdk/gcloud/reference/run/deploy#FLAGS. |
9191
| `gcloud_version` | _optional_ | `latest` | Pin the version of Cloud SDK `gcloud` CLI. |
92+
| `gcloud_component` | _optional_ | | Pin the Cloud SDK `gcloud` CLI components version, valid values are `alpha` or `beta`. |
9293
| `credentials`| Required if not using the `setup-gcloud` action with exported credentials. | | (**Deprecated**) This input is deprecated. See [auth section](https://github.com/google-github-actions/deploy-cloudrun#via-google-github-actionsauth) for more details. Service account key to use for authentication. This should be the JSON formatted private key which can be exported from the Cloud Console. The value can be raw or base64-encoded. |
9394

9495
### Metadata customizations

action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ inputs:
143143
installed. Example: "290.0.1".
144144
required: false
145145

146+
gcloud_component:
147+
description: |-
148+
Version of the Cloud SDK components to install and use. If unspecified, the latest
149+
or released version will be used. This is the equivalent of running
150+
'gcloud alpha run' or 'gcloud beta run'. Valid values are `alpha` or `beta`.
151+
required: false
152+
146153
outputs:
147154
url:
148155
description: The URL of your Cloud Run service

src/deploy-cloudrun.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import * as core from '@actions/core';
1818
import { getExecOutput } from '@actions/exec';
1919
import * as toolCache from '@actions/tool-cache';
20+
import { presence } from '@google-github-actions/actions-utils/dist';
2021
import * as setupGcloud from '@google-github-actions/setup-cloud-sdk';
2122
import path from 'path';
2223
import { parseDeployResponse, parseUpdateTrafficResponse } from './output-parser';
@@ -54,6 +55,7 @@ export async function run(): Promise<void> {
5455
const credentials = core.getInput('credentials'); // Service account key
5556
let projectId = core.getInput('project_id');
5657
let gcloudVersion = core.getInput('gcloud_version');
58+
let gcloudComponent = presence(core.getInput('gcloud_component')); // Cloud SDK component version
5759
// Flags
5860
const envVars = core.getInput('env_vars'); // String of env vars KEY=VALUE,...
5961
const secrets = core.getInput('secrets'); // String of secrets KEY=VALUE,...
@@ -77,7 +79,6 @@ export async function run(): Promise<void> {
7779
}
7880

7981
let responseType = ResponseTypes.DEPLOY; // Default response type for output parsing
80-
let installBeta = false; // Flag for installing gcloud beta components
8182
let cmd;
8283

8384
// Throw errors if inputs aren't valid
@@ -88,6 +89,11 @@ export async function run(): Promise<void> {
8889
throw new Error('No service name set.');
8990
}
9091

92+
// Validate gcloud component input
93+
if (gcloudComponent && gcloudComponent !== 'alpha' && gcloudComponent !== 'beta') {
94+
throw new Error(`invalid input received for gcloud_component: ${gcloudComponent}`);
95+
}
96+
9197
// Find base command
9298
if (revTraffic || tagTraffic) {
9399
// Set response type for output parsing
@@ -104,7 +110,7 @@ export async function run(): Promise<void> {
104110
'--region',
105111
region,
106112
];
107-
installBeta = true;
113+
gcloudComponent = gcloudComponent ?? 'beta';
108114
if (revTraffic) cmd.push('--to-revisions', revTraffic);
109115
if (tagTraffic) cmd.push('--to-tags', tagTraffic);
110116
} else if (source) {
@@ -121,7 +127,7 @@ export async function run(): Promise<void> {
121127
'--source',
122128
source,
123129
];
124-
installBeta = true;
130+
gcloudComponent = gcloudComponent ?? 'beta';
125131
if (timeout) cmd.push('--timeout', timeout);
126132
} else if (metadata) {
127133
// Deploy service from metadata
@@ -131,7 +137,7 @@ export async function run(): Promise<void> {
131137
);
132138
}
133139
cmd = ['run', 'services', 'replace', metadata, '--platform', 'managed', '--region', region];
134-
installBeta = true;
140+
gcloudComponent = gcloudComponent ?? 'beta';
135141
} else {
136142
// Deploy service with image specified
137143
cmd = [
@@ -152,11 +158,11 @@ export async function run(): Promise<void> {
152158
if (envVars) cmd.push('--update-env-vars', envVars);
153159
if (secrets) {
154160
cmd.push('--update-secrets', secrets.replace('\n', ','));
155-
installBeta = true;
161+
gcloudComponent = gcloudComponent ?? 'beta';
156162
}
157163
if (tag) {
158164
cmd.push('--tag', tag);
159-
installBeta = true;
165+
gcloudComponent = gcloudComponent ?? 'beta';
160166
}
161167
if (suffix) cmd.push('--revision-suffix', suffix);
162168
if (noTraffic) cmd.push('--no-traffic');
@@ -199,10 +205,10 @@ export async function run(): Promise<void> {
199205
}
200206
if (projectId) cmd.push('--project', projectId);
201207

202-
// Install beta components if needed and prepend the beta command
203-
if (installBeta) {
204-
await setupGcloud.installComponent('beta');
205-
cmd.unshift('beta');
208+
// Install gcloud component if needed and prepend the command
209+
if (gcloudComponent) {
210+
await setupGcloud.installComponent(gcloudComponent);
211+
cmd.unshift(gcloudComponent);
206212
}
207213

208214
// Set output format to json

tests/unit/deploy-cloudrun.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ describe('#deploy-cloudrun', function () {
163163
await run();
164164
expect(this.stubs.setFailed.callCount).to.eq(1);
165165
});
166+
it('uses default components without gcloud_component flag', async function () {
167+
await run();
168+
expect(this.stubs.installComponent.callCount).to.eq(0);
169+
});
170+
it('throws error with invalid gcloud component flag', async function () {
171+
this.stubs.getInput.withArgs('gcloud_component').returns('wrong_value');
172+
await run();
173+
expect(
174+
this.stubs.setFailed.withArgs(`invalid input received for gcloud_component: wrong_value`)
175+
.callCount,
176+
).to.be.at.least(1);
177+
});
178+
it('installs alpha component with alpha flag', async function () {
179+
this.stubs.getInput.withArgs('gcloud_component').returns('alpha');
180+
await run();
181+
expect(this.stubs.installComponent.withArgs('alpha').callCount).to.eq(1);
182+
});
183+
it('installs beta component with beta flag', async function () {
184+
this.stubs.getInput.withArgs('gcloud_component').returns('beta');
185+
await run();
186+
expect(this.stubs.installComponent.withArgs('beta').callCount).to.eq(1);
187+
});
166188
});
167189

168190
describe('#parseFlags', () => {

0 commit comments

Comments
 (0)