-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathmain.ts
More file actions
400 lines (356 loc) · 13.1 KB
/
main.ts
File metadata and controls
400 lines (356 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import path from 'path';
import {
addPath,
debug as logDebug,
getInput,
info as logInfo,
setFailed,
setOutput,
warning as logWarning,
} from '@actions/core';
import { getExecOutput } from '@actions/exec';
import * as toolCache from '@actions/tool-cache';
import { readFile } from 'fs/promises';
import { parse as parseYAML } from 'yaml';
import {
errorMessage,
isPinnedToHead,
joinKVStringForGCloud,
KVPair,
parseBoolean,
parseCSV,
parseFlags,
parseKVString,
parseKVStringAndFile,
pinnedToHeadWarning,
presence,
} from '@google-github-actions/actions-utils';
import {
authenticateGcloudSDK,
getLatestGcloudSDKVersion,
getToolCommand,
installComponent as installGcloudComponent,
installGcloudSDK,
isInstalled as isGcloudInstalled,
} from '@google-github-actions/setup-cloud-sdk';
import { parseDeployResponse, parseUpdateTrafficResponse } from './output-parser';
// Do not listen to the linter - this can NOT be rewritten as an ES6 import
// statement.
const { version: appVersion } = require('../package.json');
// isDebug returns true if runner debugging or step debugging is enabled.
const isDebug =
parseBoolean(process.env.ACTIONS_RUNNER_DEBUG) || parseBoolean(process.env.ACTIONS_STEP_DEBUG);
/**
* DeployCloudRunOutputs are the common GitHub action outputs created by this action
*/
export interface DeployCloudRunOutputs {
url?: string | null | undefined; // Type required to match run_v1.Schema$Service.status.url
}
/**
* ResponseTypes are the gcloud command response formats
*/
enum ResponseTypes {
DEPLOY,
UPDATE_TRAFFIC,
}
/**
* Executes the main action. It includes the main business logic and is the
* primary entry point. It is documented inline.
*/
export async function run(): Promise<void> {
// Register metrics
process.env.CLOUDSDK_CORE_DISABLE_PROMPTS = '1';
process.env.CLOUDSDK_METRICS_ENVIRONMENT = 'github-actions-deploy-cloudrun';
process.env.CLOUDSDK_METRICS_ENVIRONMENT_VERSION = appVersion;
process.env.GOOGLE_APIS_USER_AGENT = `google-github-actions:deploy-cloudrun/${appVersion}`;
// Warn if pinned to HEAD
if (isPinnedToHead()) {
logWarning(pinnedToHeadWarning('v1'));
}
try {
// Get action inputs
const image = getInput('image'); // Image ie us-docker.pkg.dev/...
let 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'));
const gcloudComponent = presence(getInput('gcloud_component')); // Cloud SDK component version
const envVars = getInput('env_vars'); // String of env vars KEY=VALUE,...
const envVarsUpdateStrategy = getInput('env_vars_update_strategy') || 'merge';
const secrets = parseKVString(getInput('secrets')); // String of secrets KEY=VALUE,...
const secretsUpdateStrategy = getInput('secrets_update_strategy') || 'merge';
const region = parseCSV(getInput('region') || 'us-central1');
const source = getInput('source'); // Source directory
const suffix = getInput('suffix');
const tag = getInput('tag');
const timeout = getInput('timeout');
const noTraffic = (getInput('no_traffic') || '').toLowerCase() === 'true';
const wait = parseBoolean(getInput('wait'));
const revTraffic = getInput('revision_traffic');
const tagTraffic = getInput('tag_traffic');
const labels = parseKVString(getInput('labels'));
const skipDefaultLabels = parseBoolean(getInput('skip_default_labels'));
const flags = getInput('flags');
const updateTrafficFlags = getInput('update_traffic_flags');
let deployCmd: string[] = [];
// Throw errors if inputs aren't valid
if (revTraffic && tagTraffic) {
throw new Error('Only one of `revision_traffic` or `tag_traffic` inputs can be set.');
}
if ((revTraffic || tagTraffic) && !service) {
throw new Error('No service name set.');
}
if (source && image) {
throw new Error('Only one of `source` or `image` inputs can be set.');
}
if (service && job) {
throw new Error('Only one of `service` or `job` inputs can be set.');
}
// Validate gcloud component input
if (gcloudComponent && gcloudComponent !== 'alpha' && gcloudComponent !== 'beta') {
throw new Error(`invalid input received for gcloud_component: ${gcloudComponent}`);
}
// Find base command
if (metadata) {
const contents = await readFile(metadata, 'utf8');
const parsed = parseYAML(contents);
// Extract service name from metadata template
const name = parsed?.metadata?.name;
if (!name) {
throw new Error(`${metadata} is missing 'metadata.name'`);
}
if (service && service != name) {
throw new Error(
`service name in ${metadata} ("${name}") does not match GitHub ` +
`Actions service input ("${service}")`,
);
}
service = name;
const kind = parsed?.kind;
if (kind === 'Service') {
deployCmd = ['run', 'services', 'replace', metadata];
} else if (kind === 'Job') {
deployCmd = ['run', 'jobs', 'replace', metadata];
} else {
throw new Error(`Unkown metadata type "${kind}", expected "Job" or "Service"`);
}
} else if (job) {
deployCmd = ['run', 'jobs', 'deploy', job];
if (image) {
deployCmd.push('--image', image);
} else if (source) {
deployCmd.push('--source', source);
}
// Set optional flags from inputs
setEnvVarsFlags(deployCmd, envVars, envVarsUpdateStrategy);
setSecretsFlags(deployCmd, secrets, secretsUpdateStrategy);
if (wait) {
deployCmd.push('--wait');
}
// Compile the labels
const defLabels = skipDefaultLabels ? {} : defaultLabels();
const compiledLabels = Object.assign({}, defLabels, labels);
if (compiledLabels && Object.keys(compiledLabels).length > 0) {
deployCmd.push('--labels', joinKVStringForGCloud(compiledLabels));
}
} else {
deployCmd = ['run', 'deploy', service];
if (image) {
deployCmd.push('--image', image);
} else if (source) {
deployCmd.push('--source', source);
}
// Set optional flags from inputs
setEnvVarsFlags(deployCmd, envVars, envVarsUpdateStrategy);
setSecretsFlags(deployCmd, secrets, secretsUpdateStrategy);
if (tag) {
deployCmd.push('--tag', tag);
}
if (suffix) deployCmd.push('--revision-suffix', suffix);
if (noTraffic) deployCmd.push('--no-traffic');
if (timeout) deployCmd.push('--timeout', timeout);
// Compile the labels
const defLabels = skipDefaultLabels ? {} : defaultLabels();
const compiledLabels = Object.assign({}, defLabels, labels);
if (compiledLabels && Object.keys(compiledLabels).length > 0) {
deployCmd.push('--update-labels', joinKVStringForGCloud(compiledLabels));
}
}
// Traffic flags
let updateTrafficCmd = ['run', 'services', 'update-traffic', service];
if (revTraffic) updateTrafficCmd.push('--to-revisions', revTraffic);
if (tagTraffic) updateTrafficCmd.push('--to-tags', tagTraffic);
// Push common flags
deployCmd.push('--format', 'json');
updateTrafficCmd.push('--format', 'json');
if (region?.length > 0) {
const regions = region
.flat()
.filter((e) => e !== undefined && e !== null && e !== '')
.join(',');
deployCmd.push('--region', regions);
updateTrafficCmd.push('--region', regions);
}
if (projectId) {
deployCmd.push('--project', projectId);
updateTrafficCmd.push('--project', projectId);
}
// Add optional deploy flags
if (flags) {
const flagList = parseFlags(flags);
if (flagList) {
deployCmd = deployCmd.concat(flagList);
}
}
// Add optional update-traffic flags
if (updateTrafficFlags) {
const flagList = parseFlags(updateTrafficFlags);
if (flagList) {
updateTrafficCmd = updateTrafficCmd.concat(flagList);
}
}
// Install gcloud if not already installed.
if (!isGcloudInstalled(gcloudVersion)) {
await installGcloudSDK(gcloudVersion);
} else {
const toolPath = toolCache.find('gcloud', gcloudVersion);
addPath(path.join(toolPath, 'bin'));
}
// Install gcloud component if needed and prepend the command
if (gcloudComponent) {
await installGcloudComponent(gcloudComponent);
deployCmd.unshift(gcloudComponent);
updateTrafficCmd.unshift(gcloudComponent);
}
// Authenticate - this comes from google-github-actions/auth.
const credFile = process.env.GOOGLE_GHA_CREDS_PATH;
if (credFile) {
await authenticateGcloudSDK(credFile);
logInfo('Successfully authenticated');
} else {
logWarning('No authentication found, authenticate with `google-github-actions/auth`.');
}
const toolCommand = getToolCommand();
const options = { silent: !isDebug, ignoreReturnCode: true };
const commandString = `${toolCommand} ${deployCmd.join(' ')}`;
logInfo(`Running: ${commandString}`);
logDebug(
JSON.stringify({ toolCommand: toolCommand, args: deployCmd, options: options }, null, ' '),
);
// Run deploy command
const deployCmdExec = await getExecOutput(toolCommand, deployCmd, options);
if (deployCmdExec.exitCode !== 0) {
const errMsg =
deployCmdExec.stderr ||
`command exited ${deployCmdExec.exitCode}, but stderr had no output`;
throw new Error(`failed to deploy: ${errMsg}, full command:\n\t${commandString}`);
}
setActionOutputs(parseDeployResponse(deployCmdExec.stdout, { tag: tag }));
// Run revision/tag command
if (revTraffic || tagTraffic) {
const updateTrafficExec = await getExecOutput(toolCommand, updateTrafficCmd, options);
if (updateTrafficExec.exitCode !== 0) {
const errMsg =
updateTrafficExec.stderr ||
`command exited ${updateTrafficExec.exitCode}, but stderr had no output`;
throw new Error(`failed to update traffic: ${errMsg}, full command:\n\t${commandString}`);
}
setActionOutputs(parseUpdateTrafficResponse(updateTrafficExec.stdout));
}
} catch (err) {
const msg = errorMessage(err);
setFailed(`google-github-actions/deploy-cloudrun failed with: ${msg}`);
}
}
// Map output response to GitHub Action outputs
export function setActionOutputs(outputs: DeployCloudRunOutputs): void {
Object.keys(outputs).forEach((key: string) => {
setOutput(key, outputs[key as keyof DeployCloudRunOutputs]);
});
}
/**
* defaultLabels returns the default labels to apply to the Cloud Run service.
*
* @return KVPair
*/
function defaultLabels(): KVPair {
const rawValues: Record<string, string | undefined> = {
'managed-by': 'github-actions',
'commit-sha': process.env.GITHUB_SHA,
};
const labels: KVPair = {};
for (const key in rawValues) {
const value = rawValues[key];
if (value) {
// Labels can only be lowercase
labels[key] = value.toLowerCase();
}
}
return labels;
}
/**
* computeGcloudVersion computes the appropriate gcloud version for the given
* string.
*/
async function computeGcloudVersion(str: string): Promise<string> {
str = (str || '').trim();
if (str === '' || str === 'latest') {
return await getLatestGcloudSDKVersion();
}
return str;
}
function setEnvVarsFlags(cmd: string[], envVars: string, strategy: string) {
const compiledEnvVars = parseKVString(envVars);
if (compiledEnvVars && Object.keys(compiledEnvVars).length > 0) {
let flag = '';
if (strategy === 'overwrite') {
flag = '--set-env-vars';
} else if (strategy === 'merge') {
flag = '--update-env-vars';
} else {
throw new Error(
`Invalid "env_vars_update_strategy" value "${strategy}", valid values ` +
`are "overwrite" and "merge".`,
);
}
cmd.push(flag, joinKVStringForGCloud(compiledEnvVars));
}
}
function setSecretsFlags(cmd: string[], secrets: KVPair | undefined, strategy: string) {
if (secrets && Object.keys(secrets).length > 0) {
let flag = '';
if (strategy === 'overwrite') {
flag = '--set-secrets';
} else if (strategy === 'merge') {
flag = '--update-secrets';
} else {
throw new Error(
`Invalid "secrets_update_strategy" value "${strategy}", valid values ` +
`are "overwrite" and "merge".`,
);
}
cmd.push(flag, joinKVStringForGCloud(secrets));
}
}
/**
* execute the main function when this module is required directly.
*/
if (require.main === module) {
run();
}