From 247dcbb60f44c55c63cace417389cf717cf5dfce Mon Sep 17 00:00:00 2001 From: James Ives Date: Sat, 21 Dec 2019 14:58:59 -0500 Subject: [PATCH] [Issue-89] Post Deployment Tasks (#90) * cleanup * Changes * Post cleanup2 * Changes * Update git.test.ts --- __tests__/git.test.ts | 5 +++-- lib/git.js | 16 +++++++++++----- lib/main.js | 3 ++- src/git.ts | 20 +++++++++++++++----- src/main.ts | 3 ++- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 552ed902..9af38a02 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -1,5 +1,6 @@ // Initial env variable setup for tests. process.env["INPUT_FOLDER"] = "build"; +process.env["GITHUB_SHA"] = "123" import { execute } from "../src/util"; import { init, generateBranch, deploy } from "../src/git"; @@ -114,7 +115,7 @@ describe("git", () => { it('should execute five commands', async () => { const call = await generateBranch(); expect(execute).toBeCalledTimes(6); - expect(call).toBe('Deployment branch creation step complete...') + expect(call).toBe('Deployment branch creation step complete... ✅') }) }) @@ -131,7 +132,7 @@ describe("git", () => { const call = await deploy(); // Includes the call to generateBranch - expect(execute).toBeCalledTimes(16); + expect(execute).toBeCalledTimes(18); expect(call).toBe('Commit step complete...') }) }) diff --git a/lib/git.js b/lib/git.js index 83a240d0..42af948e 100644 --- a/lib/git.js +++ b/lib/git.js @@ -50,7 +50,7 @@ exports.init = init; function generateBranch() { return __awaiter(this, void 0, void 0, function* () { try { - console.log(`Creating ${constants_1.action.branch} branch...`); + console.log(`Creating ${constants_1.action.branch} branch... 🔧`); yield util_1.execute(`git switch ${constants_1.action.baseBranch || "master"}`, constants_1.workspace); yield util_1.execute(`git switch --orphan ${constants_1.action.branch}`, constants_1.workspace); yield util_1.execute(`git reset --hard`, constants_1.workspace); @@ -60,10 +60,10 @@ function generateBranch() { yield util_1.execute(`git switch ${constants_1.action.baseBranch || "master"}`, constants_1.workspace); } catch (error) { - core.setFailed(`There was an error creating the deployment branch: ${error}`); + core.setFailed(`There was an error creating the deployment branch: ${error} ❌`); } finally { - return Promise.resolve("Deployment branch creation step complete..."); + return Promise.resolve("Deployment branch creation step complete... ✅"); } }); } @@ -96,7 +96,7 @@ function deploy() { excludedItems.forEach((item) => (excludes += `--exclude ${item} `)); } catch (_a) { - console.log("There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details."); + console.log("There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌"); } } /* @@ -110,7 +110,7 @@ function deploy() { : ""} --exclude .git --exclude .github ${constants_1.action.build === constants_1.root ? `--exclude ${temporaryDeploymentDirectory}` : ""}`, constants_1.workspace); const hasFilesToCommit = yield util_1.execute(`git status --porcelain`, temporaryDeploymentDirectory); if (!hasFilesToCommit && !constants_1.isTest) { - console.log("There is nothing to commit. Exiting..."); + console.log("There is nothing to commit. Exiting... ✅"); return Promise.resolve(); } // Commits to GitHub. @@ -118,6 +118,12 @@ function deploy() { yield util_1.execute(`git switch -c ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory); yield util_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory); yield util_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory); + // Cleans up temporary files/folders and restores the git state. + if (process.env.GITHUB_SHA) { + console.log("Running post deployment cleanup jobs... 🔧"); + yield util_1.execute(`rm -rf ${temporaryDeploymentDirectory}`, constants_1.workspace); + yield util_1.execute(`git checkout --progress --force ${process.env.GITHUB_SHA}`, constants_1.workspace); + } return Promise.resolve("Commit step complete..."); }); } diff --git a/lib/main.js b/lib/main.js index b41f767a..f9741828 100644 --- a/lib/main.js +++ b/lib/main.js @@ -26,10 +26,11 @@ const git_1 = require("./git"); yield git_1.deploy(); } catch (error) { + console.log("The deployment encountered an error. ❌"); core.setFailed(error.message); } finally { - console.log("Completed Deployment"); + console.log("Completed Deployment ✅"); } }); })(); diff --git a/src/git.ts b/src/git.ts index cf6fe4f3..5e45251e 100644 --- a/src/git.ts +++ b/src/git.ts @@ -34,7 +34,7 @@ export async function init(): Promise { */ export async function generateBranch(): Promise { try { - console.log(`Creating ${action.branch} branch...`); + console.log(`Creating ${action.branch} branch... 🔧`); await execute(`git switch ${action.baseBranch || "master"}`, workspace); await execute(`git switch --orphan ${action.branch}`, workspace); await execute(`git reset --hard`, workspace); @@ -48,10 +48,10 @@ export async function generateBranch(): Promise { await execute(`git switch ${action.baseBranch || "master"}`, workspace); } catch (error) { core.setFailed( - `There was an error creating the deployment branch: ${error}` + `There was an error creating the deployment branch: ${error} ❌` ); } finally { - return Promise.resolve("Deployment branch creation step complete..."); + return Promise.resolve("Deployment branch creation step complete... ✅"); } } @@ -92,7 +92,7 @@ export async function deploy(): Promise { ); } catch { console.log( - "There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details." + "There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌" ); } } @@ -122,7 +122,7 @@ export async function deploy(): Promise { ); if (!hasFilesToCommit && !isTest) { - console.log("There is nothing to commit. Exiting..."); + console.log("There is nothing to commit. Exiting... ✅"); return Promise.resolve(); } @@ -141,5 +141,15 @@ export async function deploy(): Promise { temporaryDeploymentDirectory ); + // Cleans up temporary files/folders and restores the git state. + if (process.env.GITHUB_SHA) { + console.log("Running post deployment cleanup jobs... 🔧"); + await execute(`rm -rf ${temporaryDeploymentDirectory}`, workspace); + await execute( + `git checkout --progress --force ${process.env.GITHUB_SHA}`, + workspace + ); + } + return Promise.resolve("Commit step complete..."); } diff --git a/src/main.ts b/src/main.ts index 852c294e..3e940487 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,8 +7,9 @@ import { init, deploy } from "./git"; await init(); await deploy(); } catch (error) { + console.log("The deployment encountered an error. ❌"); core.setFailed(error.message); } finally { - console.log("Completed Deployment"); + console.log("Completed Deployment ✅"); } })();