Automatically remove hashed files (#63)

* Adding a CLEAN option

* Summary

* Clean

* Tests etc

* Update git.js
This commit is contained in:
James Ives 2019-11-22 09:58:00 -05:00 committed by GitHub
parent 3d79fb5ce1
commit 1c579cbd85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 55 deletions

View File

@ -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** | | `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** | | `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** | | `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. With the action correctly configured you should see the workflow trigger the deployment under the configured conditions.

View File

@ -4,7 +4,6 @@ process.env["INPUT_FOLDER"] = "build";
import { execute } from "../src/util"; import { execute } from "../src/util";
import { init, generateBranch, deploy } from "../src/git"; import { init, generateBranch, deploy } from "../src/git";
import {action} from '../src/constants' import {action} from '../src/constants'
import {cp} from '@actions/io';
import _ from 'lodash'; import _ from 'lodash';
const originalAction = _.cloneDeep(action); const originalAction = _.cloneDeep(action);
@ -13,10 +12,6 @@ jest.mock("../src/util", () => ({
execute: jest.fn() execute: jest.fn()
})); }));
jest.mock("@actions/io", () => ({
cp: jest.fn()
}));
describe("git", () => { describe("git", () => {
afterEach(() => { afterEach(() => {
_.assignIn(action, originalAction); _.assignIn(action, originalAction);
@ -135,26 +130,8 @@ describe("git", () => {
const call = await deploy(); 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 // Includes the call to generateBranch
expect(execute).toBeCalledTimes(16); expect(execute).toBeCalledTimes(16);
expect(cp).toBeCalledTimes(0)
expect(call).toBe('Commit step complete...') expect(call).toBe('Commit step complete...')
}) })
}) })

View File

@ -24,6 +24,7 @@ exports.action = {
accessToken: core.getInput("ACCESS_TOKEN"), accessToken: core.getInput("ACCESS_TOKEN"),
branch: core.getInput("BRANCH"), branch: core.getInput("BRANCH"),
baseBranch: core.getInput("BASE_BRANCH") || "master", baseBranch: core.getInput("BASE_BRANCH") || "master",
clean: core.getInput("CLEAN"),
pusher pusher
}; };
// Repository path used for commits/pushes. // Repository path used for commits/pushes.

View File

@ -17,7 +17,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core")); const core = __importStar(require("@actions/core"));
const io_1 = require("@actions/io");
const util_1 = require("./util"); const util_1 = require("./util");
const constants_1 = require("./constants"); const constants_1 = require("./constants");
/** Generates the branch if it doesn't exist on the remote. /** 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); 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. Pushes all of the build files into the deployment directory.
Allows the user to specify the root if '.' is provided. */ Allows the user to specify the root if '.' is provided.
if (constants_1.action.build === constants_1.root) { rysync is used to prevent file duplication. */
// 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} ${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);
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
});
}
const hasFilesToCommit = yield util_1.execute(`git status --porcelain`, temporaryDeploymentDirectory); const hasFilesToCommit = yield util_1.execute(`git status --porcelain`, temporaryDeploymentDirectory);
if (!hasFilesToCommit && !constants_1.isTest) { if (!hasFilesToCommit && !constants_1.isTest) {
console.log("There is nothing to commit. Exiting..."); console.log("There is nothing to commit. Exiting...");

View File

@ -34,8 +34,7 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.0.0", "@actions/core": "^1.0.0",
"@actions/exec": "^1.0.1", "@actions/exec": "^1.0.1",
"@actions/github": "^1.1.0", "@actions/github": "^1.1.0"
"@actions/io": "^1.0.1"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^24.0.23", "@types/jest": "^24.0.23",

View File

@ -19,6 +19,7 @@ export const action = {
accessToken: core.getInput("ACCESS_TOKEN"), accessToken: core.getInput("ACCESS_TOKEN"),
branch: core.getInput("BRANCH"), branch: core.getInput("BRANCH"),
baseBranch: core.getInput("BASE_BRANCH") || "master", baseBranch: core.getInput("BASE_BRANCH") || "master",
clean: core.getInput("CLEAN"),
pusher pusher
}; };

View File

@ -1,5 +1,4 @@
import * as core from "@actions/core"; import * as core from "@actions/core";
import { cp } from "@actions/io";
import { execute } from "./util"; import { execute } from "./util";
import { workspace, action, root, repositoryPath, isTest } from "./constants"; import { workspace, action, root, repositoryPath, isTest } from "./constants";
@ -85,19 +84,18 @@ export async function deploy(): Promise<any> {
/* /*
Pushes all of the build files into the deployment directory. Pushes all of the build files into the deployment directory.
Allows the user to specify the root if '.' is provided. */ Allows the user to specify the root if '.' is provided.
if (action.build === root) { rysync is used to prevent file duplication. */
// rsync is executed here so the .git and temporary deployment directories don't get duplicated. await execute(
await execute( `rsync -q -av --progress ${
`rsync -q -av --progress ${action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude .github --exclude ${temporaryDeploymentDirectory}`, action.build
workspace }/. ${temporaryDeploymentDirectory} ${
); action.clean ? `--delete --exclude CNAME --exclude .nojekyll` : ""
} else { } --exclude .git --exclude .github ${
await cp(`${action.build}/.`, temporaryDeploymentDirectory, { action.build === root ? `--exclude ${temporaryDeploymentDirectory}` : ""
recursive: true, }`,
force: true workspace
}); );
}
const hasFilesToCommit = await execute( const hasFilesToCommit = await execute(
`git status --porcelain`, `git status --porcelain`,

View File

@ -17,10 +17,6 @@
"@octokit/graphql" "^2.0.1" "@octokit/graphql" "^2.0.1"
"@octokit/rest" "^16.15.0" "@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": "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.5.5":
version "7.5.5" version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.5.5.tgz#bc0782f6d69f7b7d49531219699b988f669a8f9d" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.5.5.tgz#bc0782f6d69f7b7d49531219699b988f669a8f9d"