github-pages-deploy-action/lib/git.js

161 lines
9.5 KiB
JavaScript
Raw Normal View History

2020-03-02 22:38:52 +08:00
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
2020-05-15 05:33:08 +08:00
exports.deploy = exports.generateBranch = exports.switchToBaseBranch = exports.init = void 0;
2020-03-31 20:48:28 +08:00
const core_1 = require("@actions/core");
2020-04-12 01:59:32 +08:00
const io_1 = require("@actions/io");
2020-05-15 05:33:08 +08:00
const constants_1 = require("./constants");
2020-03-02 22:38:52 +08:00
const execute_1 = require("./execute");
const util_1 = require("./util");
/* Initializes git in the workspace. */
function init(action) {
return __awaiter(this, void 0, void 0, function* () {
try {
util_1.hasRequiredParameters(action);
2020-03-31 20:48:28 +08:00
core_1.info(`Deploying using ${action.tokenType}… 🔑`);
core_1.info('Configuring git…');
2020-03-02 22:38:52 +08:00
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);
2020-05-18 20:58:56 +08:00
yield execute_1.execute(`git fetch --no-recurse-submodules`, action.workspace);
2020-03-31 20:48:28 +08:00
core_1.info('Git configured… 🔧');
2020-03-02 22:38:52 +08:00
}
catch (error) {
throw new Error(`There was an error initializing the repository: ${util_1.suppressSensitiveInformation(error.message, action)}`);
}
});
}
exports.init = init;
/* Switches to the base branch. */
function switchToBaseBranch(action) {
return __awaiter(this, void 0, void 0, function* () {
try {
util_1.hasRequiredParameters(action);
yield execute_1.execute(`git checkout --progress --force ${action.baseBranch ? action.baseBranch : action.defaultBranch}`, action.workspace);
}
catch (error) {
throw new Error(`There was an error switching to the base branch: ${util_1.suppressSensitiveInformation(error.message, action)}`);
}
});
}
exports.switchToBaseBranch = switchToBaseBranch;
/* Generates the branch if it doesn't exist on the remote. */
function generateBranch(action) {
return __awaiter(this, void 0, void 0, function* () {
try {
util_1.hasRequiredParameters(action);
2020-03-31 20:48:28 +08:00
core_1.info(`Creating the ${action.branch} branch…`);
2020-03-02 22:38:52 +08:00
yield switchToBaseBranch(action);
yield execute_1.execute(`git checkout --orphan ${action.branch}`, action.workspace);
yield execute_1.execute(`git reset --hard`, action.workspace);
2020-03-31 20:48:28 +08:00
yield execute_1.execute(`git commit --allow-empty -m "Initial ${action.branch} commit"`, action.workspace);
2020-03-31 21:41:31 +08:00
yield execute_1.execute(`git push --force ${action.repositoryPath} ${action.branch}`, action.workspace);
2020-03-02 22:38:52 +08:00
yield execute_1.execute(`git fetch`, action.workspace);
2020-03-31 20:48:28 +08:00
core_1.info(`Created the ${action.branch} branch… 🔧`);
2020-03-02 22:38:52 +08:00
}
catch (error) {
throw new Error(`There was an error creating the deployment branch: ${util_1.suppressSensitiveInformation(error.message, action)}`);
}
});
}
exports.generateBranch = generateBranch;
/* Runs the necessary steps to make the deployment. */
function deploy(action) {
return __awaiter(this, void 0, void 0, function* () {
2020-05-25 00:56:27 +08:00
const temporaryDeploymentDirectory = 'github-pages-deploy-action-temp-deployment-folder';
const temporaryDeploymentBranch = `github-pages-deploy-action/${Math.random()
.toString(36)
.substr(2, 9)}`;
2020-03-31 20:48:28 +08:00
core_1.info('Starting to commit changes…');
2020-03-02 22:38:52 +08:00
try {
util_1.hasRequiredParameters(action);
2020-04-09 23:05:35 +08:00
const commitMessage = !util_1.isNullOrUndefined(action.commitMessage)
2020-03-31 20:48:28 +08:00
? action.commitMessage
2020-04-09 23:05:35 +08:00
: `Deploying to ${action.branch} from ${action.baseBranch} ${process.env.GITHUB_SHA ? `@ ${process.env.GITHUB_SHA}` : ''} 🚀`;
2020-03-02 22:38:52 +08:00
/*
Checks to see if the remote exists prior to deploying.
If the branch doesn't exist it gets created here as an orphan.
*/
const branchExists = yield execute_1.execute(`git ls-remote --heads ${action.repositoryPath} ${action.branch} | wc -l`, action.workspace);
if (!branchExists && !action.isTest) {
yield generateBranch(action);
}
// Checks out the base branch to begin the deployment process.
yield switchToBaseBranch(action);
yield execute_1.execute(`git fetch ${action.repositoryPath}`, action.workspace);
yield execute_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${action.branch}`, action.workspace);
// Ensures that items that need to be excluded from the clean job get parsed.
2020-03-07 11:45:40 +08:00
let excludes = '';
2020-03-02 22:38:52 +08:00
if (action.clean && action.cleanExclude) {
try {
2020-03-07 11:45:40 +08:00
const excludedItems = typeof action.cleanExclude === 'string'
2020-03-02 22:38:52 +08:00
? JSON.parse(action.cleanExclude)
: action.cleanExclude;
2020-03-07 11:45:40 +08:00
for (const item of excludedItems) {
excludes += `--exclude ${item} `;
}
2020-03-02 22:38:52 +08:00
}
catch (_a) {
2020-03-31 20:48:28 +08:00
core_1.info('There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌');
2020-03-02 22:38:52 +08:00
}
}
2020-04-30 20:40:07 +08:00
if (action.targetFolder) {
core_1.info(`Creating target folder if it doesn't already exist… 📌`);
yield io_1.mkdirP(`${temporaryDeploymentDirectory}/${action.targetFolder}`);
}
2020-03-02 22:38:52 +08:00
/*
Pushes all of the build files into the deployment directory.
Allows the user to specify the root if '.' is provided.
rsync is used to prevent file duplication. */
yield execute_1.execute(`rsync -q -av --progress ${action.folder}/. ${action.targetFolder
? `${temporaryDeploymentDirectory}/${action.targetFolder}`
: temporaryDeploymentDirectory} ${action.clean
? `--delete ${excludes} --exclude CNAME --exclude .nojekyll`
2020-03-07 11:45:40 +08:00
: ''} --exclude .ssh --exclude .git --exclude .github ${action.folder === action.root
2020-03-02 22:38:52 +08:00
? `--exclude ${temporaryDeploymentDirectory}`
2020-03-07 11:45:40 +08:00
: ''}`, action.workspace);
2020-03-02 22:38:52 +08:00
const hasFilesToCommit = yield execute_1.execute(`git status --porcelain`, `${action.workspace}/${temporaryDeploymentDirectory}`);
if (!hasFilesToCommit && !action.isTest) {
2020-05-15 05:33:08 +08:00
return constants_1.Status.SKIPPED;
2020-03-02 22:38:52 +08:00
}
// Commits to GitHub.
yield execute_1.execute(`git add --all .`, `${action.workspace}/${temporaryDeploymentDirectory}`);
yield execute_1.execute(`git checkout -b ${temporaryDeploymentBranch}`, `${action.workspace}/${temporaryDeploymentDirectory}`);
2020-03-31 20:48:28 +08:00
yield execute_1.execute(`git commit -m "${commitMessage}" --quiet`, `${action.workspace}/${temporaryDeploymentDirectory}`);
2020-03-02 22:38:52 +08:00
yield execute_1.execute(`git push --force ${action.repositoryPath} ${temporaryDeploymentBranch}:${action.branch}`, `${action.workspace}/${temporaryDeploymentDirectory}`);
2020-03-31 20:48:28 +08:00
core_1.info(`Changes committed to the ${action.branch} branch… 📦`);
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… 🚿');
}
2020-03-02 22:38:52 +08:00
yield execute_1.execute(`git checkout --progress --force ${action.defaultBranch}`, action.workspace);
2020-05-15 05:33:08 +08:00
return constants_1.Status.SUCCESS;
2020-03-02 22:38:52 +08:00
}
catch (error) {
throw new Error(`The deploy step encountered an error: ${util_1.suppressSensitiveInformation(error.message, action)}`);
}
finally {
2020-05-24 23:18:50 +08:00
// Cleans up temporary files/folders and restores the git state.
core_1.info('Running post deployment cleanup jobs… 🗑️');
yield execute_1.execute(`git worktree remove ${temporaryDeploymentDirectory} --force`, action.workspace);
2020-04-12 01:59:32 +08:00
yield io_1.rmRF(temporaryDeploymentDirectory);
2020-03-02 22:38:52 +08:00
}
});
}
exports.deploy = deploy;