Skip to content

Commit aad2200

Browse files
authored
fix: remove unnecessary check and update documentation (#2)
refactor code to stop creating an empty action. refactor code to move deploy code to stand alone methods. change log severity to report errors. add extra info logs to improve verbosity of the action.
1 parent f8bd3d3 commit aad2200

File tree

4 files changed

+87
-131
lines changed

4 files changed

+87
-131
lines changed

src/createCheck.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

src/createGACFile.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
*/
1616

1717
import { fileSync } from "tmp";
18-
import { writeSync } from "fs";
18+
import { writeSync, existsSync } from "fs";
1919

2020
// Set up Google Application Credentials for use by the Firebase CLI
2121
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
2222
export async function createGacFile(googleApplicationCredentials: string) {
23+
try {
24+
if (existsSync(googleApplicationCredentials)) {
25+
return googleApplicationCredentials;
26+
}
27+
}
28+
catch (e) {
29+
// googleApplicationCredentials is not a path to a file
30+
}
2331
const tmpFile = fileSync({ postfix: ".json" });
24-
2532
writeSync(tmpFile.fd, googleApplicationCredentials);
26-
2733
return tmpFile.name;
2834
}

src/index.ts

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
} from "@actions/core";
2424
import { context, getOctokit } from "@actions/github";
2525
import { existsSync } from "fs";
26-
import { createCheck } from "./createCheck";
2726
import { createGacFile } from "./createGACFile";
2827
import {
2928
deployPreview,
@@ -44,21 +43,13 @@ const googleApplicationCredentials = getInput("firebaseServiceAccount", {
4443
required: true,
4544
});
4645
const configuredChannelId = getInput("channelId");
47-
const isProductionDeploy = configuredChannelId === "live";
4846
const token = process.env.GITHUB_TOKEN || getInput("repoToken");
4947
const octokit = token ? getOctokit(token) : undefined;
5048
const entryPoint = getInput("entryPoint");
5149
const target = getInput("target");
5250
const firebaseToolsVersion = getInput("firebaseToolsVersion");
5351

5452
async function run() {
55-
const isPullRequest = !!context.payload.pull_request;
56-
57-
let finish = (details: Object) => console.log(details);
58-
if (token && isPullRequest) {
59-
finish = await createCheck(octokit, context);
60-
}
61-
6253
try {
6354
startGroup("Verifying firebase.json exists");
6455

@@ -74,96 +65,93 @@ async function run() {
7465
if (existsSync("./firebase.json")) {
7566
console.log("firebase.json file found. Continuing deploy.");
7667
} else {
77-
throw Error(
78-
"firebase.json file not found. If your firebase.json file is not in the root of your repo, edit the entryPoint option of this GitHub action."
79-
);
68+
console.warn("firebase.json file not found. If your firebase.json file is not in the root of your repo, edit the entryPoint option of this GitHub action.");
8069
}
8170
endGroup();
8271

8372
startGroup("Setting up CLI credentials");
8473
const gacFilename = await createGacFile(googleApplicationCredentials);
85-
console.log(
86-
"Created a temporary file with Application Default Credentials."
87-
);
74+
if (gacFilename != googleApplicationCredentials) {
75+
console.log(
76+
"Created a temporary file with Application Default Credentials."
77+
);
78+
}
8879
endGroup();
8980

90-
if (isProductionDeploy) {
81+
if (configuredChannelId === "live") {
9182
startGroup("Deploying to production site");
92-
const deployment = await deployProductionSite(gacFilename, {
93-
projectId,
94-
target,
95-
firebaseToolsVersion,
96-
});
97-
if (deployment.status === "error") {
98-
throw Error((deployment as ErrorResult).error);
99-
}
83+
await deployToProduction(gacFilename);
84+
endGroup();
85+
} else {
86+
const channelId = getChannelId(configuredChannelId, context);
87+
startGroup(`Deploying to Firebase preview channel ${channelId}`);
88+
await deployToPreviewChannel(gacFilename, channelId);
10089
endGroup();
101-
102-
const hostname = target ? `${target}.web.app` : `${projectId}.web.app`;
103-
const url = `https://${hostname}/`;
104-
await finish({
105-
details_url: url,
106-
conclusion: "success",
107-
output: {
108-
title: `Production deploy succeeded`,
109-
summary: `[${hostname}](${url})`,
110-
},
111-
});
112-
return;
113-
}
114-
115-
const channelId = getChannelId(configuredChannelId, context);
116-
117-
startGroup(`Deploying to Firebase preview channel ${channelId}`);
118-
const deployment = await deployPreview(gacFilename, {
119-
projectId,
120-
expires,
121-
channelId,
122-
target,
123-
firebaseToolsVersion,
124-
});
125-
126-
if (deployment.status === "error") {
127-
throw Error((deployment as ErrorResult).error);
12890
}
129-
endGroup();
130-
131-
const { expireTime, urls } = interpretChannelDeployResult(deployment);
91+
} catch (e) {
92+
setFailed(e.message);
93+
}
94+
return undefined;
95+
}
13296

133-
setOutput("urls", urls);
134-
setOutput("expire_time", expireTime);
135-
setOutput("details_url", urls[0]);
97+
async function deployToProduction(gacFilePath: string) {
98+
const deployment = await deployProductionSite(gacFilePath, {
99+
projectId,
100+
target,
101+
firebaseToolsVersion,
102+
});
103+
if (deployment.status === "error") {
104+
throw Error((deployment as ErrorResult).error);
105+
}
106+
const hostname = target ? `${target}.web.app` : `${projectId}.web.app`;
107+
const url = `https://${hostname}/`;
108+
console.log({
109+
details_url: url,
110+
conclusion: "success",
111+
output: {
112+
title: `Production deploy succeeded`,
113+
summary: `[${hostname}](${url})`,
114+
},
115+
});
116+
return undefined;
117+
}
136118

137-
const urlsListMarkdown =
138-
urls.length === 1
139-
? `[${urls[0]}](${urls[0]})`
140-
: urls.map((url) => `- [${url}](${url})`).join("\n");
119+
async function deployToPreviewChannel(gacFilePath: string, channelId: string) {
120+
const deployment = await deployPreview(gacFilePath, {
121+
projectId,
122+
expires,
123+
channelId,
124+
target,
125+
firebaseToolsVersion,
126+
});
127+
if (deployment.status === "error") {
128+
throw Error((deployment as ErrorResult).error);
129+
}
141130

142-
if (token && isPullRequest && !!octokit) {
143-
const commitId = context.payload.pull_request?.head.sha.substring(0, 7);
131+
const { expireTime, urls } = interpretChannelDeployResult(deployment);
132+
setOutput("urls", urls);
133+
setOutput("expire_time", expireTime);
134+
setOutput("details_url", urls[0]);
144135

145-
await postChannelSuccessComment(octokit, context, deployment, commitId);
146-
}
136+
const urlsListMarkdown =
137+
urls.length === 1
138+
? `[${urls[0]}](${urls[0]})`
139+
: urls.map((url) => `- [${url}](${url})`).join("\n");
147140

148-
await finish({
149-
details_url: urls[0],
150-
conclusion: "success",
151-
output: {
152-
title: `Deploy preview succeeded`,
153-
summary: getURLsMarkdownFromChannelDeployResult(deployment),
154-
},
155-
});
156-
} catch (e) {
157-
setFailed(e.message);
141+
if (token && !!context.payload.pull_request && !!octokit) {
142+
const commitId = context.payload.pull_request?.head.sha.substring(0, 7);
158143

159-
await finish({
160-
conclusion: "failure",
161-
output: {
162-
title: "Deploy preview failed",
163-
summary: `Error: ${e.message}`,
164-
},
165-
});
144+
await postChannelSuccessComment(octokit, context, deployment, commitId);
166145
}
146+
console.log({
147+
details_url: urls[0],
148+
conclusion: "success",
149+
output: {
150+
title: `Deploy preview succeeded`,
151+
summary: getURLsMarkdownFromChannelDeployResult(deployment),
152+
},
153+
});
154+
return undefined;
167155
}
168156

169157
run();

src/postOrUpdateComment.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,26 @@ export async function postChannelSuccessComment(
8282
};
8383

8484
startGroup(`Commenting on PR`);
85+
8586
const deploySignature = createDeploySignature(result);
8687
const isCommentByBot = createBotCommentIdentifier(deploySignature);
8788

8889
let commentId;
8990
try {
9091
const comments = (await github.rest.issues.listComments(commentInfo)).data;
91-
for (let i = comments.length; i--; ) {
92+
for (let i = comments.length; i--;) {
9293
const c = comments[i];
9394
if (isCommentByBot(c)) {
9495
commentId = c.id;
9596
break;
9697
}
9798
}
9899
} catch (e) {
99-
console.log("Error checking for previous comments: " + e.message);
100+
console.error("Error checking for previous comments: " + e.message);
100101
}
101102

102-
if (commentId) {
103+
if (!!commentId) {
104+
console.log("Found existing comment.");
103105
try {
104106
await github.rest.issues.updateComment({
105107
...context.repo,
@@ -110,12 +112,12 @@ export async function postChannelSuccessComment(
110112
commentId = null;
111113
}
112114
}
113-
114-
if (!commentId) {
115+
else {
116+
console.log("New comment will be created.");
115117
try {
116118
await github.rest.issues.createComment(comment);
117119
} catch (e) {
118-
console.log(`Error creating comment: ${e.message}`);
120+
console.error(`Error creating comment: ${e.message}`);
119121
}
120122
}
121123
endGroup();

0 commit comments

Comments
 (0)