Skip to content

Commit d82ffbc

Browse files
committed
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 e3fb1c2 commit d82ffbc

File tree

4 files changed

+87
-132
lines changed

4 files changed

+87
-132
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 & 83 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,7 +43,6 @@ 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");
@@ -53,13 +51,6 @@ const firebaseToolsVersion = getInput("firebaseToolsVersion");
5351
const disableComment = getInput("disableComment");
5452

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

@@ -75,97 +66,93 @@ async function run() {
7566
if (existsSync("./firebase.json")) {
7667
console.log("firebase.json file found. Continuing deploy.");
7768
} else {
78-
throw Error(
79-
"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."
80-
);
69+
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.");
8170
}
8271
endGroup();
8372

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

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

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

135-
setOutput("urls", urls);
136-
setOutput("expire_time", expireTime);
137-
setOutput("expire_time_formatted", expire_time_formatted);
138-
setOutput("details_url", urls[0]);
120+
async function deployToPreviewChannel(gacFilePath: string, channelId: string) {
121+
const deployment = await deployPreview(gacFilePath, {
122+
projectId,
123+
expires,
124+
channelId,
125+
target,
126+
firebaseToolsVersion,
127+
});
128+
if (deployment.status === "error") {
129+
throw Error((deployment as ErrorResult).error);
130+
}
139131

140-
if (disableComment === "true") {
141-
console.log(
142-
`Commenting on PR is disabled with "disableComment: ${disableComment}"`
143-
);
144-
} else if (token && isPullRequest && !!octokit) {
145-
const commitId = context.payload.pull_request?.head.sha.substring(0, 7);
132+
const { expireTime, urls } = interpretChannelDeployResult(deployment);
133+
setOutput("urls", urls);
134+
setOutput("expire_time", expireTime);
135+
setOutput("details_url", urls[0]);
146136

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

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

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

171158
run();

src/postOrUpdateComment.ts

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

8080
startGroup(`Commenting on PR`);
81+
8182
const deploySignature = createDeploySignature(result);
8283
const isCommentByBot = createBotCommentIdentifier(deploySignature);
8384

8485
let commentId;
8586
try {
8687
const comments = (await github.rest.issues.listComments(commentInfo)).data;
87-
for (let i = comments.length; i--; ) {
88+
for (let i = comments.length; i--;) {
8889
const c = comments[i];
8990
if (isCommentByBot(c)) {
9091
commentId = c.id;
9192
break;
9293
}
9394
}
9495
} catch (e) {
95-
console.log("Error checking for previous comments: " + e.message);
96+
console.error("Error checking for previous comments: " + e.message);
9697
}
9798

98-
if (commentId) {
99+
if (!!commentId) {
100+
console.log("Found existing comment.");
99101
try {
100102
await github.rest.issues.updateComment({
101103
...context.repo,
@@ -106,12 +108,12 @@ export async function postChannelSuccessComment(
106108
commentId = null;
107109
}
108110
}
109-
110-
if (!commentId) {
111+
else {
112+
console.log("New comment will be created.");
111113
try {
112114
await github.rest.issues.createComment(comment);
113115
} catch (e) {
114-
console.log(`Error creating comment: ${e.message}`);
116+
console.error(`Error creating comment: ${e.message}`);
115117
}
116118
}
117119
endGroup();

0 commit comments

Comments
 (0)