ghaction-github-pages/lib/main.js

116 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-10-11 07:41:16 +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 child_process = __importStar(require("child_process"));
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
2019-11-15 23:09:01 +08:00
const ncp_1 = require("ncp");
const fs = __importStar(require("fs"));
2019-11-15 22:57:27 +08:00
const os = __importStar(require("os"));
const path = __importStar(require("path"));
2019-10-11 07:41:16 +08:00
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const repo = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
const target_branch = core.getInput('target_branch') || 'gh-pages';
const keep_history = /true/i.test(core.getInput('keep_history'));
const allow_empty_commit = /true/i.test(core.getInput('allow_empty_commit'));
2019-10-11 07:41:16 +08:00
const build_dir = core.getInput('build_dir', { required: true });
const comitter_name = core.getInput('comitter_name') || process.env['GITHUB_ACTOR'] || 'github-actions';
const comitter_email = core.getInput('comitter_email') || `${comitter_name}@users.noreply.github.com`;
2019-11-06 10:02:59 +08:00
const commit_message = core.getInput('commit_message') || 'Deploy to GitHub pages';
const fqdn = core.getInput('fqdn');
2019-10-11 07:41:16 +08:00
if (!fs.existsSync(build_dir)) {
core.setFailed('⛔️ Build dir does not exist');
return;
}
if (fqdn) {
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(build_dir, 'CNAME')}`);
fs.writeFileSync(path.join(build_dir, 'CNAME'), fqdn.trim());
}
let remote_url = String('https://');
if (process.env['GITHUB_PAT']) {
core.info(`✅ Use GITHUB_PAT`);
remote_url = remote_url.concat(process.env['GITHUB_PAT']);
}
else if (process.env['GITHUB_TOKEN']) {
core.info(`✅ Use GITHUB_TOKEN`);
remote_url = remote_url.concat('x-access-token:', process.env['GITHUB_TOKEN']);
}
else {
core.setFailed('❌️ You have to provide a GITHUB_TOKEN or GITHUB_PAT');
return;
}
remote_url = remote_url.concat('@github.com/', repo, '.git');
2019-11-15 22:57:27 +08:00
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'github-pages-'));
process.chdir(tmpdir);
core.info(`🏃 Copying ${path.resolve(build_dir)} contents to ${tmpdir}`);
2019-11-15 23:09:01 +08:00
ncp_1.ncp.ncp(path.resolve(build_dir), tmpdir, err => {
2019-11-15 23:02:43 +08:00
if (err !== null) {
core.setFailed(`⛔️ Failed to copy ${path.resolve(build_dir)}`);
}
2019-11-15 22:57:27 +08:00
});
const remote_branch_exists = child_process.execSync(`git ls-remote --heads ${remote_url} ${target_branch}`, { encoding: 'utf8' }).trim().length >
0;
if (remote_branch_exists) {
yield exec.exec('git', ['clone', '--quiet', '--branch', target_branch, '--depth', '1', remote_url]);
}
else {
core.info(`🏃 Initializing local git repo`);
yield exec.exec('git', ['init', '.']);
yield exec.exec('git', ['checkout', '--orphan', target_branch]);
}
2019-11-15 22:57:27 +08:00
core.info(`🔨 Configuring git committer to be ${comitter_name} <${comitter_email}>`);
yield exec.exec('git', ['config', 'user.name', comitter_name]);
yield exec.exec('git', ['config', 'user.email', comitter_email]);
2019-10-11 07:41:16 +08:00
try {
child_process.execSync('git status --porcelain').toString();
}
catch (err) {
core.info('⚠️ Nothing to deploy');
2019-10-11 07:41:16 +08:00
return;
}
2019-11-15 22:01:51 +08:00
yield exec.exec('git', ['add', '--all', '.']);
let gitCommitCmd = [];
gitCommitCmd.push('commit');
if (allow_empty_commit) {
core.info(`✅ Allow empty commit`);
gitCommitCmd.push('--allow-empty');
}
gitCommitCmd.push('-m', commit_message);
yield exec.exec('git', gitCommitCmd);
yield exec.exec('git', ['show', '--stat-count=10', 'HEAD']);
let gitPushCmd = [];
gitPushCmd.push('push', '--quiet');
if (!keep_history) {
core.info(`✅ Force push`);
gitPushCmd.push('--force');
}
gitPushCmd.push(remote_url, target_branch);
2019-11-15 22:57:27 +08:00
core.info(`🏃 Deploying ${build_dir} directory to ${target_branch} branch on ${repo} repo`);
yield exec.exec('git', gitPushCmd);
2019-10-11 07:41:16 +08:00
process.chdir(process.env['GITHUB_WORKSPACE'] || '.');
2019-10-11 07:47:41 +08:00
core.info(`🎉 Content of ${build_dir} has been deployed to GitHub Pages.`);
2019-10-11 07:41:16 +08:00
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();