github-pages-deploy-action/lib/git.js
James Ives 204e85b9d1
3.2.0 (#142)
* Additional Integration tests (#123)

* Update integration-beta.yml

* git fetch again

* git fetch

* Tests

* Failing Build (#124)

* Bump @types/node from 13.1.6 to 13.1.7 (#126)

Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 13.1.6 to 13.1.7.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Bump @types/jest from 24.0.25 to 24.9.0 (#127)

Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 24.0.25 to 24.9.0.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* js > ts

* License Upgrades

* Bump typescript from 3.7.4 to 3.7.5 (#132)

Bumps [typescript](https://github.com/Microsoft/TypeScript) from 3.7.4 to 3.7.5.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/compare/v3.7.4...v3.7.5)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* [Issue-133] Customizable Commit Messages (#134)

* Customizable Commit Messages

* Removes lodash

* Update package.json

* README

* Changes

* Check for null

* Adds a dash

* Support action/checkout@v2 (#136)

* Create CODEOWNERS

* Update integration.yml

* Adds automatic issue labels (#139)

* [Issue-130] SSH Support (#140)

* First pass at deploy key support

* Needed

* More ssh

* Adds keyscan

* Another look at this

* More things....

* w

* Path to home

* w

* k

* Update git.ts

* ff

* dd

* test

* File Sync maybe

* Adds SSH Examples

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* Update README.md

* x

* c

* Update README.md

* Integration tests

* Update integration-beta.yml

* More Changes

* Update README.md

* README Reorg

* Update README.md

* Update README.md

* Update README.md

* Adds correct metadata

* Update action.yml

* Formatting

* Update build.yml

* Adds CodeCov report

* Update README.md

* Test Coverage Improvements (#141)

* Test Coverage Improvements

* Update env.js

* More Test Coverage / Imps

* Update README.md

* Formatting

* Update git.js

* Test Names...

* Update action.yml

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-01-19 16:33:10 -05:00

145 lines
8.7 KiB
JavaScript

"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 });
const core_1 = require("@actions/core");
const constants_1 = require("./constants");
const execute_1 = require("./execute");
const util_1 = require("./util");
/** Generates the branch if it doesn't exist on the remote.
* @returns {Promise}
*/
function init() {
return __awaiter(this, void 0, void 0, function* () {
try {
if (util_1.isNullOrUndefined(constants_1.action.accessToken) &&
util_1.isNullOrUndefined(constants_1.action.gitHubToken) &&
util_1.isNullOrUndefined(constants_1.action.ssh)) {
core_1.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.");
throw Error("No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.");
}
if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) {
core_1.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`);
throw Error("Incorrectly formatted build folder. The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.");
}
console.log(`Deploying using ${constants_1.tokenType}... 🔑`);
yield execute_1.execute(`git init`, constants_1.workspace);
yield execute_1.execute(`git config user.name ${constants_1.action.name}`, constants_1.workspace);
yield execute_1.execute(`git config user.email ${constants_1.action.email}`, constants_1.workspace);
yield execute_1.execute(`git remote rm origin`, constants_1.workspace);
yield execute_1.execute(`git remote add origin ${constants_1.repositoryPath}`, constants_1.workspace);
yield execute_1.execute(`git fetch`, constants_1.workspace);
}
catch (error) {
console.log(`There was an error initializing the repository: ${error}`);
}
finally {
return Promise.resolve("Initialization step complete...");
}
});
}
exports.init = init;
/** Switches to the base branch.
* @returns {Promise}
*/
function switchToBaseBranch() {
return __awaiter(this, void 0, void 0, function* () {
yield execute_1.execute(`git checkout --progress --force ${constants_1.action.baseBranch ? constants_1.action.baseBranch : constants_1.action.defaultBranch}`, constants_1.workspace);
return Promise.resolve("Switched to the base branch...");
});
}
exports.switchToBaseBranch = switchToBaseBranch;
/** Generates the branch if it doesn't exist on the remote.
* @returns {Promise}
*/
function generateBranch() {
return __awaiter(this, void 0, void 0, function* () {
try {
if (util_1.isNullOrUndefined(constants_1.action.branch)) {
throw Error("Branch is required.");
}
console.log(`Creating ${constants_1.action.branch} branch... 🔧`);
yield switchToBaseBranch();
yield execute_1.execute(`git checkout --orphan ${constants_1.action.branch}`, constants_1.workspace);
yield execute_1.execute(`git reset --hard`, constants_1.workspace);
yield execute_1.execute(`git commit --allow-empty -m "Initial ${constants_1.action.branch} commit."`, constants_1.workspace);
yield execute_1.execute(`git push ${constants_1.repositoryPath} ${constants_1.action.branch}`, constants_1.workspace);
yield execute_1.execute(`git fetch`, constants_1.workspace);
}
catch (error) {
core_1.setFailed(`There was an error creating the deployment branch: ${error}`);
}
finally {
return Promise.resolve("Deployment branch creation step complete... ✅");
}
});
}
exports.generateBranch = generateBranch;
/** Runs the necessary steps to make the deployment.
* @returns {Promise}
*/
function deploy() {
return __awaiter(this, void 0, void 0, function* () {
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.
*/
const branchExists = yield execute_1.execute(`git ls-remote --heads ${constants_1.repositoryPath} ${constants_1.action.branch} | wc -l`, constants_1.workspace);
if (!branchExists && !constants_1.action.isTest) {
console.log("Deployment branch does not exist. Creating....");
yield generateBranch();
}
// Checks out the base branch to begin the deployment process.
yield switchToBaseBranch();
yield execute_1.execute(`git fetch ${constants_1.repositoryPath}`, constants_1.workspace);
yield execute_1.execute(`git worktree add --checkout ${temporaryDeploymentDirectory} origin/${constants_1.action.branch}`, constants_1.workspace);
// Ensures that items that need to be excluded from the clean job get parsed.
let excludes = "";
if (constants_1.action.clean && constants_1.action.cleanExclude) {
try {
const excludedItems = JSON.parse(constants_1.action.cleanExclude);
excludedItems.forEach((item) => (excludes += `--exclude ${item} `));
}
catch (_a) {
console.log("There was an error parsing your CLEAN_EXCLUDE items. Please refer to the README for more details. ❌");
}
}
/*
Pushes all of the build files into the deployment directory.
Allows the user to specify the root if '.' is provided.
rysync is used to prevent file duplication. */
yield execute_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${constants_1.action.targetFolder
? `${temporaryDeploymentDirectory}/${constants_1.action.targetFolder}`
: temporaryDeploymentDirectory} ${constants_1.action.clean
? `--delete ${excludes} --exclude CNAME --exclude .nojekyll`
: ""} --exclude .ssh --exclude .git --exclude .github ${constants_1.action.build === constants_1.root ? `--exclude ${temporaryDeploymentDirectory}` : ""}`, constants_1.workspace);
const hasFilesToCommit = yield execute_1.execute(`git status --porcelain`, temporaryDeploymentDirectory);
if (!hasFilesToCommit && !constants_1.action.isTest) {
console.log("There is nothing to commit. Exiting... ✅");
return Promise.resolve();
}
// Commits to GitHub.
yield execute_1.execute(`git add --all .`, temporaryDeploymentDirectory);
yield execute_1.execute(`git checkout -b ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory);
yield execute_1.execute(`git commit -m "${!util_1.isNullOrUndefined(constants_1.action.commitMessage)
? constants_1.action.commitMessage
: `Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch}`} - ${process.env.GITHUB_SHA} 🚀" --quiet`, temporaryDeploymentDirectory);
yield execute_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory);
// Cleans up temporary files/folders and restores the git state.
console.log("Running post deployment cleanup jobs... 🔧");
yield execute_1.execute(`rm -rf ${temporaryDeploymentDirectory}`, constants_1.workspace);
yield execute_1.execute(`git checkout --progress --force ${constants_1.action.defaultBranch}`, constants_1.workspace);
return Promise.resolve("Commit step complete...");
});
}
exports.deploy = deploy;