From 38d5950b3800a2d1421998e427dae30cb25c8597 Mon Sep 17 00:00:00 2001 From: JamesIves Date: Tue, 31 Mar 2020 08:48:28 -0400 Subject: [PATCH] 3.4.3 Build --- lib/constants.d.ts | 12 ++++++------ lib/constants.js | 19 +++++++++++++------ lib/execute.d.ts | 6 +++--- lib/execute.js | 9 +++++---- lib/git.js | 39 +++++++++++++++++++++++++-------------- lib/lib.d.ts | 5 ++++- lib/lib.js | 13 ++++++------- lib/util.js | 5 ++--- 8 files changed, 64 insertions(+), 44 deletions(-) diff --git a/lib/constants.d.ts b/lib/constants.d.ts index f21af1b5..0dae5049 100644 --- a/lib/constants.d.ts +++ b/lib/constants.d.ts @@ -6,13 +6,11 @@ export interface ActionInterface { /** The branch that the action should deploy to. */ branch: string; /** 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. */ - clean?: string | boolean; + clean?: boolean | null; /** If you need to use CLEAN but you'd like to preserve certain files or folders you can use this option. */ cleanExclude?: string | string[]; /** If you need to customize the commit message for an integration you can do so. */ commitMessage?: string; - /** Unhides the Git commands from the function terminal. */ - debug?: boolean | string; /** The default branch of the deployment. Similar to baseBranch if you're using this action as a module. */ defaultBranch?: string; /** The git config email. */ @@ -22,17 +20,19 @@ export interface ActionInterface { /** GitHub deployment token. */ gitHubToken?: string | null; /** Determines if the action is running in test mode or not. */ - isTest?: string | undefined | null; + isTest?: boolean | null; /** The git config name. */ name?: string; - /** The repository path, for example JamesIves/github-pages-deploy-action */ + /** The repository path, for example JamesIves/github-pages-deploy-action. */ repositoryName?: string; /** The fully qualified repositpory path, this gets auto generated if repositoryName is provided. */ repositoryPath?: string; /** The root directory where your project lives. */ root?: string; + /** Wipes the commit history from the deployment branch in favor of a single commit. */ + singleCommit?: boolean | null; /** Set to true if you're using an ssh client in your build step. */ - ssh?: string | boolean | null; + ssh?: boolean | null; /** If you'd like to push the contents of the deployment folder into a specific directory on the deployment branch you can specify it here. */ targetFolder?: string; /** The token type, ie ssh/github token/access token, this gets automatically generated. */ diff --git a/lib/constants.js b/lib/constants.js index 347a4eba..7199a0de 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -18,18 +18,19 @@ exports.action = { folder: core_1.getInput('FOLDER'), branch: core_1.getInput('BRANCH'), commitMessage: core_1.getInput('COMMIT_MESSAGE'), - clean: core_1.getInput('CLEAN'), + clean: !util_1.isNullOrUndefined(core_1.getInput('CLEAN')) + ? core_1.getInput('CLEAN').toLowerCase() === 'true' + : false, cleanExclude: core_1.getInput('CLEAN_EXCLUDE'), - debug: core_1.getInput('DEBUG'), defaultBranch: process.env.GITHUB_SHA ? process.env.GITHUB_SHA : 'master', - isTest: process.env.UNIT_TEST, - ssh: core_1.getInput('SSH'), + isTest: process.env.UNIT_TEST + ? process.env.UNIT_TEST.toLowerCase() === 'true' + : false, email: !util_1.isNullOrUndefined(core_1.getInput('GIT_CONFIG_EMAIL')) ? core_1.getInput('GIT_CONFIG_EMAIL') : pusher && pusher.email ? pusher.email - : `${process.env.GITHUB_ACTOR || - 'github-pages-deploy-action'}@users.noreply.github.com`, + : `${process.env.GITHUB_ACTOR || 'github-pages-deploy-action'}@users.noreply.github.com`, gitHubToken: core_1.getInput('GITHUB_TOKEN'), name: !util_1.isNullOrUndefined(core_1.getInput('GIT_CONFIG_NAME')) ? core_1.getInput('GIT_CONFIG_NAME') @@ -44,6 +45,12 @@ exports.action = { ? repository.full_name : process.env.GITHUB_REPOSITORY, root: '.', + singleCommit: !util_1.isNullOrUndefined(core_1.getInput('SINGLE_COMMIT')) + ? core_1.getInput('SINGLE_COMMIT').toLowerCase() === 'true' + : false, + ssh: !util_1.isNullOrUndefined(core_1.getInput('SSH')) + ? core_1.getInput('SSH').toLowerCase() === 'true' + : false, targetFolder: core_1.getInput('TARGET_FOLDER'), workspace: process.env.GITHUB_WORKSPACE || '' }; diff --git a/lib/execute.d.ts b/lib/execute.d.ts index a6462dcb..4b0b6326 100644 --- a/lib/execute.d.ts +++ b/lib/execute.d.ts @@ -1,8 +1,8 @@ /** Wrapper around the GitHub toolkit exec command which returns the output. * Also allows you to easily toggle the current working directory. - * @param cmd = The command to execute. - * @param cwd - The current working directory. - * @returns - The output from the command. + * + * @param {string} cmd - The command to execute. + * @param {string} cwd - The current working directory. */ export declare function execute(cmd: string, cwd: string): Promise; export declare function stdout(data: any): string | void; diff --git a/lib/execute.js b/lib/execute.js index 067ef0d8..1a8e59d4 100644 --- a/lib/execute.js +++ b/lib/execute.js @@ -9,20 +9,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +const core_1 = require("@actions/core"); const exec_1 = require("@actions/exec"); let output; /** Wrapper around the GitHub toolkit exec command which returns the output. * Also allows you to easily toggle the current working directory. - * @param cmd = The command to execute. - * @param cwd - The current working directory. - * @returns - The output from the command. + * + * @param {string} cmd - The command to execute. + * @param {string} cwd - The current working directory. */ function execute(cmd, cwd) { return __awaiter(this, void 0, void 0, function* () { output = ''; yield exec_1.exec(cmd, [], { // Silences the input unless the INPUT_DEBUG flag is set. - silent: process.env.DEBUG_DEPLOY_ACTION ? false : true, + silent: core_1.isDebug() ? false : true, cwd, listeners: { stdout diff --git a/lib/git.js b/lib/git.js index 05ee488e..58340830 100644 --- a/lib/git.js +++ b/lib/git.js @@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +const core_1 = require("@actions/core"); const execute_1 = require("./execute"); const util_1 = require("./util"); /* Initializes git in the workspace. */ @@ -16,15 +17,15 @@ function init(action) { return __awaiter(this, void 0, void 0, function* () { try { util_1.hasRequiredParameters(action); - console.log(`Deploying using ${action.tokenType}... 🔑`); - console.log('Configuring git...'); + core_1.info(`Deploying using ${action.tokenType}… 🔑`); + core_1.info('Configuring git…'); yield execute_1.execute(`git init`, action.workspace); yield execute_1.execute(`git config user.name "${action.name}"`, action.workspace); yield execute_1.execute(`git config user.email "${action.email}"`, action.workspace); yield execute_1.execute(`git remote rm origin`, action.workspace); yield execute_1.execute(`git remote add origin ${action.repositoryPath}`, action.workspace); yield execute_1.execute(`git fetch`, action.workspace); - console.log('Git configured... 🔧'); + core_1.info('Git configured… 🔧'); } catch (error) { throw new Error(`There was an error initializing the repository: ${util_1.suppressSensitiveInformation(error.message, action)} ❌`); @@ -50,14 +51,14 @@ function generateBranch(action) { return __awaiter(this, void 0, void 0, function* () { try { util_1.hasRequiredParameters(action); - console.log(`Creating the ${action.branch} branch...`); + core_1.info(`Creating the ${action.branch} branch…`); yield switchToBaseBranch(action); yield execute_1.execute(`git checkout --orphan ${action.branch}`, action.workspace); yield execute_1.execute(`git reset --hard`, action.workspace); - yield execute_1.execute(`git commit --allow-empty -m "Initial ${action.branch} commit."`, action.workspace); + yield execute_1.execute(`git commit --allow-empty -m "Initial ${action.branch} commit"`, action.workspace); yield execute_1.execute(`git push ${action.repositoryPath} ${action.branch}`, action.workspace); yield execute_1.execute(`git fetch`, action.workspace); - console.log(`Created the ${action.branch} branch... 🔧`); + core_1.info(`Created the ${action.branch} branch… 🔧`); } catch (error) { throw new Error(`There was an error creating the deployment branch: ${util_1.suppressSensitiveInformation(error.message, action)} ❌`); @@ -70,9 +71,12 @@ function deploy(action) { return __awaiter(this, void 0, void 0, function* () { const temporaryDeploymentDirectory = 'gh-action-temp-deployment-folder'; const temporaryDeploymentBranch = 'gh-action-temp-deployment-branch'; - console.log('Starting to commit changes...'); + core_1.info('Starting to commit changes…'); try { util_1.hasRequiredParameters(action); + const commitMessage = `${!util_1.isNullOrUndefined(action.commitMessage) + ? action.commitMessage + : `Deploying to ${action.branch} from ${action.baseBranch}`} ${process.env.GITHUB_SHA ? `@ ${process.env.GITHUB_SHA}` : ''} 🚀`; /* Checks to see if the remote exists prior to deploying. If the branch doesn't exist it gets created here as an orphan. @@ -97,7 +101,7 @@ function deploy(action) { } } catch (_a) { - console.log('There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌'); + core_1.info('There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌'); } } /* @@ -113,19 +117,26 @@ function deploy(action) { : ''}`, action.workspace); const hasFilesToCommit = yield execute_1.execute(`git status --porcelain`, `${action.workspace}/${temporaryDeploymentDirectory}`); if (!hasFilesToCommit && !action.isTest) { - console.log('There is nothing to commit. Exiting early... 📭'); + core_1.info('There is nothing to commit. Exiting early… 📭'); return; } // Commits to GitHub. yield execute_1.execute(`git add --all .`, `${action.workspace}/${temporaryDeploymentDirectory}`); yield execute_1.execute(`git checkout -b ${temporaryDeploymentBranch}`, `${action.workspace}/${temporaryDeploymentDirectory}`); - yield execute_1.execute(`git commit -m "${!util_1.isNullOrUndefined(action.commitMessage) - ? action.commitMessage - : `Deploying to ${action.branch} from ${action.baseBranch}`} ${process.env.GITHUB_SHA ? `- ${process.env.GITHUB_SHA}` : ''} 🚀" --quiet`, `${action.workspace}/${temporaryDeploymentDirectory}`); + yield execute_1.execute(`git commit -m "${commitMessage}" --quiet`, `${action.workspace}/${temporaryDeploymentDirectory}`); yield execute_1.execute(`git push --force ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`, `${action.workspace}/${temporaryDeploymentDirectory}`); - console.log(`Changes committed to the ${action.branch} branch... 📦`); + core_1.info(`Changes committed to the ${action.branch} branch… 📦`); // Cleans up temporary files/folders and restores the git state. - console.log('Running post deployment cleanup jobs...'); + core_1.info('Running post deployment cleanup jobs…'); + if (action.singleCommit) { + yield execute_1.execute(`git fetch ${action.repositoryPath}`, action.workspace); + yield execute_1.execute(`git checkout --orphan ${action.branch}-temp`, `${action.workspace}/${temporaryDeploymentDirectory}`); + yield execute_1.execute(`git add --all .`, `${action.workspace}/${temporaryDeploymentDirectory}`); + yield execute_1.execute(`git commit -m "${commitMessage}" --quiet`, `${action.workspace}/${temporaryDeploymentDirectory}`); + yield execute_1.execute(`git branch -M ${action.branch}-temp ${action.branch}`, `${action.workspace}/${temporaryDeploymentDirectory}`); + yield execute_1.execute(`git push origin ${action.branch} --force`, `${action.workspace}/${temporaryDeploymentDirectory}`); + core_1.info('Cleared git history… 🚿'); + } yield execute_1.execute(`git checkout --progress --force ${action.defaultBranch}`, action.workspace); } catch (error) { diff --git a/lib/lib.d.ts b/lib/lib.d.ts index 1daa4d29..ed545d5a 100644 --- a/lib/lib.d.ts +++ b/lib/lib.d.ts @@ -1,5 +1,8 @@ import { ActionInterface } from './constants'; import { deploy, generateBranch, init } from './git'; -/** Initializes and runs the action. */ +/** Initializes and runs the action. + * + * @param {object} configuration - The action configuration. + */ export default function run(configuration: ActionInterface): Promise; export { init, deploy, generateBranch, ActionInterface }; diff --git a/lib/lib.js b/lib/lib.js index b604358e..e365f352 100644 --- a/lib/lib.js +++ b/lib/lib.js @@ -16,20 +16,19 @@ exports.deploy = git_1.deploy; exports.generateBranch = git_1.generateBranch; exports.init = git_1.init; const util_1 = require("./util"); -/** Initializes and runs the action. */ +/** Initializes and runs the action. + * + * @param {object} configuration - The action configuration. + */ function run(configuration) { return __awaiter(this, void 0, void 0, function* () { let errorState = false; try { - console.log('Checking configuration and starting deployment...🚦'); + core_1.info('Checking configuration and starting deployment… 🚦'); const settings = Object.assign(Object.assign({}, constants_1.action), configuration); // Defines the repository paths and token types. settings.repositoryPath = util_1.generateRepositoryPath(settings); settings.tokenType = util_1.generateTokenType(settings); - if (settings.debug) { - // Sets the debug flag if passed as an arguement. - core_1.exportVariable('DEBUG_DEPLOY_ACTION', 'debug'); - } yield git_1.init(settings); yield git_1.deploy(settings); } @@ -38,7 +37,7 @@ function run(configuration) { core_1.setFailed(error.message); } finally { - console.log(`${errorState + core_1.info(`${errorState ? 'Deployment Failed ❌' : 'Completed Deployment Successfully! ✅'}`); } diff --git a/lib/util.js b/lib/util.js index 2681d6aa..7547a481 100644 --- a/lib/util.js +++ b/lib/util.js @@ -14,8 +14,7 @@ exports.generateTokenType = (action) => action.ssh /* Generates a the repository path used to make the commits. */ exports.generateRepositoryPath = (action) => action.ssh ? `git@github.com:${action.repositoryName}` - : `https://${action.accessToken || - `x-access-token:${action.gitHubToken}`}@github.com/${action.repositoryName}.git`; + : `https://${action.accessToken || `x-access-token:${action.gitHubToken}`}@github.com/${action.repositoryName}.git`; /* Checks for the required tokens and formatting. Throws an error if any case is matched. */ exports.hasRequiredParameters = (action) => { if ((exports.isNullOrUndefined(action.accessToken) && @@ -37,7 +36,7 @@ exports.hasRequiredParameters = (action) => { /* Suppresses sensitive information from being exposed in error messages. */ exports.suppressSensitiveInformation = (str, action) => { let value = str; - if (core_1.getInput('DEBUG')) { + if (core_1.isDebug()) { // Data is unmasked in debug mode. return value; }