From a4594d3404e31637349e70bd169757395a3a02a1 Mon Sep 17 00:00:00 2001 From: James Ives Date: Sun, 10 Nov 2019 16:31:05 -0500 Subject: [PATCH] Fixes root deployment --- __tests__/git.test.ts | 44 +++++++++++++++++++++++++++++++++++++++++-- lib/constants.js | 1 + lib/git.js | 15 +++++++-------- src/constants.ts | 1 + src/git.ts | 23 +++++++++++----------- 5 files changed, 63 insertions(+), 21 deletions(-) diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 847b4071..2deb8c25 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -98,24 +98,64 @@ describe("git", () => { expect(execute).toBeCalledTimes(0); expect(call).toBe('Initialization step complete...') }) + + it("should not fail if root is used", async () => { + Object.assign(action, { + accessToken: '123', + build: '.', + pusher: { + name: 'asd', + email: 'as@cat' + }}) + + const call = await init() + + expect(execute).toBeCalledTimes(3); + expect(call).toBe('Initialization step complete...') + }) }); describe('generateBranch', () => { it('should execute five commands', async () => { const call = await generateBranch(); - expect(execute).toBeCalledTimes(5); + expect(execute).toBeCalledTimes(6); expect(call).toBe('Deployment branch creation step complete...') }) }) describe('deploy', () => { it('should execute five commands', async () => { + Object.assign(action, { + build: 'build', + gitHubToken: '123', + pusher: { + name: 'asd', + email: 'as@cat' + }}) + const call = await deploy(); // Includes the call to generateBranch - expect(execute).toBeCalledTimes(13); + expect(execute).toBeCalledTimes(14); expect(cp).toBeCalledTimes(1) expect(call).toBe('Commit step complete...') }) + + it('should execute six commands if root is used', async () => { + Object.assign(action, { + build: '.', + gitHubToken: '123', + pusher: { + name: 'asd', + email: 'as@cat' + }}) + + const call = await deploy(); + + // Includes the call to generateBranch + expect(execute).toBeCalledTimes(15); + expect(cp).toBeCalledTimes(0) + expect(call).toBe('Commit step complete...') + }) }) }); diff --git a/lib/constants.js b/lib/constants.js index 7d97c653..dbaa4339 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -12,6 +12,7 @@ const github = __importStar(require("@actions/github")); const { pusher, repository } = github.context.payload; exports.workspace = process.env.GITHUB_WORKSPACE; exports.folder = core.getInput("FOLDER", { required: true }); +exports.root = "."; // Required action data. exports.action = { build: exports.folder, diff --git a/lib/git.js b/lib/git.js index acbc6336..520d22db 100644 --- a/lib/git.js +++ b/lib/git.js @@ -29,11 +29,9 @@ function init() { if (!constants_1.action.accessToken && !constants_1.action.gitHubToken) { return core.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy."); } - /*if (action.build.startsWith("/") || action.build.startsWith("./")) { - return core.setFailed( - `The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.` - ); - }*/ + if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) { + return core.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`); + } yield util_1.execute(`git init`, constants_1.workspace); yield util_1.execute(`git config user.name ${constants_1.action.pusher.name}`, constants_1.workspace); yield util_1.execute(`git config user.email ${constants_1.action.pusher.email}`, constants_1.workspace); @@ -76,8 +74,8 @@ exports.generateBranch = generateBranch; */ function deploy() { return __awaiter(this, void 0, void 0, function* () { - const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder'; - const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch'; + const temporaryDeploymentDirectory = "gh-action-temp-deployment-folder"; + const temporaryDeploymentBranch = "gh-action-temp-deployment-branch"; /* Checks to see if the remote exists prior to deploying. If the branch doesn't exist it gets created here as an orphan. @@ -94,7 +92,8 @@ function deploy() { /* Pushes all of the build files into the deployment directory. Allows the user to specify the root if '.' is provided. */ - if (constants_1.action.build === '.') { + if (constants_1.action.build === constants_1.root) { + // rsync is executed here so the .git and temporary deployment directories don't get duplicated. yield util_1.execute(`rsync -av --progress ${constants_1.action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude ${temporaryDeploymentDirectory}`, constants_1.workspace); } else { diff --git a/src/constants.ts b/src/constants.ts index 4d54b01c..12d9fe03 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,6 +5,7 @@ const { pusher, repository } = github.context.payload; export const workspace: any = process.env.GITHUB_WORKSPACE; export const folder = core.getInput("FOLDER", { required: true }); +export const root = "."; // Required action data. export const action = { diff --git a/src/git.ts b/src/git.ts index 90ac3e6a..dda23638 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,7 +1,7 @@ import * as core from "@actions/core"; -import { cp, rmRF } from "@actions/io"; +import { cp } from "@actions/io"; import { execute } from "./util"; -import { workspace, action, repositoryPath } from "./constants"; +import { workspace, action, root, repositoryPath } from "./constants"; /** Generates the branch if it doesn't exist on the remote. * @returns {Promise} @@ -14,11 +14,11 @@ export async function init(): Promise { ); } - /*if (action.build.startsWith("/") || action.build.startsWith("./")) { + if (action.build.startsWith("/") || action.build.startsWith("./")) { return core.setFailed( `The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.` ); - }*/ + } await execute(`git init`, workspace); await execute(`git config user.name ${action.pusher.name}`, workspace); @@ -60,8 +60,8 @@ export async function generateBranch(): Promise { * @returns {Promise} */ export async function deploy(): Promise { - const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder' - const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch' + const temporaryDeploymentDirectory = "gh-action-temp-deployment-folder"; + const temporaryDeploymentBranch = "gh-action-temp-deployment-branch"; /* Checks to see if the remote exists prior to deploying. If the branch doesn't exist it gets created here as an orphan. @@ -86,15 +86,17 @@ export async function deploy(): Promise { /* Pushes all of the build files into the deployment directory. Allows the user to specify the root if '.' is provided. */ - - if (action.build === '.') { - await execute(`rsync -av --progress ${action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude ${temporaryDeploymentDirectory}`, workspace) + if (action.build === root) { + // rsync is executed here so the .git and temporary deployment directories don't get duplicated. + await execute( + `rsync -av --progress ${action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude ${temporaryDeploymentDirectory}`, + workspace + ); } else { await cp(`${action.build}/.`, temporaryDeploymentDirectory, { recursive: true, force: true }); - } // Commits to GitHub. @@ -111,7 +113,6 @@ export async function deploy(): Promise { `git push --force ${repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`, temporaryDeploymentDirectory ); - return Promise.resolve("Commit step complete..."); }