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

114 lines
6.2 KiB
JavaScript
Raw Normal View History

2019-11-08 06:46:04 +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());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
2019-11-11 05:16:30 +08:00
const io_1 = require("@actions/io");
2019-11-08 06:46:04 +08:00
const util_1 = require("./util");
const constants_1 = require("./constants");
2019-11-09 21:29:29 +08:00
/** Generates the branch if it doesn't exist on the remote.
* @returns {Promise}
*/
2019-11-08 06:46:04 +08:00
function init() {
return __awaiter(this, void 0, void 0, function* () {
try {
2019-11-09 21:29:29 +08:00
if (!constants_1.action.accessToken && !constants_1.action.gitHubToken) {
return core.setFailed("You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy.");
2019-11-08 06:46:04 +08:00
}
2019-11-11 05:31:05 +08:00
if (constants_1.action.build.startsWith("/") || constants_1.action.build.startsWith("./")) {
return core.setFailed(`The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.`);
}
2019-11-11 04:36:26 +08:00
yield util_1.execute(`git init`, constants_1.workspace);
yield util_1.execute(`git config user.name ${constants_1.action.pusher.name}`, constants_1.workspace);
yield util_1.execute(`git config user.email ${constants_1.action.pusher.email}`, constants_1.workspace);
2019-11-08 06:46:04 +08:00
}
catch (error) {
core.setFailed(`There was an error initializing the repository: ${error}`);
}
finally {
2019-11-09 21:29:29 +08:00
return Promise.resolve("Initialization step complete...");
2019-11-08 06:46:04 +08:00
}
});
}
exports.init = init;
2019-11-09 21:29:29 +08:00
/** Generates the branch if it doesn't exist on the remote.
* @returns {Promise}
*/
2019-11-08 06:46:04 +08:00
function generateBranch() {
return __awaiter(this, void 0, void 0, function* () {
try {
console.log(`Creating ${constants_1.action.branch} branch...`);
yield util_1.execute(`git switch ${constants_1.action.baseBranch || "master"}`, constants_1.workspace);
yield util_1.execute(`git switch --orphan ${constants_1.action.branch}`, constants_1.workspace);
2019-11-08 06:46:04 +08:00
yield util_1.execute(`git reset --hard`, constants_1.workspace);
yield util_1.execute(`git commit --allow-empty -m "Initial ${constants_1.action.branch} commit."`, constants_1.workspace);
yield util_1.execute(`git push ${constants_1.repositoryPath} ${constants_1.action.branch}`, constants_1.workspace);
2019-11-11 03:16:01 +08:00
// Switches back to the base branch.
yield util_1.execute(`git switch ${constants_1.action.baseBranch || "master"}`, constants_1.workspace);
2019-11-08 06:46:04 +08:00
}
catch (error) {
core.setFailed(`There was an error creating the deployment branch: ${error}`);
}
finally {
2019-11-09 21:29:29 +08:00
return Promise.resolve("Deployment branch creation step complete...");
2019-11-08 06:46:04 +08:00
}
});
}
exports.generateBranch = generateBranch;
2019-11-09 21:29:29 +08:00
/** Runs the necessary steps to make the deployment.
* @returns {Promise}
*/
2019-11-08 06:46:04 +08:00
function deploy() {
return __awaiter(this, void 0, void 0, function* () {
2019-11-11 05:31:05 +08:00
const temporaryDeploymentDirectory = "gh-action-temp-deployment-folder";
const temporaryDeploymentBranch = "gh-action-temp-deployment-branch";
2019-11-08 06:46:04 +08:00
/*
2019-11-09 21:29:29 +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.
2019-11-11 04:27:06 +08:00
*/
const branchExists = yield util_1.execute(`git ls-remote --heads ${constants_1.repositoryPath} ${constants_1.action.branch} | wc -l`, constants_1.workspace);
2019-11-08 06:46:04 +08:00
if (!branchExists) {
2019-11-11 04:27:06 +08:00
console.log("Deployment branch does not exist. Creating....");
yield generateBranch();
}
2019-11-11 04:42:45 +08:00
// Checks out the base branch to begin the deployment process.
2019-11-11 04:46:27 +08:00
yield util_1.execute(`git checkout ${constants_1.action.baseBranch || "master"}`, constants_1.workspace);
2019-11-11 04:42:45 +08:00
yield util_1.execute(`git fetch origin`, constants_1.workspace);
2019-11-11 04:27:06 +08:00
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. */
2019-11-11 05:31:05 +08:00
if (constants_1.action.build === constants_1.root) {
// rsync is executed here so the .git and temporary deployment directories don't get duplicated.
2019-11-11 06:01:54 +08:00
yield util_1.execute(`rsync -q -av --progress ${constants_1.action.build}/. ${temporaryDeploymentDirectory} --exclude .git --exclude .github --exclude ${temporaryDeploymentDirectory}`, constants_1.workspace);
2019-11-11 05:16:30 +08:00
}
else {
yield io_1.cp(`${constants_1.action.build}/.`, temporaryDeploymentDirectory, {
recursive: true,
force: true
});
}
2019-11-11 04:27:06 +08:00
// Commits to GitHub.
yield util_1.execute(`git add --all .`, temporaryDeploymentDirectory);
yield util_1.execute(`git switch -c ${temporaryDeploymentBranch}`, temporaryDeploymentDirectory);
yield util_1.execute(`git commit -m "Deploying to ${constants_1.action.branch} from ${constants_1.action.baseBranch} ${process.env.GITHUB_SHA}" --quiet`, temporaryDeploymentDirectory);
yield util_1.execute(`git push --force ${constants_1.repositoryPath} ${temporaryDeploymentBranch}:${constants_1.action.branch}`, temporaryDeploymentDirectory);
2019-11-09 21:29:29 +08:00
return Promise.resolve("Commit step complete...");
2019-11-08 06:46:04 +08:00
});
}
exports.deploy = deploy;