Skip to content

Commit

Permalink
chore: cleanup temp file at the end of the action
Browse files Browse the repository at this point in the history
  • Loading branch information
minherz committed Mar 25, 2024
1 parent bb7817d commit 5121ca6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
15 changes: 7 additions & 8 deletions src/createGACFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
import { fileSync } from "tmp";
import { writeSync, existsSync } from "fs";

// Set up Google Application Credentials for use by the Firebase CLI
// https://cloud.google.com/docs/authentication/production#finding_credentials_automatically
export async function createGacFile(googleApplicationCredentials: string) {
// creates file with GAC info if parameter is not already a path to a file
// NOTE: no validation of the credential information is performed
export async function createGacFile(gacInfo: string) {
try {
if (existsSync(googleApplicationCredentials)) {
return googleApplicationCredentials;
if (existsSync(gacInfo)) {
return gacInfo;
}
}
catch (e) {
console.log("debug: failed checking file existence with error %O", e);
// googleApplicationCredentials is not a path to a file
console.warn("unexpected error while validing GAC info. Interpreting provided info as credentials data.");
}
const tmpFile = fileSync({ postfix: ".json" });
writeSync(tmpFile.fd, googleApplicationCredentials);
writeSync(tmpFile.fd, gacInfo);
return tmpFile.name;
}
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
startGroup,
} from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { existsSync } from "fs";
import { existsSync, unlinkSync } from "fs";
import { createGacFile } from "./createGACFile";
import {
deployPreview,
Expand Down Expand Up @@ -51,7 +51,7 @@ const firebaseToolsVersion = getInput("firebaseToolsVersion");

async function run() {
try {
startGroup("Verifying firebase.json exists");
startGroup("Verifying setup parameters");

if (entryPoint !== ".") {
console.log(`Changing to directory: ${entryPoint}`);
Expand All @@ -61,21 +61,15 @@ async function run() {
throw Error(`Error changing to directory ${entryPoint}: ${err}`);
}
}

if (existsSync("./firebase.json")) {
console.log("firebase.json file found. Continuing deploy.");
} else {
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.");
}
endGroup();

startGroup("Setting up CLI credentials");
console.log("validating passed credentials: %s", googleApplicationCredentials);
const gacFilename = await createGacFile(googleApplicationCredentials);
console.log("generated credentials: %s", gacFilename)
if (gacFilename !== googleApplicationCredentials) {
console.log(
"Created a temporary file with Application Default Credentials."
"Created a temporary file with Google Application Credentials."
);
}
endGroup();
Expand All @@ -90,6 +84,11 @@ async function run() {
await deployToPreviewChannel(gacFilename, channelId);
endGroup();
}

// cleanup
if (gacFilename !== googleApplicationCredentials) {
unlinkSync(gacFilename);
}
} catch (e) {
setFailed(e.message);
}
Expand Down

0 comments on commit 5121ca6

Please sign in to comment.