From 1c579cbd85f718e391dd1e112b20468ecc9d4cb1 Mon Sep 17 00:00:00 2001 From: James Ives Date: Fri, 22 Nov 2019 09:58:00 -0500 Subject: [PATCH] Automatically remove hashed files (#63) * Adding a CLEAN option * Summary * Clean * Tests etc * Update git.js --- README.md | 1 + __tests__/git.test.ts | 23 ----------------------- lib/constants.js | 1 + lib/git.js | 15 +++------------ package.json | 3 +-- src/constants.ts | 1 + src/git.ts | 26 ++++++++++++-------------- yarn.lock | 4 ---- 8 files changed, 19 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 5e67ab70..468f095a 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Below you'll find a description of what each option does. | `BRANCH` | This is the branch you wish to deploy to, for example `gh-pages` or `docs`. | `with` | **Yes** | | `FOLDER` | The folder in your repository that you want to deploy. If your build script compiles into a directory named `build` you'd put it here. **Folder paths cannot have a leading `/` or `./`**. If you wish to deploy the root directory you can place a `.` here. | `with` | **Yes** | | `BASE_BRANCH` | The base branch of your repository which you'd like to checkout prior to deploying. This defaults to `master`. | `with` | **No** | +| `CLEAN` | If your project generates hashed files on build you can use this option to automatically delete them from the deployment branch with each deploy. This option can be toggled on by setting it to `true`. | `with` | **No** | With the action correctly configured you should see the workflow trigger the deployment under the configured conditions. diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 4b00a934..552ed902 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -4,7 +4,6 @@ process.env["INPUT_FOLDER"] = "build"; import { execute } from "../src/util"; import { init, generateBranch, deploy } from "../src/git"; import {action} from '../src/constants' -import {cp} from '@actions/io'; import _ from 'lodash'; const originalAction = _.cloneDeep(action); @@ -13,10 +12,6 @@ jest.mock("../src/util", () => ({ execute: jest.fn() })); -jest.mock("@actions/io", () => ({ - cp: jest.fn() -})); - describe("git", () => { afterEach(() => { _.assignIn(action, originalAction); @@ -135,26 +130,8 @@ describe("git", () => { const call = await deploy(); - // Includes the call to generateBranch - expect(execute).toBeCalledTimes(15); - 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(16); - expect(cp).toBeCalledTimes(0) expect(call).toBe('Commit step complete...') }) }) diff --git a/lib/constants.js b/lib/constants.js index a38706db..7f56fb64 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -24,6 +24,7 @@ exports.action = { accessToken: core.getInput("ACCESS_TOKEN"), branch: core.getInput("BRANCH"), baseBranch: core.getInput("BASE_BRANCH") || "master", + clean: core.getInput("CLEAN"), pusher }; // Repository path used for commits/pushes. diff --git a/lib/git.js b/lib/git.js index faa894fd..984c7c13 100644 --- a/lib/git.js +++ b/lib/git.js @@ -17,7 +17,6 @@ var __importStar = (this && this.__importStar) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); -const io_1 = require("@actions/io"); const util_1 = require("./util"); const constants_1 = require("./constants"); /** Generates the branch if it doesn't exist on the remote. @@ -91,17 +90,9 @@ function deploy() { yield util_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${constants_1.action.branch}`, constants_1.workspace); /* 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 === constants_1.root) { - // rsync is executed here so the .git and temporary deployment directories don't get duplicated. - yield util_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude .github --exclude ${temporaryDeploymentDirectory}`, constants_1.workspace); - } - else { - yield io_1.cp(`${constants_1.action.build}/.`, temporaryDeploymentDirectory, { - recursive: true, - force: true - }); - } + Allows the user to specify the root if '.' is provided. + rysync is used to prevent file duplication. */ + yield util_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${temporaryDeploymentDirectory} ${constants_1.action.clean ? `--delete --exclude CNAME --exclude .nojekyll` : ""} --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..."); diff --git a/package.json b/package.json index b862a333..466ab7db 100644 --- a/package.json +++ b/package.json @@ -34,8 +34,7 @@ "dependencies": { "@actions/core": "^1.0.0", "@actions/exec": "^1.0.1", - "@actions/github": "^1.1.0", - "@actions/io": "^1.0.1" + "@actions/github": "^1.1.0" }, "devDependencies": { "@types/jest": "^24.0.23", diff --git a/src/constants.ts b/src/constants.ts index 8d546be5..9d10dc12 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -19,6 +19,7 @@ export const action = { accessToken: core.getInput("ACCESS_TOKEN"), branch: core.getInput("BRANCH"), baseBranch: core.getInput("BASE_BRANCH") || "master", + clean: core.getInput("CLEAN"), pusher }; diff --git a/src/git.ts b/src/git.ts index 4d94d58f..f3fa36dc 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,5 +1,4 @@ import * as core from "@actions/core"; -import { cp } from "@actions/io"; import { execute } from "./util"; import { workspace, action, root, repositoryPath, isTest } from "./constants"; @@ -85,19 +84,18 @@ 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 === root) { - // rsync is executed here so the .git and temporary deployment directories don't get duplicated. - await execute( - `rsync -q -av --progress ${action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude .github --exclude ${temporaryDeploymentDirectory}`, - workspace - ); - } else { - await cp(`${action.build}/.`, temporaryDeploymentDirectory, { - recursive: true, - force: true - }); - } + Allows the user to specify the root if '.' is provided. + rysync is used to prevent file duplication. */ + await execute( + `rsync -q -av --progress ${ + action.build + }/. ${temporaryDeploymentDirectory} ${ + action.clean ? `--delete --exclude CNAME --exclude .nojekyll` : "" + } --exclude .git --exclude .github ${ + action.build === root ? `--exclude ${temporaryDeploymentDirectory}` : "" + }`, + workspace + ); const hasFilesToCommit = await execute( `git status --porcelain`, diff --git a/yarn.lock b/yarn.lock index ac70f604..176b65c0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -17,10 +17,6 @@ "@octokit/graphql" "^2.0.1" "@octokit/rest" "^16.15.0" -"@actions/io@^1.0.1": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.0.1.tgz#81a9418fe2bbdef2d2717a8e9f85188b9c565aca" - "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.5.5": version "7.5.5" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.5.5.tgz#bc0782f6d69f7b7d49531219699b988f669a8f9d"