MCPcopy Create free account
hub / github.com/firebase/firebase-tools / initGitHub

Function initGitHub

src/init/features/hosting/github.ts:55–214  ·  view source on GitHub ↗
(setup: Setup)

Source from the content-addressed store, hash-verified

53 * @param options Command line options.
54 */
55export async function initGitHub(setup: Setup): Promise<void> {
56 if (!setup.projectId) {
57 return reject("Could not determine Project ID, can't set up GitHub workflow.", { exit: 1 });
58 }
59
60 if (!setup.config.hosting && !setup.featureInfo?.hosting) {
61 return reject(
62 `Didn't find a Hosting config in firebase.json. Run ${bold("firebase init hosting")} instead.`,
63 );
64 }
65
66 logger.info();
67
68 // Find existing Git/Github config
69 const gitRoot = getGitFolderPath();
70 GIT_DIR = path.join(gitRoot, ".git");
71 GITHUB_DIR = path.join(gitRoot, ".github");
72 WORKFLOW_DIR = `${GITHUB_DIR}/workflows`;
73 YML_FULL_PATH_PULL_REQUEST = `${WORKFLOW_DIR}/${YML_PULL_REQUEST_FILENAME}`;
74 YML_FULL_PATH_MERGE = `${WORKFLOW_DIR}/${YML_MERGE_FILENAME}`;
75
76 // GitHub Oauth
77 logBullet(
78 "Authorizing with GitHub to upload your service account to a GitHub repository's secrets store.",
79 );
80
81 const ghAccessToken = await signInWithGitHub();
82
83 // Get GitHub user Details
84 const userDetails = await getGitHubUserDetails(ghAccessToken);
85 const ghUserName = userDetails.login;
86
87 logger.info();
88 logSuccess(`Success! Logged into GitHub as ${bold(ghUserName)}`);
89 logger.info();
90
91 // Prompt for repo and validate by getting the public key
92 const { repo, key, keyId } = await promptForRepo(setup, ghAccessToken);
93
94 const { default_branch: defaultBranch, id: repoId } = await getRepoDetails(repo, ghAccessToken);
95
96 // Valid secret names:
97 // https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#naming-your-secrets
98 const githubSecretName = `FIREBASE_SERVICE_ACCOUNT_${setup.projectId
99 .replace(/-/g, "_")
100 .toUpperCase()}`;
101
102 const serviceAccountName = `github-action-${repoId}`;
103
104 const serviceAccountJSON = await createServiceAccountAndKeyWithRetry(
105 setup,
106 repo,
107 serviceAccountName,
108 );
109
110 logger.info();
111 logSuccess(
112 `Created service account ${bold(serviceAccountName)} with Firebase Hosting admin permissions.`,

Callers

nothing calls this directly

Calls 15

rejectFunction · 0.90
logBulletFunction · 0.90
logSuccessFunction · 0.90
confirmFunction · 0.90
logLabeledBulletFunction · 0.90
githubClientIdFunction · 0.90
getGitFolderPathFunction · 0.85
signInWithGitHubFunction · 0.85
getGitHubUserDetailsFunction · 0.85
promptForRepoFunction · 0.85
getRepoDetailsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…