-
Notifications
You must be signed in to change notification settings - Fork 938
/
github.ts
588 lines (513 loc) · 17.6 KB
/
github.ts
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
import { bold } from "cli-color";
import * as fs from "fs";
import * as yaml from "js-yaml";
import { safeLoad } from "js-yaml";
import * as ora from "ora";
import * as path from "path";
import * as sodium from "tweetsodium";
import { Setup } from "../..";
import { login } from "../../../auth";
import { dirExistsSync } from "../../../fsutils";
import {
createServiceAccount,
createServiceAccountKey,
deleteServiceAccount,
} from "../../../gcp/iam";
import { addServiceAccountToRoles, firebaseRoles } from "../../../gcp/resourceManager";
import * as logger from "../../../logger";
import { prompt } from "../../../prompt";
import { logBullet, logLabeledBullet, logSuccess } from "../../../utils";
import api = require("../../../api");
let GIT_DIR: string;
let GITHUB_DIR: string;
let WORKFLOW_DIR: string;
let YML_FULL_PATH_PULL_REQUEST: string;
let YML_FULL_PATH_MERGE: string;
const YML_PULL_REQUEST_FILENAME = "firebase-hosting-pull-request.yml";
const YML_MERGE_FILENAME = "firebase-hosting-merge.yml";
const CHECKOUT_GITHUB_ACTION_NAME = "actions/checkout@v2";
const HOSTING_GITHUB_ACTION_NAME = "FirebaseExtended/action-hosting-deploy@v0";
/**
* Assists in setting up a GitHub workflow by doing the following:
* - Creates a GCP service account with permission to deploy to Hosting
* - Encrypts that service account's JSON key and uploads it to the specified GitHub repository as a Secret
* - https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
* - Writes GitHub workflow yaml configuration files that reference the newly created secret
* to configure the Deploy to Firebase Hosting GitHub Action
* - https://github.com/marketplace/actions/deploy-to-firebase-hosting
* - https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
*
* @param setup A helper object provided by the `firebase init` command.
* @param config Configuration for the project.
* @param options Command line options.
*/
export async function initGitHub(setup: Setup, config: any, options: any): Promise<void> {
logger.info();
// Find existing Git/Github config
const gitRoot = getGitFolderPath();
GIT_DIR = path.join(gitRoot, ".git");
GITHUB_DIR = path.join(gitRoot, ".github");
WORKFLOW_DIR = `${GITHUB_DIR}/workflows`;
YML_FULL_PATH_PULL_REQUEST = `${WORKFLOW_DIR}/${YML_PULL_REQUEST_FILENAME}`;
YML_FULL_PATH_MERGE = `${WORKFLOW_DIR}/${YML_MERGE_FILENAME}`;
// GitHub Oauth
logBullet(
"Authorizing with GitHub to upload your service account to a GitHub repository's secrets store."
);
const ghAccessToken = await signInWithGitHub();
// Get GitHub user Details
const userDetails = await getGitHubUserDetails(ghAccessToken);
const ghUserName = userDetails.login;
logger.info();
logSuccess(`Success! Logged into GitHub as ${bold(ghUserName)}`);
logger.info();
// Prompt for repo and validate by getting the public key
const { repo, key, keyId } = await promptForRepo(setup, ghAccessToken);
const { default_branch: defaultBranch, id: repoId } = await getRepoDetails(repo, ghAccessToken);
// Valid secret names:
// https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#naming-your-secrets
const githubSecretName = `FIREBASE_SERVICE_ACCOUNT_${setup.projectId
.replace(/-/g, "_")
.toUpperCase()}`;
const serviceAccountName = `github-action-${repoId}`;
const serviceAccountJSON = await createServiceAccountAndKeyWithRetry(
setup,
repo,
serviceAccountName
);
logger.info();
logSuccess(
`Created service account ${bold(serviceAccountName)} with Firebase Hosting admin permissions.`
);
const spinnerSecrets = ora.default(`Uploading service account secrets to repository: ${repo}`);
spinnerSecrets.start();
const encryptedServiceAccountJSON = encryptServiceAccountJSON(serviceAccountJSON, key);
await uploadSecretToGitHub(
repo,
ghAccessToken,
encryptedServiceAccountJSON,
keyId,
githubSecretName
);
spinnerSecrets.stop();
logSuccess(`Uploaded service account JSON to GitHub as secret ${bold(githubSecretName)}.`);
logBullet(`You can manage your secrets at https://github.com/${repo}/settings/secrets.`);
logger.info();
// If the developer is using predeploy scripts in firebase.json,
// remind them before they set up a script in their workflow file.
if (setup.config.hosting.predeploy) {
logBullet(`You have a predeploy script configured in firebase.json.`);
}
const { script } = await promptForBuildScript();
const ymlDeployDoc = loadYMLDeploy();
let shouldWriteYMLHostingFile = true;
let shouldWriteYMLDeployFile = false;
// If the preview YML file exists, ask the user to overwrite. This file is generated by
// the CLI and rarely touched by the user.
if (fs.existsSync(YML_FULL_PATH_PULL_REQUEST)) {
const { overwrite } = await promptForWriteYMLFile({
message: `GitHub workflow file for PR previews exists. Overwrite? ${YML_PULL_REQUEST_FILENAME}`,
});
shouldWriteYMLHostingFile = overwrite;
}
if (shouldWriteYMLHostingFile) {
writeChannelActionYMLFile(
YML_FULL_PATH_PULL_REQUEST,
githubSecretName,
setup.projectId,
script
);
logger.info();
logSuccess(`Created workflow file ${bold(YML_FULL_PATH_PULL_REQUEST)}`);
}
const { setupDeploys, branch } = await promptToSetupDeploys(ymlDeployDoc.branch || defaultBranch);
// If the user has an existing YML file for deploys to production, we need to
// check the branch used in the file against the branch supplied by the user
// in the prompt. If those values are different, we will overwrite the YML
// file without consent from the user because the deploy will break if they
// keep the old file. If the values are the same, we will prompt for consent.
if (setupDeploys) {
if (ymlDeployDoc.exists) {
if (ymlDeployDoc.branch !== branch) {
shouldWriteYMLDeployFile = true;
} else {
const { overwrite } = await promptForWriteYMLFile({
message: `The GitHub workflow file for deploying to the live channel already exists. Overwrite? ${YML_MERGE_FILENAME}`,
});
shouldWriteYMLDeployFile = overwrite;
}
} else {
shouldWriteYMLDeployFile = true;
}
if (shouldWriteYMLDeployFile) {
writeDeployToProdActionYMLFile(
YML_FULL_PATH_MERGE,
branch,
githubSecretName,
setup.projectId,
script
);
logger.info();
logSuccess(`Created workflow file ${bold(YML_FULL_PATH_MERGE)}`);
}
}
logger.info();
logLabeledBullet(
"Action required",
`Visit this URL to revoke authorization for the Firebase CLI GitHub OAuth App:`
);
logger.info(
bold.underline(`https://github.com/settings/connections/applications/${api.githubClientId}`)
);
logLabeledBullet("Action required", `Push any new workflow file(s) to your repo`);
}
/**
* Finds the folder that contains the .git folder
*
* For example, if the .git folder is /Users/sparky/projects/my-web-app/.git
* This function will return /Users/sparky/projects/my-web-app
*
* Modeled after https://github.com/firebase/firebase-tools/blob/master/src/detectProjectRoot.ts
*
* @return {string} The folder that contains the .git folder
*/
function getGitFolderPath(): string {
const commandDir = process.cwd();
let projectRootDir = commandDir;
while (!fs.existsSync(path.resolve(projectRootDir, ".git"))) {
const parentDir = path.dirname(projectRootDir);
// Stop searching if we get to the root of the filesystem
if (parentDir === projectRootDir) {
logBullet(`Didn't detect a .git folder. Assuming ${commandDir} is the project root.`);
return commandDir;
}
projectRootDir = parentDir;
}
logBullet(`Detected a .git folder at ${projectRootDir}`);
return projectRootDir;
}
function defaultGithubRepo(): string | undefined {
const gitConfigPath = path.join(GIT_DIR, "config");
if (fs.existsSync(gitConfigPath)) {
const gitConfig = fs.readFileSync(gitConfigPath, "utf8");
const match = /github\.com:(.+)\.git/.exec(gitConfig);
if (match) {
return match[1];
}
}
return undefined;
}
function loadYMLDeploy(): { exists: boolean; branch?: string } {
if (fs.existsSync(YML_FULL_PATH_MERGE)) {
const { on } = loadYML(YML_FULL_PATH_MERGE);
const branch = on.push.branches[0];
return { exists: true, branch };
} else {
return { exists: false };
}
}
function loadYML(ymlPath: string) {
return safeLoad(fs.readFileSync(ymlPath, "utf8"));
}
function mkdirNotExists(dir: string): void {
if (!dirExistsSync(dir)) {
fs.mkdirSync(dir);
}
}
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions
type GitHubWorkflowConfig = {
name: string;
on: string | { [key: string]: { [key: string]: string[] } };
jobs: {
[key: string]: {
"runs-on": string;
steps: {
uses?: string;
run?: string;
with?: { [key: string]: string };
env?: { [key: string]: string };
}[];
};
};
};
function writeChannelActionYMLFile(
ymlPath: string,
secretName: string,
projectId: string,
script?: string
): void {
const workflowConfig: GitHubWorkflowConfig = {
name: "Deploy to Firebase Hosting on PR",
on: "pull_request",
jobs: {
["build_and_preview"]: {
"runs-on": "ubuntu-latest",
steps: [{ uses: CHECKOUT_GITHUB_ACTION_NAME }],
},
},
};
if (script) {
workflowConfig.jobs.build_and_preview.steps.push({
run: script,
});
}
workflowConfig.jobs.build_and_preview.steps.push({
uses: HOSTING_GITHUB_ACTION_NAME,
with: {
repoToken: "${{ secrets.GITHUB_TOKEN }}",
firebaseServiceAccount: `\${{ secrets.${secretName} }}`,
projectId: projectId,
},
env: {
FIREBASE_CLI_PREVIEWS: "hostingchannels",
},
});
const ymlContents = `# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools
${yaml.safeDump(workflowConfig)}`;
mkdirNotExists(GITHUB_DIR);
mkdirNotExists(WORKFLOW_DIR);
fs.writeFileSync(ymlPath, ymlContents, "utf8");
}
function writeDeployToProdActionYMLFile(
ymlPath: string,
branch: string | undefined,
secretName: string,
projectId: string,
script?: string
): void {
const workflowConfig: GitHubWorkflowConfig = {
name: "Deploy to Firebase Hosting on merge",
on: { push: { branches: [branch || "master"] } },
jobs: {
["build_and_deploy"]: {
"runs-on": "ubuntu-latest",
steps: [{ uses: CHECKOUT_GITHUB_ACTION_NAME }],
},
},
};
if (script) {
workflowConfig.jobs.build_and_deploy.steps.push({
run: script,
});
}
workflowConfig.jobs.build_and_deploy.steps.push({
uses: HOSTING_GITHUB_ACTION_NAME,
with: {
repoToken: "${{ secrets.GITHUB_TOKEN }}",
firebaseServiceAccount: `\${{ secrets.${secretName} }}`,
channelId: "live",
projectId: projectId,
},
env: {
FIREBASE_CLI_PREVIEWS: "hostingchannels",
},
});
const ymlContents = `# This file was auto-generated by the Firebase CLI
# https://github.com/firebase/firebase-tools
${yaml.safeDump(workflowConfig)}`;
mkdirNotExists(GITHUB_DIR);
mkdirNotExists(WORKFLOW_DIR);
fs.writeFileSync(ymlPath, ymlContents, "utf8");
}
async function uploadSecretToGitHub(
repo: string,
ghAccessToken: string,
encryptedServiceAccountJSON: string,
keyId: string,
secretName: string
): Promise<{ status: any }> {
return await api.request("PUT", `/repos/${repo}/actions/secrets/${secretName}`, {
origin: api.githubApiOrigin,
headers: { Authorization: `token ${ghAccessToken}`, "User-Agent": "Firebase CLI" },
data: {
["encrypted_value"]: encryptedServiceAccountJSON,
["key_id"]: keyId,
},
});
}
async function promptForRepo(
options: any,
ghAccessToken: string
): Promise<{ repo: string; key: string; keyId: string }> {
let key = "";
let keyId = "";
const { repo } = await prompt(options, [
{
type: "input",
name: "repo",
default: defaultGithubRepo(), // TODO look at github origin
message: "For which GitHub repository would you like to set up a GitHub workflow?",
validate: async (repo: string) => {
const { body } = await api.request("GET", `/repos/${repo}/actions/secrets/public-key`, {
origin: api.githubApiOrigin,
headers: { Authorization: `token ${ghAccessToken}`, "User-Agent": "Firebase CLI" },
data: {
type: "owner",
},
});
key = body.key;
keyId = body.key_id;
return true;
},
},
]);
return { repo, key, keyId };
}
async function promptForBuildScript(): Promise<{ script?: string }> {
const { shouldSetupScript } = await prompt({}, [
{
type: "confirm",
name: "shouldSetupScript",
default: false,
message: "Set up the workflow to run a build script before every deploy?",
},
]);
if (!shouldSetupScript) {
return { script: undefined };
}
const { script } = await prompt({}, [
{
type: "input",
name: "script",
default: "npm ci && npm run build",
message: "What script should be run before every deploy?",
},
]);
return { script };
}
async function promptToSetupDeploys(
defaultBranch: string
): Promise<{ setupDeploys: boolean; branch?: string }> {
const { setupDeploys } = await prompt({}, [
{
type: "confirm",
name: "setupDeploys",
default: true,
message: "Set up automatic deployment to your site's live channel when a PR is merged?",
},
]);
if (!setupDeploys) {
return { setupDeploys };
}
const { branch } = await prompt({}, [
{
type: "input",
name: "branch",
default: defaultBranch,
message: "What is the name of the GitHub branch associated with your site's live channel?",
},
]);
return { branch, setupDeploys };
}
async function promptForWriteYMLFile({ message }: { message: string }) {
return await prompt({}, [
{
type: "confirm",
name: "overwrite",
default: false,
message,
},
]);
}
async function getGitHubUserDetails(ghAccessToken: any): Promise<Record<string, any>> {
const { body: ghUserDetails } = await api.request("GET", `/user`, {
origin: api.githubApiOrigin,
headers: { Authorization: `token ${ghAccessToken}`, "User-Agent": "Firebase CLI" },
});
return ghUserDetails;
}
async function getRepoDetails(repo: string, ghAccessToken: string) {
const { body } = await api.request("GET", `/repos/${repo}`, {
origin: api.githubApiOrigin,
headers: { Authorization: `token ${ghAccessToken}`, "User-Agent": "Firebase CLI" },
});
return body;
}
async function signInWithGitHub() {
return await login(true, null, "GITHUB");
}
async function createServiceAccountAndKeyWithRetry(
options: any,
repo: string,
accountId: string
): Promise<string> {
const spinnerServiceAccount = ora.default("Retrieving a service account.");
spinnerServiceAccount.start();
try {
const serviceAccountJSON = await createServiceAccountAndKey(options, repo, accountId);
spinnerServiceAccount.stop();
return serviceAccountJSON;
} catch (e) {
spinnerServiceAccount.stop();
if (!e.message.includes("429")) {
throw e;
}
// TODO prompt if they want to recreate the service account
spinnerServiceAccount.start();
await deleteServiceAccount(
options.projectId,
`${accountId}@${options.projectId}.iam.gserviceaccount.com`
);
const serviceAccountJSON = await createServiceAccountAndKey(options, repo, accountId);
spinnerServiceAccount.stop();
return serviceAccountJSON;
}
}
async function createServiceAccountAndKey(
options: any,
repo: string,
accountId: string
): Promise<string> {
try {
await createServiceAccount(
options.projectId,
accountId,
`A service account with permission to deploy to Firebase Hosting for the GitHub repository ${repo}`,
`GitHub Actions (${repo})`
);
} catch (e) {
// No need to throw if there is an existing service account
if (!e.message.includes("409")) {
throw e;
}
}
// The roles for the service account.
// https://firebase.google.com/docs/projects/iam/roles-predefined-product#hosting
const requiredRoles = [
firebaseRoles.hostingAdmin,
firebaseRoles.apiKeysViewer,
firebaseRoles.runViewer,
];
await addServiceAccountToRoles(options.projectId, accountId, requiredRoles);
const serviceAccountKey = await createServiceAccountKey(options.projectId, accountId);
const buf = Buffer.from(serviceAccountKey.privateKeyData, "base64");
const serviceAccountJSON = buf.toString();
return serviceAccountJSON;
/*
TODO if too many keys error, delete service account and retry on prompt
await deleteServiceAccount(
options.projectId,
`${accountId}@${options.projectId}.iam.gserviceaccount.com`
);
return createServiceAccountAndKey(options, repo, accountId);
*/
}
/**
* Encrypt service account to prepare to upload as a secret
* using the method recommended in the GitHub docs:
* https://developer.github.com/v3/actions/secrets/#create-or-update-a-repository-secret
*
* @param serviceAccountJSON A service account's JSON private key
* @param key a GitHub repository's public key
*
* @return {string} The encrypted service account key
*/
function encryptServiceAccountJSON(serviceAccountJSON: string, key: string): string {
const messageBytes = Buffer.from(serviceAccountJSON);
const keyBytes = Buffer.from(key, "base64");
// Encrypt using LibSodium.
const encryptedBytes = sodium.seal(messageBytes, keyBytes);
// Base64 the encrypted secret
return Buffer.from(encryptedBytes).toString("base64");
}