2019-10-11 07:41:16 +08:00
|
|
|
import * as child_process from 'child_process';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2019-11-15 23:20:34 +08:00
|
|
|
import {copySync} from 'fs-extra';
|
2019-11-06 09:46:35 +08:00
|
|
|
import * as fs from 'fs';
|
2019-11-15 22:57:27 +08:00
|
|
|
import * as os from 'os';
|
2019-11-15 21:40:43 +08:00
|
|
|
import * as path from 'path';
|
2019-10-11 07:41:16 +08:00
|
|
|
|
|
|
|
async function run() {
|
|
|
|
try {
|
2019-11-15 21:40:43 +08:00
|
|
|
const repo: string = core.getInput('repo') || process.env['GITHUB_REPOSITORY'] || '';
|
|
|
|
const target_branch: string = core.getInput('target_branch') || 'gh-pages';
|
|
|
|
const keep_history: boolean = /true/i.test(core.getInput('keep_history'));
|
|
|
|
const allow_empty_commit: boolean = /true/i.test(core.getInput('allow_empty_commit'));
|
|
|
|
const build_dir: string = core.getInput('build_dir', {required: true});
|
2019-11-16 22:04:57 +08:00
|
|
|
const committer_name: string = core.getInput('committer_name') || process.env['GITHUB_ACTOR'] || 'github-actions';
|
|
|
|
const committer_email: string = core.getInput('committer_email') || `${committer_name}@users.noreply.github.com`;
|
2019-11-15 21:40:43 +08:00
|
|
|
const commit_message: string = core.getInput('commit_message') || 'Deploy to GitHub pages';
|
|
|
|
const fqdn: string = core.getInput('fqdn');
|
2019-10-11 07:41:16 +08:00
|
|
|
|
|
|
|
if (!fs.existsSync(build_dir)) {
|
|
|
|
core.setFailed('⛔️ Build dir does not exist');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:25:50 +08:00
|
|
|
let remote_url = String('https://');
|
|
|
|
if (process.env['GITHUB_PAT']) {
|
|
|
|
core.info(`✅ Use GITHUB_PAT`);
|
2019-11-19 07:33:27 +08:00
|
|
|
remote_url = remote_url.concat(process.env['GITHUB_PAT'].trim());
|
2019-11-15 22:25:50 +08:00
|
|
|
} else if (process.env['GITHUB_TOKEN']) {
|
|
|
|
core.info(`✅ Use GITHUB_TOKEN`);
|
2019-11-19 07:33:27 +08:00
|
|
|
remote_url = remote_url.concat('x-access-token:', process.env['GITHUB_TOKEN'].trim());
|
2019-11-15 22:25:50 +08:00
|
|
|
} 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-'));
|
2019-11-15 23:29:46 +08:00
|
|
|
const currentdir = path.resolve('.');
|
2019-11-15 22:57:27 +08:00
|
|
|
process.chdir(tmpdir);
|
|
|
|
|
2019-11-15 22:25:50 +08:00
|
|
|
const remote_branch_exists =
|
|
|
|
child_process.execSync(`git ls-remote --heads ${remote_url} ${target_branch}`, {encoding: 'utf8'}).trim().length >
|
|
|
|
0;
|
2019-11-19 20:15:27 +08:00
|
|
|
if (keep_history && remote_branch_exists) {
|
2019-11-15 23:36:31 +08:00
|
|
|
await exec.exec('git', ['clone', '--quiet', '--branch', target_branch, '--depth', '1', remote_url, '.']);
|
2019-11-15 22:25:50 +08:00
|
|
|
} else {
|
|
|
|
core.info(`🏃 Initializing local git repo`);
|
|
|
|
await exec.exec('git', ['init', '.']);
|
|
|
|
await exec.exec('git', ['checkout', '--orphan', target_branch]);
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:36:31 +08:00
|
|
|
core.info(`🏃 Copying ${path.join(currentdir, build_dir)} contents to ${tmpdir}`);
|
|
|
|
copySync(path.join(currentdir, build_dir), tmpdir);
|
|
|
|
|
|
|
|
if (fqdn) {
|
2019-11-26 15:31:04 +08:00
|
|
|
core.info(`✍️ Writing ${fqdn} domain name to ${path.join(tmpdir, 'CNAME')}`);
|
|
|
|
fs.writeFileSync(path.join(tmpdir, 'CNAME'), fqdn.trim());
|
2019-11-15 23:36:31 +08:00
|
|
|
}
|
|
|
|
|
2019-11-16 22:04:57 +08:00
|
|
|
core.info(`🔨 Configuring git committer to be ${committer_name} <${committer_email}>`);
|
|
|
|
await exec.exec('git', ['config', 'user.name', committer_name]);
|
|
|
|
await exec.exec('git', ['config', 'user.email', committer_email]);
|
2019-10-11 07:41:16 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
child_process.execSync('git status --porcelain').toString();
|
|
|
|
} catch (err) {
|
2019-11-15 21:40:43 +08:00
|
|
|
core.info('⚠️ Nothing to deploy');
|
2019-10-11 07:41:16 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:01:51 +08:00
|
|
|
await exec.exec('git', ['add', '--all', '.']);
|
2019-11-15 21:40:43 +08:00
|
|
|
|
|
|
|
let gitCommitCmd: Array<string> = [];
|
|
|
|
gitCommitCmd.push('commit');
|
|
|
|
if (allow_empty_commit) {
|
|
|
|
core.info(`✅ Allow empty commit`);
|
|
|
|
gitCommitCmd.push('--allow-empty');
|
|
|
|
}
|
|
|
|
gitCommitCmd.push('-m', commit_message);
|
|
|
|
await exec.exec('git', gitCommitCmd);
|
|
|
|
|
|
|
|
await exec.exec('git', ['show', '--stat-count=10', 'HEAD']);
|
2019-10-11 07:41:16 +08:00
|
|
|
|
2019-11-15 21:40:43 +08:00
|
|
|
let gitPushCmd: Array<string> = [];
|
|
|
|
gitPushCmd.push('push', '--quiet');
|
|
|
|
if (!keep_history) {
|
|
|
|
core.info(`✅ Force push`);
|
|
|
|
gitPushCmd.push('--force');
|
|
|
|
}
|
2019-11-15 22:25:50 +08:00
|
|
|
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`);
|
2019-11-15 21:40:43 +08:00
|
|
|
await exec.exec('git', gitPushCmd);
|
2019-10-11 07:41:16 +08:00
|
|
|
|
2019-11-15 23:29:46 +08:00
|
|
|
process.chdir(currentdir);
|
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();
|